1#!/usr/bin/perl 2 3# gitweb - simple web interface to track changes in git repositories 4# 5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> 6# (C) 2005, Christian Gierke 7# 8# This program is licensed under the GPLv2 9 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21BEGIN{ 22 CGI->compile()if$ENV{'MOD_PERL'}; 23} 24 25our$cgi= new CGI; 26our$version="++GIT_VERSION++"; 27our$my_url=$cgi->url(); 28our$my_uri=$cgi->url(-absolute =>1); 29 30# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 31# needed and used only for URLs with nonempty PATH_INFO 32our$base_url=$my_url; 33 34# When the script is used as DirectoryIndex, the URL does not contain the name 35# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 36# have to do it ourselves. We make $path_info global because it's also used 37# later on. 38# 39# Another issue with the script being the DirectoryIndex is that the resulting 40# $my_url data is not the full script URL: this is good, because we want 41# generated links to keep implying the script name if it wasn't explicitly 42# indicated in the URL we're handling, but it means that $my_url cannot be used 43# as base URL. 44# Therefore, if we needed to strip PATH_INFO, then we know that we have 45# to build the base URL ourselves: 46our$path_info=$ENV{"PATH_INFO"}; 47if($path_info) { 48if($my_url=~ s,\Q$path_info\E$,, && 49$my_uri=~ s,\Q$path_info\E$,, && 50defined$ENV{'SCRIPT_NAME'}) { 51$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 52} 53} 54 55# core git executable to use 56# this can just be "git" if your webserver has a sensible PATH 57our$GIT="++GIT_BINDIR++/git"; 58 59# absolute fs-path which will be prepended to the project path 60#our $projectroot = "/pub/scm"; 61our$projectroot="++GITWEB_PROJECTROOT++"; 62 63# fs traversing limit for getting project list 64# the number is relative to the projectroot 65our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 66 67# target of the home link on top of all pages 68our$home_link=$my_uri||"/"; 69 70# string of the home link on top of all pages 71our$home_link_str="++GITWEB_HOME_LINK_STR++"; 72 73# name of your site or organization to appear in page titles 74# replace this with something more descriptive for clearer bookmarks 75our$site_name="++GITWEB_SITENAME++" 76|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 77 78# filename of html text to include at top of each page 79our$site_header="++GITWEB_SITE_HEADER++"; 80# html text to include at home page 81our$home_text="++GITWEB_HOMETEXT++"; 82# filename of html text to include at bottom of each page 83our$site_footer="++GITWEB_SITE_FOOTER++"; 84 85# URI of stylesheets 86our@stylesheets= ("++GITWEB_CSS++"); 87# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 88our$stylesheet=undef; 89# URI of GIT logo (72x27 size) 90our$logo="++GITWEB_LOGO++"; 91# URI of GIT favicon, assumed to be image/png type 92our$favicon="++GITWEB_FAVICON++"; 93 94# URI and label (title) of GIT logo link 95#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 96#our $logo_label = "git documentation"; 97our$logo_url="http://git.or.cz/"; 98our$logo_label="git homepage"; 99 100# source of projects list 101our$projects_list="++GITWEB_LIST++"; 102 103# the width (in characters) of the projects list "Description" column 104our$projects_list_description_width=25; 105 106# default order of projects list 107# valid values are none, project, descr, owner, and age 108our$default_projects_order="project"; 109 110# show repository only if this file exists 111# (only effective if this variable evaluates to true) 112our$export_ok="++GITWEB_EXPORT_OK++"; 113 114# show repository only if this subroutine returns true 115# when given the path to the project, for example: 116# sub { return -e "$_[0]/git-daemon-export-ok"; } 117our$export_auth_hook=undef; 118 119# only allow viewing of repositories also shown on the overview page 120our$strict_export="++GITWEB_STRICT_EXPORT++"; 121 122# list of git base URLs used for URL to where fetch project from, 123# i.e. full URL is "$git_base_url/$project" 124our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 125 126# default blob_plain mimetype and default charset for text/plain blob 127our$default_blob_plain_mimetype='text/plain'; 128our$default_text_plain_charset=undef; 129 130# file to use for guessing MIME types before trying /etc/mime.types 131# (relative to the current git repository) 132our$mimetypes_file=undef; 133 134# assume this charset if line contains non-UTF-8 characters; 135# it should be valid encoding (see Encoding::Supported(3pm) for list), 136# for which encoding all byte sequences are valid, for example 137# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 138# could be even 'utf-8' for the old behavior) 139our$fallback_encoding='latin1'; 140 141# rename detection options for git-diff and git-diff-tree 142# - default is '-M', with the cost proportional to 143# (number of removed files) * (number of new files). 144# - more costly is '-C' (which implies '-M'), with the cost proportional to 145# (number of changed files + number of removed files) * (number of new files) 146# - even more costly is '-C', '--find-copies-harder' with cost 147# (number of files in the original tree) * (number of new files) 148# - one might want to include '-B' option, e.g. '-B', '-M' 149our@diff_opts= ('-M');# taken from git_commit 150 151# Disables features that would allow repository owners to inject script into 152# the gitweb domain. 153our$prevent_xss=0; 154 155# information about snapshot formats that gitweb is capable of serving 156our%known_snapshot_formats= ( 157# name => { 158# 'display' => display name, 159# 'type' => mime type, 160# 'suffix' => filename suffix, 161# 'format' => --format for git-archive, 162# 'compressor' => [compressor command and arguments] 163# (array reference, optional)} 164# 165'tgz'=> { 166'display'=>'tar.gz', 167'type'=>'application/x-gzip', 168'suffix'=>'.tar.gz', 169'format'=>'tar', 170'compressor'=> ['gzip']}, 171 172'tbz2'=> { 173'display'=>'tar.bz2', 174'type'=>'application/x-bzip2', 175'suffix'=>'.tar.bz2', 176'format'=>'tar', 177'compressor'=> ['bzip2']}, 178 179'zip'=> { 180'display'=>'zip', 181'type'=>'application/x-zip', 182'suffix'=>'.zip', 183'format'=>'zip'}, 184); 185 186# Aliases so we understand old gitweb.snapshot values in repository 187# configuration. 188our%known_snapshot_format_aliases= ( 189'gzip'=>'tgz', 190'bzip2'=>'tbz2', 191 192# backward compatibility: legacy gitweb config support 193'x-gzip'=>undef,'gz'=>undef, 194'x-bzip2'=>undef,'bz2'=>undef, 195'x-zip'=>undef,''=>undef, 196); 197 198# You define site-wide feature defaults here; override them with 199# $GITWEB_CONFIG as necessary. 200our%feature= ( 201# feature => { 202# 'sub' => feature-sub (subroutine), 203# 'override' => allow-override (boolean), 204# 'default' => [ default options...] (array reference)} 205# 206# if feature is overridable (it means that allow-override has true value), 207# then feature-sub will be called with default options as parameters; 208# return value of feature-sub indicates if to enable specified feature 209# 210# if there is no 'sub' key (no feature-sub), then feature cannot be 211# overriden 212# 213# use gitweb_get_feature(<feature>) to retrieve the <feature> value 214# (an array) or gitweb_check_feature(<feature>) to check if <feature> 215# is enabled 216 217# Enable the 'blame' blob view, showing the last commit that modified 218# each line in the file. This can be very CPU-intensive. 219 220# To enable system wide have in $GITWEB_CONFIG 221# $feature{'blame'}{'default'} = [1]; 222# To have project specific config enable override in $GITWEB_CONFIG 223# $feature{'blame'}{'override'} = 1; 224# and in project config gitweb.blame = 0|1; 225'blame'=> { 226'sub'=>sub{ feature_bool('blame',@_) }, 227'override'=>0, 228'default'=> [0]}, 229 230# Enable the 'snapshot' link, providing a compressed archive of any 231# tree. This can potentially generate high traffic if you have large 232# project. 233 234# Value is a list of formats defined in %known_snapshot_formats that 235# you wish to offer. 236# To disable system wide have in $GITWEB_CONFIG 237# $feature{'snapshot'}{'default'} = []; 238# To have project specific config enable override in $GITWEB_CONFIG 239# $feature{'snapshot'}{'override'} = 1; 240# and in project config, a comma-separated list of formats or "none" 241# to disable. Example: gitweb.snapshot = tbz2,zip; 242'snapshot'=> { 243'sub'=> \&feature_snapshot, 244'override'=>0, 245'default'=> ['tgz']}, 246 247# Enable text search, which will list the commits which match author, 248# committer or commit text to a given string. Enabled by default. 249# Project specific override is not supported. 250'search'=> { 251'override'=>0, 252'default'=> [1]}, 253 254# Enable grep search, which will list the files in currently selected 255# tree containing the given string. Enabled by default. This can be 256# potentially CPU-intensive, of course. 257 258# To enable system wide have in $GITWEB_CONFIG 259# $feature{'grep'}{'default'} = [1]; 260# To have project specific config enable override in $GITWEB_CONFIG 261# $feature{'grep'}{'override'} = 1; 262# and in project config gitweb.grep = 0|1; 263'grep'=> { 264'sub'=>sub{ feature_bool('grep',@_) }, 265'override'=>0, 266'default'=> [1]}, 267 268# Enable the pickaxe search, which will list the commits that modified 269# a given string in a file. This can be practical and quite faster 270# alternative to 'blame', but still potentially CPU-intensive. 271 272# To enable system wide have in $GITWEB_CONFIG 273# $feature{'pickaxe'}{'default'} = [1]; 274# To have project specific config enable override in $GITWEB_CONFIG 275# $feature{'pickaxe'}{'override'} = 1; 276# and in project config gitweb.pickaxe = 0|1; 277'pickaxe'=> { 278'sub'=>sub{ feature_bool('pickaxe',@_) }, 279'override'=>0, 280'default'=> [1]}, 281 282# Make gitweb use an alternative format of the URLs which can be 283# more readable and natural-looking: project name is embedded 284# directly in the path and the query string contains other 285# auxiliary information. All gitweb installations recognize 286# URL in either format; this configures in which formats gitweb 287# generates links. 288 289# To enable system wide have in $GITWEB_CONFIG 290# $feature{'pathinfo'}{'default'} = [1]; 291# Project specific override is not supported. 292 293# Note that you will need to change the default location of CSS, 294# favicon, logo and possibly other files to an absolute URL. Also, 295# if gitweb.cgi serves as your indexfile, you will need to force 296# $my_uri to contain the script name in your $GITWEB_CONFIG. 297'pathinfo'=> { 298'override'=>0, 299'default'=> [0]}, 300 301# Make gitweb consider projects in project root subdirectories 302# to be forks of existing projects. Given project $projname.git, 303# projects matching $projname/*.git will not be shown in the main 304# projects list, instead a '+' mark will be added to $projname 305# there and a 'forks' view will be enabled for the project, listing 306# all the forks. If project list is taken from a file, forks have 307# to be listed after the main project. 308 309# To enable system wide have in $GITWEB_CONFIG 310# $feature{'forks'}{'default'} = [1]; 311# Project specific override is not supported. 312'forks'=> { 313'override'=>0, 314'default'=> [0]}, 315 316# Insert custom links to the action bar of all project pages. 317# This enables you mainly to link to third-party scripts integrating 318# into gitweb; e.g. git-browser for graphical history representation 319# or custom web-based repository administration interface. 320 321# The 'default' value consists of a list of triplets in the form 322# (label, link, position) where position is the label after which 323# to insert the link and link is a format string where %n expands 324# to the project name, %f to the project path within the filesystem, 325# %h to the current hash (h gitweb parameter) and %b to the current 326# hash base (hb gitweb parameter); %% expands to %. 327 328# To enable system wide have in $GITWEB_CONFIG e.g. 329# $feature{'actions'}{'default'} = [('graphiclog', 330# '/git-browser/by-commit.html?r=%n', 'summary')]; 331# Project specific override is not supported. 332'actions'=> { 333'override'=>0, 334'default'=> []}, 335 336# Allow gitweb scan project content tags described in ctags/ 337# of project repository, and display the popular Web 2.0-ish 338# "tag cloud" near the project list. Note that this is something 339# COMPLETELY different from the normal Git tags. 340 341# gitweb by itself can show existing tags, but it does not handle 342# tagging itself; you need an external application for that. 343# For an example script, check Girocco's cgi/tagproj.cgi. 344# You may want to install the HTML::TagCloud Perl module to get 345# a pretty tag cloud instead of just a list of tags. 346 347# To enable system wide have in $GITWEB_CONFIG 348# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 349# Project specific override is not supported. 350'ctags'=> { 351'override'=>0, 352'default'=> [0]}, 353 354# The maximum number of patches in a patchset generated in patch 355# view. Set this to 0 or undef to disable patch view, or to a 356# negative number to remove any limit. 357 358# To disable system wide have in $GITWEB_CONFIG 359# $feature{'patches'}{'default'} = [0]; 360# To have project specific config enable override in $GITWEB_CONFIG 361# $feature{'patches'}{'override'} = 1; 362# and in project config gitweb.patches = 0|n; 363# where n is the maximum number of patches allowed in a patchset. 364'patches'=> { 365'sub'=> \&feature_patches, 366'override'=>0, 367'default'=> [16]}, 368); 369 370sub gitweb_get_feature { 371my($name) =@_; 372return unlessexists$feature{$name}; 373my($sub,$override,@defaults) = ( 374$feature{$name}{'sub'}, 375$feature{$name}{'override'}, 376@{$feature{$name}{'default'}}); 377if(!$override) {return@defaults; } 378if(!defined$sub) { 379warn"feature$nameis not overrideable"; 380return@defaults; 381} 382return$sub->(@defaults); 383} 384 385# A wrapper to check if a given feature is enabled. 386# With this, you can say 387# 388# my $bool_feat = gitweb_check_feature('bool_feat'); 389# gitweb_check_feature('bool_feat') or somecode; 390# 391# instead of 392# 393# my ($bool_feat) = gitweb_get_feature('bool_feat'); 394# (gitweb_get_feature('bool_feat'))[0] or somecode; 395# 396sub gitweb_check_feature { 397return(gitweb_get_feature(@_))[0]; 398} 399 400 401sub feature_bool { 402my$key=shift; 403my($val) = git_get_project_config($key,'--bool'); 404 405if(!defined$val) { 406return($_[0]); 407}elsif($valeq'true') { 408return(1); 409}elsif($valeq'false') { 410return(0); 411} 412} 413 414sub feature_snapshot { 415my(@fmts) =@_; 416 417my($val) = git_get_project_config('snapshot'); 418 419if($val) { 420@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 421} 422 423return@fmts; 424} 425 426sub feature_patches { 427my@val= (git_get_project_config('patches','--int')); 428 429if(@val) { 430return@val; 431} 432 433return($_[0]); 434} 435 436# checking HEAD file with -e is fragile if the repository was 437# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 438# and then pruned. 439sub check_head_link { 440my($dir) =@_; 441my$headfile="$dir/HEAD"; 442return((-e $headfile) || 443(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 444} 445 446sub check_export_ok { 447my($dir) =@_; 448return(check_head_link($dir) && 449(!$export_ok|| -e "$dir/$export_ok") && 450(!$export_auth_hook||$export_auth_hook->($dir))); 451} 452 453# process alternate names for backward compatibility 454# filter out unsupported (unknown) snapshot formats 455sub filter_snapshot_fmts { 456my@fmts=@_; 457 458@fmts=map{ 459exists$known_snapshot_format_aliases{$_} ? 460$known_snapshot_format_aliases{$_} :$_}@fmts; 461@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 462 463} 464 465our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 466if(-e $GITWEB_CONFIG) { 467do$GITWEB_CONFIG; 468}else{ 469our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 470do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 471} 472 473# version of the core git binary 474our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 475 476$projects_list||=$projectroot; 477 478# ====================================================================== 479# input validation and dispatch 480 481# input parameters can be collected from a variety of sources (presently, CGI 482# and PATH_INFO), so we define an %input_params hash that collects them all 483# together during validation: this allows subsequent uses (e.g. href()) to be 484# agnostic of the parameter origin 485 486our%input_params= (); 487 488# input parameters are stored with the long parameter name as key. This will 489# also be used in the href subroutine to convert parameters to their CGI 490# equivalent, and since the href() usage is the most frequent one, we store 491# the name -> CGI key mapping here, instead of the reverse. 492# 493# XXX: Warning: If you touch this, check the search form for updating, 494# too. 495 496our@cgi_param_mapping= ( 497 project =>"p", 498 action =>"a", 499 file_name =>"f", 500 file_parent =>"fp", 501 hash =>"h", 502 hash_parent =>"hp", 503 hash_base =>"hb", 504 hash_parent_base =>"hpb", 505 page =>"pg", 506 order =>"o", 507 searchtext =>"s", 508 searchtype =>"st", 509 snapshot_format =>"sf", 510 extra_options =>"opt", 511 search_use_regexp =>"sr", 512); 513our%cgi_param_mapping=@cgi_param_mapping; 514 515# we will also need to know the possible actions, for validation 516our%actions= ( 517"blame"=> \&git_blame, 518"blobdiff"=> \&git_blobdiff, 519"blobdiff_plain"=> \&git_blobdiff_plain, 520"blob"=> \&git_blob, 521"blob_plain"=> \&git_blob_plain, 522"commitdiff"=> \&git_commitdiff, 523"commitdiff_plain"=> \&git_commitdiff_plain, 524"commit"=> \&git_commit, 525"forks"=> \&git_forks, 526"heads"=> \&git_heads, 527"history"=> \&git_history, 528"log"=> \&git_log, 529"patch"=> \&git_patch, 530"patches"=> \&git_patches, 531"rss"=> \&git_rss, 532"atom"=> \&git_atom, 533"search"=> \&git_search, 534"search_help"=> \&git_search_help, 535"shortlog"=> \&git_shortlog, 536"summary"=> \&git_summary, 537"tag"=> \&git_tag, 538"tags"=> \&git_tags, 539"tree"=> \&git_tree, 540"snapshot"=> \&git_snapshot, 541"object"=> \&git_object, 542# those below don't need $project 543"opml"=> \&git_opml, 544"project_list"=> \&git_project_list, 545"project_index"=> \&git_project_index, 546); 547 548# finally, we have the hash of allowed extra_options for the commands that 549# allow them 550our%allowed_options= ( 551"--no-merges"=> [qw(rss atom log shortlog history)], 552); 553 554# fill %input_params with the CGI parameters. All values except for 'opt' 555# should be single values, but opt can be an array. We should probably 556# build an array of parameters that can be multi-valued, but since for the time 557# being it's only this one, we just single it out 558while(my($name,$symbol) =each%cgi_param_mapping) { 559if($symboleq'opt') { 560$input_params{$name} = [$cgi->param($symbol) ]; 561}else{ 562$input_params{$name} =$cgi->param($symbol); 563} 564} 565 566# now read PATH_INFO and update the parameter list for missing parameters 567sub evaluate_path_info { 568return ifdefined$input_params{'project'}; 569return if!$path_info; 570$path_info=~ s,^/+,,; 571return if!$path_info; 572 573# find which part of PATH_INFO is project 574my$project=$path_info; 575$project=~ s,/+$,,; 576while($project&& !check_head_link("$projectroot/$project")) { 577$project=~ s,/*[^/]*$,,; 578} 579return unless$project; 580$input_params{'project'} =$project; 581 582# do not change any parameters if an action is given using the query string 583return if$input_params{'action'}; 584$path_info=~ s,^\Q$project\E/*,,; 585 586# next, check if we have an action 587my$action=$path_info; 588$action=~ s,/.*$,,; 589if(exists$actions{$action}) { 590$path_info=~ s,^$action/*,,; 591$input_params{'action'} =$action; 592} 593 594# list of actions that want hash_base instead of hash, but can have no 595# pathname (f) parameter 596my@wants_base= ( 597'tree', 598'history', 599); 600 601# we want to catch 602# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 603my($parentrefname,$parentpathname,$refname,$pathname) = 604($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 605 606# first, analyze the 'current' part 607if(defined$pathname) { 608# we got "branch:filename" or "branch:dir/" 609# we could use git_get_type(branch:pathname), but: 610# - it needs $git_dir 611# - it does a git() call 612# - the convention of terminating directories with a slash 613# makes it superfluous 614# - embedding the action in the PATH_INFO would make it even 615# more superfluous 616$pathname=~ s,^/+,,; 617if(!$pathname||substr($pathname, -1)eq"/") { 618$input_params{'action'} ||="tree"; 619$pathname=~ s,/$,,; 620}else{ 621# the default action depends on whether we had parent info 622# or not 623if($parentrefname) { 624$input_params{'action'} ||="blobdiff_plain"; 625}else{ 626$input_params{'action'} ||="blob_plain"; 627} 628} 629$input_params{'hash_base'} ||=$refname; 630$input_params{'file_name'} ||=$pathname; 631}elsif(defined$refname) { 632# we got "branch". In this case we have to choose if we have to 633# set hash or hash_base. 634# 635# Most of the actions without a pathname only want hash to be 636# set, except for the ones specified in @wants_base that want 637# hash_base instead. It should also be noted that hand-crafted 638# links having 'history' as an action and no pathname or hash 639# set will fail, but that happens regardless of PATH_INFO. 640$input_params{'action'} ||="shortlog"; 641if(grep{$_eq$input_params{'action'} }@wants_base) { 642$input_params{'hash_base'} ||=$refname; 643}else{ 644$input_params{'hash'} ||=$refname; 645} 646} 647 648# next, handle the 'parent' part, if present 649if(defined$parentrefname) { 650# a missing pathspec defaults to the 'current' filename, allowing e.g. 651# someproject/blobdiff/oldrev..newrev:/filename 652if($parentpathname) { 653$parentpathname=~ s,^/+,,; 654$parentpathname=~ s,/$,,; 655$input_params{'file_parent'} ||=$parentpathname; 656}else{ 657$input_params{'file_parent'} ||=$input_params{'file_name'}; 658} 659# we assume that hash_parent_base is wanted if a path was specified, 660# or if the action wants hash_base instead of hash 661if(defined$input_params{'file_parent'} || 662grep{$_eq$input_params{'action'} }@wants_base) { 663$input_params{'hash_parent_base'} ||=$parentrefname; 664}else{ 665$input_params{'hash_parent'} ||=$parentrefname; 666} 667} 668 669# for the snapshot action, we allow URLs in the form 670# $project/snapshot/$hash.ext 671# where .ext determines the snapshot and gets removed from the 672# passed $refname to provide the $hash. 673# 674# To be able to tell that $refname includes the format extension, we 675# require the following two conditions to be satisfied: 676# - the hash input parameter MUST have been set from the $refname part 677# of the URL (i.e. they must be equal) 678# - the snapshot format MUST NOT have been defined already (e.g. from 679# CGI parameter sf) 680# It's also useless to try any matching unless $refname has a dot, 681# so we check for that too 682if(defined$input_params{'action'} && 683$input_params{'action'}eq'snapshot'&& 684defined$refname&&index($refname,'.') != -1&& 685$refnameeq$input_params{'hash'} && 686!defined$input_params{'snapshot_format'}) { 687# We loop over the known snapshot formats, checking for 688# extensions. Allowed extensions are both the defined suffix 689# (which includes the initial dot already) and the snapshot 690# format key itself, with a prepended dot 691while(my($fmt,%opt) =each%known_snapshot_formats) { 692my$hash=$refname; 693my$sfx; 694$hash=~s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//; 695next unless$sfx=$1; 696# a valid suffix was found, so set the snapshot format 697# and reset the hash parameter 698$input_params{'snapshot_format'} =$fmt; 699$input_params{'hash'} =$hash; 700# we also set the format suffix to the one requested 701# in the URL: this way a request for e.g. .tgz returns 702# a .tgz instead of a .tar.gz 703$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 704last; 705} 706} 707} 708evaluate_path_info(); 709 710our$action=$input_params{'action'}; 711if(defined$action) { 712if(!validate_action($action)) { 713 die_error(400,"Invalid action parameter"); 714} 715} 716 717# parameters which are pathnames 718our$project=$input_params{'project'}; 719if(defined$project) { 720if(!validate_project($project)) { 721undef$project; 722 die_error(404,"No such project"); 723} 724} 725 726our$file_name=$input_params{'file_name'}; 727if(defined$file_name) { 728if(!validate_pathname($file_name)) { 729 die_error(400,"Invalid file parameter"); 730} 731} 732 733our$file_parent=$input_params{'file_parent'}; 734if(defined$file_parent) { 735if(!validate_pathname($file_parent)) { 736 die_error(400,"Invalid file parent parameter"); 737} 738} 739 740# parameters which are refnames 741our$hash=$input_params{'hash'}; 742if(defined$hash) { 743if(!validate_refname($hash)) { 744 die_error(400,"Invalid hash parameter"); 745} 746} 747 748our$hash_parent=$input_params{'hash_parent'}; 749if(defined$hash_parent) { 750if(!validate_refname($hash_parent)) { 751 die_error(400,"Invalid hash parent parameter"); 752} 753} 754 755our$hash_base=$input_params{'hash_base'}; 756if(defined$hash_base) { 757if(!validate_refname($hash_base)) { 758 die_error(400,"Invalid hash base parameter"); 759} 760} 761 762our@extra_options= @{$input_params{'extra_options'}}; 763# @extra_options is always defined, since it can only be (currently) set from 764# CGI, and $cgi->param() returns the empty array in array context if the param 765# is not set 766foreachmy$opt(@extra_options) { 767if(not exists$allowed_options{$opt}) { 768 die_error(400,"Invalid option parameter"); 769} 770if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 771 die_error(400,"Invalid option parameter for this action"); 772} 773} 774 775our$hash_parent_base=$input_params{'hash_parent_base'}; 776if(defined$hash_parent_base) { 777if(!validate_refname($hash_parent_base)) { 778 die_error(400,"Invalid hash parent base parameter"); 779} 780} 781 782# other parameters 783our$page=$input_params{'page'}; 784if(defined$page) { 785if($page=~m/[^0-9]/) { 786 die_error(400,"Invalid page parameter"); 787} 788} 789 790our$searchtype=$input_params{'searchtype'}; 791if(defined$searchtype) { 792if($searchtype=~m/[^a-z]/) { 793 die_error(400,"Invalid searchtype parameter"); 794} 795} 796 797our$search_use_regexp=$input_params{'search_use_regexp'}; 798 799our$searchtext=$input_params{'searchtext'}; 800our$search_regexp; 801if(defined$searchtext) { 802if(length($searchtext) <2) { 803 die_error(403,"At least two characters are required for search parameter"); 804} 805$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 806} 807 808# path to the current git repository 809our$git_dir; 810$git_dir="$projectroot/$project"if$project; 811 812# list of supported snapshot formats 813our@snapshot_fmts= gitweb_get_feature('snapshot'); 814@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 815 816# dispatch 817if(!defined$action) { 818if(defined$hash) { 819$action= git_get_type($hash); 820}elsif(defined$hash_base&&defined$file_name) { 821$action= git_get_type("$hash_base:$file_name"); 822}elsif(defined$project) { 823$action='summary'; 824}else{ 825$action='project_list'; 826} 827} 828if(!defined($actions{$action})) { 829 die_error(400,"Unknown action"); 830} 831if($action!~m/^(opml|project_list|project_index)$/&& 832!$project) { 833 die_error(400,"Project needed"); 834} 835$actions{$action}->(); 836exit; 837 838## ====================================================================== 839## action links 840 841sub href (%) { 842my%params=@_; 843# default is to use -absolute url() i.e. $my_uri 844my$href=$params{-full} ?$my_url:$my_uri; 845 846$params{'project'} =$projectunlessexists$params{'project'}; 847 848if($params{-replay}) { 849while(my($name,$symbol) =each%cgi_param_mapping) { 850if(!exists$params{$name}) { 851$params{$name} =$input_params{$name}; 852} 853} 854} 855 856my$use_pathinfo= gitweb_check_feature('pathinfo'); 857if($use_pathinfoand defined$params{'project'}) { 858# try to put as many parameters as possible in PATH_INFO: 859# - project name 860# - action 861# - hash_parent or hash_parent_base:/file_parent 862# - hash or hash_base:/filename 863# - the snapshot_format as an appropriate suffix 864 865# When the script is the root DirectoryIndex for the domain, 866# $href here would be something like http://gitweb.example.com/ 867# Thus, we strip any trailing / from $href, to spare us double 868# slashes in the final URL 869$href=~ s,/$,,; 870 871# Then add the project name, if present 872$href.="/".esc_url($params{'project'}); 873delete$params{'project'}; 874 875# since we destructively absorb parameters, we keep this 876# boolean that remembers if we're handling a snapshot 877my$is_snapshot=$params{'action'}eq'snapshot'; 878 879# Summary just uses the project path URL, any other action is 880# added to the URL 881if(defined$params{'action'}) { 882$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 883delete$params{'action'}; 884} 885 886# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 887# stripping nonexistent or useless pieces 888$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 889||$params{'hash_parent'} ||$params{'hash'}); 890if(defined$params{'hash_base'}) { 891if(defined$params{'hash_parent_base'}) { 892$href.= esc_url($params{'hash_parent_base'}); 893# skip the file_parent if it's the same as the file_name 894delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 895if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 896$href.=":/".esc_url($params{'file_parent'}); 897delete$params{'file_parent'}; 898} 899$href.=".."; 900delete$params{'hash_parent'}; 901delete$params{'hash_parent_base'}; 902}elsif(defined$params{'hash_parent'}) { 903$href.= esc_url($params{'hash_parent'}).".."; 904delete$params{'hash_parent'}; 905} 906 907$href.= esc_url($params{'hash_base'}); 908if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 909$href.=":/".esc_url($params{'file_name'}); 910delete$params{'file_name'}; 911} 912delete$params{'hash'}; 913delete$params{'hash_base'}; 914}elsif(defined$params{'hash'}) { 915$href.= esc_url($params{'hash'}); 916delete$params{'hash'}; 917} 918 919# If the action was a snapshot, we can absorb the 920# snapshot_format parameter too 921if($is_snapshot) { 922my$fmt=$params{'snapshot_format'}; 923# snapshot_format should always be defined when href() 924# is called, but just in case some code forgets, we 925# fall back to the default 926$fmt||=$snapshot_fmts[0]; 927$href.=$known_snapshot_formats{$fmt}{'suffix'}; 928delete$params{'snapshot_format'}; 929} 930} 931 932# now encode the parameters explicitly 933my@result= (); 934for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 935my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 936if(defined$params{$name}) { 937if(ref($params{$name})eq"ARRAY") { 938foreachmy$par(@{$params{$name}}) { 939push@result,$symbol."=". esc_param($par); 940} 941}else{ 942push@result,$symbol."=". esc_param($params{$name}); 943} 944} 945} 946$href.="?".join(';',@result)ifscalar@result; 947 948return$href; 949} 950 951 952## ====================================================================== 953## validation, quoting/unquoting and escaping 954 955sub validate_action { 956my$input=shift||returnundef; 957returnundefunlessexists$actions{$input}; 958return$input; 959} 960 961sub validate_project { 962my$input=shift||returnundef; 963if(!validate_pathname($input) || 964!(-d "$projectroot/$input") || 965!check_export_ok("$projectroot/$input") || 966($strict_export&& !project_in_list($input))) { 967returnundef; 968}else{ 969return$input; 970} 971} 972 973sub validate_pathname { 974my$input=shift||returnundef; 975 976# no '.' or '..' as elements of path, i.e. no '.' nor '..' 977# at the beginning, at the end, and between slashes. 978# also this catches doubled slashes 979if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 980returnundef; 981} 982# no null characters 983if($input=~m!\0!) { 984returnundef; 985} 986return$input; 987} 988 989sub validate_refname { 990my$input=shift||returnundef; 991 992# textual hashes are O.K. 993if($input=~m/^[0-9a-fA-F]{40}$/) { 994return$input; 995} 996# it must be correct pathname 997$input= validate_pathname($input) 998orreturnundef; 999# restrictions on ref name according to git-check-ref-format1000if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1001returnundef;1002}1003return$input;1004}10051006# decode sequences of octets in utf8 into Perl's internal form,1007# which is utf-8 with utf8 flag set if needed. gitweb writes out1008# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1009sub to_utf8 {1010my$str=shift;1011if(utf8::valid($str)) {1012 utf8::decode($str);1013return$str;1014}else{1015return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1016}1017}10181019# quote unsafe chars, but keep the slash, even when it's not1020# correct, but quoted slashes look too horrible in bookmarks1021sub esc_param {1022my$str=shift;1023$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1024$str=~s/\+/%2B/g;1025$str=~s/ /\+/g;1026return$str;1027}10281029# quote unsafe chars in whole URL, so some charactrs cannot be quoted1030sub esc_url {1031my$str=shift;1032$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1033$str=~s/\+/%2B/g;1034$str=~s/ /\+/g;1035return$str;1036}10371038# replace invalid utf8 character with SUBSTITUTION sequence1039sub esc_html ($;%) {1040my$str=shift;1041my%opts=@_;10421043$str= to_utf8($str);1044$str=$cgi->escapeHTML($str);1045if($opts{'-nbsp'}) {1046$str=~s/ / /g;1047}1048$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1049return$str;1050}10511052# quote control characters and escape filename to HTML1053sub esc_path {1054my$str=shift;1055my%opts=@_;10561057$str= to_utf8($str);1058$str=$cgi->escapeHTML($str);1059if($opts{'-nbsp'}) {1060$str=~s/ / /g;1061}1062$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1063return$str;1064}10651066# Make control characters "printable", using character escape codes (CEC)1067sub quot_cec {1068my$cntrl=shift;1069my%opts=@_;1070my%es= (# character escape codes, aka escape sequences1071"\t"=>'\t',# tab (HT)1072"\n"=>'\n',# line feed (LF)1073"\r"=>'\r',# carrige return (CR)1074"\f"=>'\f',# form feed (FF)1075"\b"=>'\b',# backspace (BS)1076"\a"=>'\a',# alarm (bell) (BEL)1077"\e"=>'\e',# escape (ESC)1078"\013"=>'\v',# vertical tab (VT)1079"\000"=>'\0',# nul character (NUL)1080);1081my$chr= ( (exists$es{$cntrl})1082?$es{$cntrl}1083:sprintf('\%2x',ord($cntrl)) );1084if($opts{-nohtml}) {1085return$chr;1086}else{1087return"<span class=\"cntrl\">$chr</span>";1088}1089}10901091# Alternatively use unicode control pictures codepoints,1092# Unicode "printable representation" (PR)1093sub quot_upr {1094my$cntrl=shift;1095my%opts=@_;10961097my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1098if($opts{-nohtml}) {1099return$chr;1100}else{1101return"<span class=\"cntrl\">$chr</span>";1102}1103}11041105# git may return quoted and escaped filenames1106sub unquote {1107my$str=shift;11081109sub unq {1110my$seq=shift;1111my%es= (# character escape codes, aka escape sequences1112't'=>"\t",# tab (HT, TAB)1113'n'=>"\n",# newline (NL)1114'r'=>"\r",# return (CR)1115'f'=>"\f",# form feed (FF)1116'b'=>"\b",# backspace (BS)1117'a'=>"\a",# alarm (bell) (BEL)1118'e'=>"\e",# escape (ESC)1119'v'=>"\013",# vertical tab (VT)1120);11211122if($seq=~m/^[0-7]{1,3}$/) {1123# octal char sequence1124returnchr(oct($seq));1125}elsif(exists$es{$seq}) {1126# C escape sequence, aka character escape code1127return$es{$seq};1128}1129# quoted ordinary character1130return$seq;1131}11321133if($str=~m/^"(.*)"$/) {1134# needs unquoting1135$str=$1;1136$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1137}1138return$str;1139}11401141# escape tabs (convert tabs to spaces)1142sub untabify {1143my$line=shift;11441145while((my$pos=index($line,"\t")) != -1) {1146if(my$count= (8- ($pos%8))) {1147my$spaces=' ' x $count;1148$line=~s/\t/$spaces/;1149}1150}11511152return$line;1153}11541155sub project_in_list {1156my$project=shift;1157my@list= git_get_projects_list();1158return@list&&scalar(grep{$_->{'path'}eq$project}@list);1159}11601161## ----------------------------------------------------------------------1162## HTML aware string manipulation11631164# Try to chop given string on a word boundary between position1165# $len and $len+$add_len. If there is no word boundary there,1166# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1167# (marking chopped part) would be longer than given string.1168sub chop_str {1169my$str=shift;1170my$len=shift;1171my$add_len=shift||10;1172my$where=shift||'right';# 'left' | 'center' | 'right'11731174# Make sure perl knows it is utf8 encoded so we don't1175# cut in the middle of a utf8 multibyte char.1176$str= to_utf8($str);11771178# allow only $len chars, but don't cut a word if it would fit in $add_len1179# if it doesn't fit, cut it if it's still longer than the dots we would add1180# remove chopped character entities entirely11811182# when chopping in the middle, distribute $len into left and right part1183# return early if chopping wouldn't make string shorter1184if($whereeq'center') {1185return$strif($len+5>=length($str));# filler is length 51186$len=int($len/2);1187}else{1188return$strif($len+4>=length($str));# filler is length 41189}11901191# regexps: ending and beginning with word part up to $add_len1192my$endre=qr/.{$len}\w{0,$add_len}/;1193my$begre=qr/\w{0,$add_len}.{$len}/;11941195if($whereeq'left') {1196$str=~m/^(.*?)($begre)$/;1197my($lead,$body) = ($1,$2);1198if(length($lead) >4) {1199$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1200$lead=" ...";1201}1202return"$lead$body";12031204}elsif($whereeq'center') {1205$str=~m/^($endre)(.*)$/;1206my($left,$str) = ($1,$2);1207$str=~m/^(.*?)($begre)$/;1208my($mid,$right) = ($1,$2);1209if(length($mid) >5) {1210$left=~s/&[^;]*$//;1211$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1212$mid=" ... ";1213}1214return"$left$mid$right";12151216}else{1217$str=~m/^($endre)(.*)$/;1218my$body=$1;1219my$tail=$2;1220if(length($tail) >4) {1221$body=~s/&[^;]*$//;1222$tail="... ";1223}1224return"$body$tail";1225}1226}12271228# takes the same arguments as chop_str, but also wraps a <span> around the1229# result with a title attribute if it does get chopped. Additionally, the1230# string is HTML-escaped.1231sub chop_and_escape_str {1232my($str) =@_;12331234my$chopped= chop_str(@_);1235if($choppedeq$str) {1236return esc_html($chopped);1237}else{1238$str=~s/([[:cntrl:]])/?/g;1239return$cgi->span({-title=>$str}, esc_html($chopped));1240}1241}12421243## ----------------------------------------------------------------------1244## functions returning short strings12451246# CSS class for given age value (in seconds)1247sub age_class {1248my$age=shift;12491250if(!defined$age) {1251return"noage";1252}elsif($age<60*60*2) {1253return"age0";1254}elsif($age<60*60*24*2) {1255return"age1";1256}else{1257return"age2";1258}1259}12601261# convert age in seconds to "nn units ago" string1262sub age_string {1263my$age=shift;1264my$age_str;12651266if($age>60*60*24*365*2) {1267$age_str= (int$age/60/60/24/365);1268$age_str.=" years ago";1269}elsif($age>60*60*24*(365/12)*2) {1270$age_str=int$age/60/60/24/(365/12);1271$age_str.=" months ago";1272}elsif($age>60*60*24*7*2) {1273$age_str=int$age/60/60/24/7;1274$age_str.=" weeks ago";1275}elsif($age>60*60*24*2) {1276$age_str=int$age/60/60/24;1277$age_str.=" days ago";1278}elsif($age>60*60*2) {1279$age_str=int$age/60/60;1280$age_str.=" hours ago";1281}elsif($age>60*2) {1282$age_str=int$age/60;1283$age_str.=" min ago";1284}elsif($age>2) {1285$age_str=int$age;1286$age_str.=" sec ago";1287}else{1288$age_str.=" right now";1289}1290return$age_str;1291}12921293useconstant{1294 S_IFINVALID =>0030000,1295 S_IFGITLINK =>0160000,1296};12971298# submodule/subproject, a commit object reference1299sub S_ISGITLINK($) {1300my$mode=shift;13011302return(($mode& S_IFMT) == S_IFGITLINK)1303}13041305# convert file mode in octal to symbolic file mode string1306sub mode_str {1307my$mode=oct shift;13081309if(S_ISGITLINK($mode)) {1310return'm---------';1311}elsif(S_ISDIR($mode& S_IFMT)) {1312return'drwxr-xr-x';1313}elsif(S_ISLNK($mode)) {1314return'lrwxrwxrwx';1315}elsif(S_ISREG($mode)) {1316# git cares only about the executable bit1317if($mode& S_IXUSR) {1318return'-rwxr-xr-x';1319}else{1320return'-rw-r--r--';1321};1322}else{1323return'----------';1324}1325}13261327# convert file mode in octal to file type string1328sub file_type {1329my$mode=shift;13301331if($mode!~m/^[0-7]+$/) {1332return$mode;1333}else{1334$mode=oct$mode;1335}13361337if(S_ISGITLINK($mode)) {1338return"submodule";1339}elsif(S_ISDIR($mode& S_IFMT)) {1340return"directory";1341}elsif(S_ISLNK($mode)) {1342return"symlink";1343}elsif(S_ISREG($mode)) {1344return"file";1345}else{1346return"unknown";1347}1348}13491350# convert file mode in octal to file type description string1351sub file_type_long {1352my$mode=shift;13531354if($mode!~m/^[0-7]+$/) {1355return$mode;1356}else{1357$mode=oct$mode;1358}13591360if(S_ISGITLINK($mode)) {1361return"submodule";1362}elsif(S_ISDIR($mode& S_IFMT)) {1363return"directory";1364}elsif(S_ISLNK($mode)) {1365return"symlink";1366}elsif(S_ISREG($mode)) {1367if($mode& S_IXUSR) {1368return"executable";1369}else{1370return"file";1371};1372}else{1373return"unknown";1374}1375}137613771378## ----------------------------------------------------------------------1379## functions returning short HTML fragments, or transforming HTML fragments1380## which don't belong to other sections13811382# format line of commit message.1383sub format_log_line_html {1384my$line=shift;13851386$line= esc_html($line, -nbsp=>1);1387if($line=~m/\b([0-9a-fA-F]{8,40})\b/) {1388my$hash_text=$1;1389my$link=1390$cgi->a({-href => href(action=>"object", hash=>$hash_text),1391-class=>"text"},$hash_text);1392$line=~s/$hash_text/$link/;1393}1394return$line;1395}13961397# format marker of refs pointing to given object13981399# the destination action is chosen based on object type and current context:1400# - for annotated tags, we choose the tag view unless it's the current view1401# already, in which case we go to shortlog view1402# - for other refs, we keep the current view if we're in history, shortlog or1403# log view, and select shortlog otherwise1404sub format_ref_marker {1405my($refs,$id) =@_;1406my$markers='';14071408if(defined$refs->{$id}) {1409foreachmy$ref(@{$refs->{$id}}) {1410# this code exploits the fact that non-lightweight tags are the1411# only indirect objects, and that they are the only objects for which1412# we want to use tag instead of shortlog as action1413my($type,$name) =qw();1414my$indirect= ($ref=~s/\^\{\}$//);1415# e.g. tags/v2.6.11 or heads/next1416if($ref=~m!^(.*?)s?/(.*)$!) {1417$type=$1;1418$name=$2;1419}else{1420$type="ref";1421$name=$ref;1422}14231424my$class=$type;1425$class.=" indirect"if$indirect;14261427my$dest_action="shortlog";14281429if($indirect) {1430$dest_action="tag"unless$actioneq"tag";1431}elsif($action=~/^(history|(short)?log)$/) {1432$dest_action=$action;1433}14341435my$dest="";1436$dest.="refs/"unless$ref=~ m!^refs/!;1437$dest.=$ref;14381439my$link=$cgi->a({1440-href => href(1441 action=>$dest_action,1442 hash=>$dest1443)},$name);14441445$markers.=" <span class=\"$class\"title=\"$ref\">".1446$link."</span>";1447}1448}14491450if($markers) {1451return' <span class="refs">'.$markers.'</span>';1452}else{1453return"";1454}1455}14561457# format, perhaps shortened and with markers, title line1458sub format_subject_html {1459my($long,$short,$href,$extra) =@_;1460$extra=''unlessdefined($extra);14611462if(length($short) <length($long)) {1463return$cgi->a({-href =>$href, -class=>"list subject",1464-title => to_utf8($long)},1465 esc_html($short) .$extra);1466}else{1467return$cgi->a({-href =>$href, -class=>"list subject"},1468 esc_html($long) .$extra);1469}1470}14711472# format git diff header line, i.e. "diff --(git|combined|cc) ..."1473sub format_git_diff_header_line {1474my$line=shift;1475my$diffinfo=shift;1476my($from,$to) =@_;14771478if($diffinfo->{'nparents'}) {1479# combined diff1480$line=~s!^(diff (.*?) )"?.*$!$1!;1481if($to->{'href'}) {1482$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1483 esc_path($to->{'file'}));1484}else{# file was deleted (no href)1485$line.= esc_path($to->{'file'});1486}1487}else{1488# "ordinary" diff1489$line=~s!^(diff (.*?) )"?a/.*$!$1!;1490if($from->{'href'}) {1491$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1492'a/'. esc_path($from->{'file'}));1493}else{# file was added (no href)1494$line.='a/'. esc_path($from->{'file'});1495}1496$line.=' ';1497if($to->{'href'}) {1498$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1499'b/'. esc_path($to->{'file'}));1500}else{# file was deleted1501$line.='b/'. esc_path($to->{'file'});1502}1503}15041505return"<div class=\"diff header\">$line</div>\n";1506}15071508# format extended diff header line, before patch itself1509sub format_extended_diff_header_line {1510my$line=shift;1511my$diffinfo=shift;1512my($from,$to) =@_;15131514# match <path>1515if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1516$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1517 esc_path($from->{'file'}));1518}1519if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1520$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1521 esc_path($to->{'file'}));1522}1523# match single <mode>1524if($line=~m/\s(\d{6})$/) {1525$line.='<span class="info"> ('.1526 file_type_long($1) .1527')</span>';1528}1529# match <hash>1530if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1531# can match only for combined diff1532$line='index ';1533for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1534if($from->{'href'}[$i]) {1535$line.=$cgi->a({-href=>$from->{'href'}[$i],1536-class=>"hash"},1537substr($diffinfo->{'from_id'}[$i],0,7));1538}else{1539$line.='0' x 7;1540}1541# separator1542$line.=','if($i<$diffinfo->{'nparents'} -1);1543}1544$line.='..';1545if($to->{'href'}) {1546$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1547substr($diffinfo->{'to_id'},0,7));1548}else{1549$line.='0' x 7;1550}15511552}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1553# can match only for ordinary diff1554my($from_link,$to_link);1555if($from->{'href'}) {1556$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1557substr($diffinfo->{'from_id'},0,7));1558}else{1559$from_link='0' x 7;1560}1561if($to->{'href'}) {1562$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1563substr($diffinfo->{'to_id'},0,7));1564}else{1565$to_link='0' x 7;1566}1567my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1568$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1569}15701571return$line."<br/>\n";1572}15731574# format from-file/to-file diff header1575sub format_diff_from_to_header {1576my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1577my$line;1578my$result='';15791580$line=$from_line;1581#assert($line =~ m/^---/) if DEBUG;1582# no extra formatting for "^--- /dev/null"1583if(!$diffinfo->{'nparents'}) {1584# ordinary (single parent) diff1585if($line=~m!^--- "?a/!) {1586if($from->{'href'}) {1587$line='--- a/'.1588$cgi->a({-href=>$from->{'href'}, -class=>"path"},1589 esc_path($from->{'file'}));1590}else{1591$line='--- a/'.1592 esc_path($from->{'file'});1593}1594}1595$result.= qq!<div class="diff from_file">$line</div>\n!;15961597}else{1598# combined diff (merge commit)1599for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1600if($from->{'href'}[$i]) {1601$line='--- '.1602$cgi->a({-href=>href(action=>"blobdiff",1603 hash_parent=>$diffinfo->{'from_id'}[$i],1604 hash_parent_base=>$parents[$i],1605 file_parent=>$from->{'file'}[$i],1606 hash=>$diffinfo->{'to_id'},1607 hash_base=>$hash,1608 file_name=>$to->{'file'}),1609-class=>"path",1610-title=>"diff". ($i+1)},1611$i+1) .1612'/'.1613$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1614 esc_path($from->{'file'}[$i]));1615}else{1616$line='--- /dev/null';1617}1618$result.= qq!<div class="diff from_file">$line</div>\n!;1619}1620}16211622$line=$to_line;1623#assert($line =~ m/^\+\+\+/) if DEBUG;1624# no extra formatting for "^+++ /dev/null"1625if($line=~m!^\+\+\+ "?b/!) {1626if($to->{'href'}) {1627$line='+++ b/'.1628$cgi->a({-href=>$to->{'href'}, -class=>"path"},1629 esc_path($to->{'file'}));1630}else{1631$line='+++ b/'.1632 esc_path($to->{'file'});1633}1634}1635$result.= qq!<div class="diff to_file">$line</div>\n!;16361637return$result;1638}16391640# create note for patch simplified by combined diff1641sub format_diff_cc_simplified {1642my($diffinfo,@parents) =@_;1643my$result='';16441645$result.="<div class=\"diff header\">".1646"diff --cc ";1647if(!is_deleted($diffinfo)) {1648$result.=$cgi->a({-href => href(action=>"blob",1649 hash_base=>$hash,1650 hash=>$diffinfo->{'to_id'},1651 file_name=>$diffinfo->{'to_file'}),1652-class=>"path"},1653 esc_path($diffinfo->{'to_file'}));1654}else{1655$result.= esc_path($diffinfo->{'to_file'});1656}1657$result.="</div>\n".# class="diff header"1658"<div class=\"diff nodifferences\">".1659"Simple merge".1660"</div>\n";# class="diff nodifferences"16611662return$result;1663}16641665# format patch (diff) line (not to be used for diff headers)1666sub format_diff_line {1667my$line=shift;1668my($from,$to) =@_;1669my$diff_class="";16701671chomp$line;16721673if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1674# combined diff1675my$prefix=substr($line,0,scalar@{$from->{'href'}});1676if($line=~m/^\@{3}/) {1677$diff_class=" chunk_header";1678}elsif($line=~m/^\\/) {1679$diff_class=" incomplete";1680}elsif($prefix=~tr/+/+/) {1681$diff_class=" add";1682}elsif($prefix=~tr/-/-/) {1683$diff_class=" rem";1684}1685}else{1686# assume ordinary diff1687my$char=substr($line,0,1);1688if($chareq'+') {1689$diff_class=" add";1690}elsif($chareq'-') {1691$diff_class=" rem";1692}elsif($chareq'@') {1693$diff_class=" chunk_header";1694}elsif($chareq"\\") {1695$diff_class=" incomplete";1696}1697}1698$line= untabify($line);1699if($from&&$to&&$line=~m/^\@{2} /) {1700my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1701$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;17021703$from_lines=0unlessdefined$from_lines;1704$to_lines=0unlessdefined$to_lines;17051706if($from->{'href'}) {1707$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1708-class=>"list"},$from_text);1709}1710if($to->{'href'}) {1711$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1712-class=>"list"},$to_text);1713}1714$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1715"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1716return"<div class=\"diff$diff_class\">$line</div>\n";1717}elsif($from&&$to&&$line=~m/^\@{3}/) {1718my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1719my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);17201721@from_text=split(' ',$ranges);1722for(my$i=0;$i<@from_text; ++$i) {1723($from_start[$i],$from_nlines[$i]) =1724(split(',',substr($from_text[$i],1)),0);1725}17261727$to_text=pop@from_text;1728$to_start=pop@from_start;1729$to_nlines=pop@from_nlines;17301731$line="<span class=\"chunk_info\">$prefix";1732for(my$i=0;$i<@from_text; ++$i) {1733if($from->{'href'}[$i]) {1734$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1735-class=>"list"},$from_text[$i]);1736}else{1737$line.=$from_text[$i];1738}1739$line.=" ";1740}1741if($to->{'href'}) {1742$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1743-class=>"list"},$to_text);1744}else{1745$line.=$to_text;1746}1747$line.="$prefix</span>".1748"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1749return"<div class=\"diff$diff_class\">$line</div>\n";1750}1751return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1752}17531754# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1755# linked. Pass the hash of the tree/commit to snapshot.1756sub format_snapshot_links {1757my($hash) =@_;1758my$num_fmts=@snapshot_fmts;1759if($num_fmts>1) {1760# A parenthesized list of links bearing format names.1761# e.g. "snapshot (_tar.gz_ _zip_)"1762return"snapshot (".join(' ',map1763$cgi->a({1764-href => href(1765 action=>"snapshot",1766 hash=>$hash,1767 snapshot_format=>$_1768)1769},$known_snapshot_formats{$_}{'display'})1770,@snapshot_fmts) .")";1771}elsif($num_fmts==1) {1772# A single "snapshot" link whose tooltip bears the format name.1773# i.e. "_snapshot_"1774my($fmt) =@snapshot_fmts;1775return1776$cgi->a({1777-href => href(1778 action=>"snapshot",1779 hash=>$hash,1780 snapshot_format=>$fmt1781),1782-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1783},"snapshot");1784}else{# $num_fmts == 01785returnundef;1786}1787}17881789## ......................................................................1790## functions returning values to be passed, perhaps after some1791## transformation, to other functions; e.g. returning arguments to href()17921793# returns hash to be passed to href to generate gitweb URL1794# in -title key it returns description of link1795sub get_feed_info {1796my$format=shift||'Atom';1797my%res= (action =>lc($format));17981799# feed links are possible only for project views1800return unless(defined$project);1801# some views should link to OPML, or to generic project feed,1802# or don't have specific feed yet (so they should use generic)1803return if($action=~/^(?:tags|heads|forks|tag|search)$/x);18041805my$branch;1806# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1807# from tag links; this also makes possible to detect branch links1808if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1809(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1810$branch=$1;1811}1812# find log type for feed description (title)1813my$type='log';1814if(defined$file_name) {1815$type="history of$file_name";1816$type.="/"if($actioneq'tree');1817$type.=" on '$branch'"if(defined$branch);1818}else{1819$type="log of$branch"if(defined$branch);1820}18211822$res{-title} =$type;1823$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1824$res{'file_name'} =$file_name;18251826return%res;1827}18281829## ----------------------------------------------------------------------1830## git utility subroutines, invoking git commands18311832# returns path to the core git executable and the --git-dir parameter as list1833sub git_cmd {1834return$GIT,'--git-dir='.$git_dir;1835}18361837# quote the given arguments for passing them to the shell1838# quote_command("command", "arg 1", "arg with ' and ! characters")1839# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1840# Try to avoid using this function wherever possible.1841sub quote_command {1842returnjoin(' ',1843map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1844}18451846# get HEAD ref of given project as hash1847sub git_get_head_hash {1848my$project=shift;1849my$o_git_dir=$git_dir;1850my$retval=undef;1851$git_dir="$projectroot/$project";1852if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1853my$head= <$fd>;1854close$fd;1855if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1856$retval=$1;1857}1858}1859if(defined$o_git_dir) {1860$git_dir=$o_git_dir;1861}1862return$retval;1863}18641865# get type of given object1866sub git_get_type {1867my$hash=shift;18681869open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1870my$type= <$fd>;1871close$fdorreturn;1872chomp$type;1873return$type;1874}18751876# repository configuration1877our$config_file='';1878our%config;18791880# store multiple values for single key as anonymous array reference1881# single values stored directly in the hash, not as [ <value> ]1882sub hash_set_multi {1883my($hash,$key,$value) =@_;18841885if(!exists$hash->{$key}) {1886$hash->{$key} =$value;1887}elsif(!ref$hash->{$key}) {1888$hash->{$key} = [$hash->{$key},$value];1889}else{1890push@{$hash->{$key}},$value;1891}1892}18931894# return hash of git project configuration1895# optionally limited to some section, e.g. 'gitweb'1896sub git_parse_project_config {1897my$section_regexp=shift;1898my%config;18991900local$/="\0";19011902open my$fh,"-|", git_cmd(),"config",'-z','-l',1903orreturn;19041905while(my$keyval= <$fh>) {1906chomp$keyval;1907my($key,$value) =split(/\n/,$keyval,2);19081909 hash_set_multi(\%config,$key,$value)1910if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1911}1912close$fh;19131914return%config;1915}19161917# convert config value to boolean: 'true' or 'false'1918# no value, number > 0, 'true' and 'yes' values are true1919# rest of values are treated as false (never as error)1920sub config_to_bool {1921my$val=shift;19221923return1if!defined$val;# section.key19241925# strip leading and trailing whitespace1926$val=~s/^\s+//;1927$val=~s/\s+$//;19281929return(($val=~/^\d+$/&&$val) ||# section.key = 11930($val=~/^(?:true|yes)$/i));# section.key = true1931}19321933# convert config value to simple decimal number1934# an optional value suffix of 'k', 'm', or 'g' will cause the value1935# to be multiplied by 1024, 1048576, or 10737418241936sub config_to_int {1937my$val=shift;19381939# strip leading and trailing whitespace1940$val=~s/^\s+//;1941$val=~s/\s+$//;19421943if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1944$unit=lc($unit);1945# unknown unit is treated as 11946return$num* ($uniteq'g'?1073741824:1947$uniteq'm'?1048576:1948$uniteq'k'?1024:1);1949}1950return$val;1951}19521953# convert config value to array reference, if needed1954sub config_to_multi {1955my$val=shift;19561957returnref($val) ?$val: (defined($val) ? [$val] : []);1958}19591960sub git_get_project_config {1961my($key,$type) =@_;19621963# key sanity check1964return unless($key);1965$key=~s/^gitweb\.//;1966return if($key=~m/\W/);19671968# type sanity check1969if(defined$type) {1970$type=~s/^--//;1971$type=undef1972unless($typeeq'bool'||$typeeq'int');1973}19741975# get config1976if(!defined$config_file||1977$config_filene"$git_dir/config") {1978%config= git_parse_project_config('gitweb');1979$config_file="$git_dir/config";1980}19811982# check if config variable (key) exists1983return unlessexists$config{"gitweb.$key"};19841985# ensure given type1986if(!defined$type) {1987return$config{"gitweb.$key"};1988}elsif($typeeq'bool') {1989# backward compatibility: 'git config --bool' returns true/false1990return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1991}elsif($typeeq'int') {1992return config_to_int($config{"gitweb.$key"});1993}1994return$config{"gitweb.$key"};1995}19961997# get hash of given path at given ref1998sub git_get_hash_by_path {1999my$base=shift;2000my$path=shift||returnundef;2001my$type=shift;20022003$path=~ s,/+$,,;20042005open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2006or die_error(500,"Open git-ls-tree failed");2007my$line= <$fd>;2008close$fdorreturnundef;20092010if(!defined$line) {2011# there is no tree or hash given by $path at $base2012returnundef;2013}20142015#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2016$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2017if(defined$type&&$typene$2) {2018# type doesn't match2019returnundef;2020}2021return$3;2022}20232024# get path of entry with given hash at given tree-ish (ref)2025# used to get 'from' filename for combined diff (merge commit) for renames2026sub git_get_path_by_hash {2027my$base=shift||return;2028my$hash=shift||return;20292030local$/="\0";20312032open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2033orreturnundef;2034while(my$line= <$fd>) {2035chomp$line;20362037#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2038#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2039if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2040close$fd;2041return$1;2042}2043}2044close$fd;2045returnundef;2046}20472048## ......................................................................2049## git utility functions, directly accessing git repository20502051sub git_get_project_description {2052my$path=shift;20532054$git_dir="$projectroot/$path";2055open my$fd,"$git_dir/description"2056orreturn git_get_project_config('description');2057my$descr= <$fd>;2058close$fd;2059if(defined$descr) {2060chomp$descr;2061}2062return$descr;2063}20642065sub git_get_project_ctags {2066my$path=shift;2067my$ctags= {};20682069$git_dir="$projectroot/$path";2070unless(opendir D,"$git_dir/ctags") {2071return$ctags;2072}2073foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir(D)) {2074open CT,$_ornext;2075my$val= <CT>;2076chomp$val;2077close CT;2078my$ctag=$_;$ctag=~ s#.*/##;2079$ctags->{$ctag} =$val;2080}2081closedir D;2082$ctags;2083}20842085sub git_populate_project_tagcloud {2086my$ctags=shift;20872088# First, merge different-cased tags; tags vote on casing2089my%ctags_lc;2090foreach(keys%$ctags) {2091$ctags_lc{lc$_}->{count} +=$ctags->{$_};2092if(not$ctags_lc{lc$_}->{topcount}2093or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2094$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2095$ctags_lc{lc$_}->{topname} =$_;2096}2097}20982099my$cloud;2100if(eval{require HTML::TagCloud;1; }) {2101$cloud= HTML::TagCloud->new;2102foreach(sort keys%ctags_lc) {2103# Pad the title with spaces so that the cloud looks2104# less crammed.2105my$title=$ctags_lc{$_}->{topname};2106$title=~s/ / /g;2107$title=~s/^/ /g;2108$title=~s/$/ /g;2109$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2110}2111}else{2112$cloud= \%ctags_lc;2113}2114$cloud;2115}21162117sub git_show_project_tagcloud {2118my($cloud,$count) =@_;2119print STDERR ref($cloud)."..\n";2120if(ref$cloudeq'HTML::TagCloud') {2121return$cloud->html_and_css($count);2122}else{2123my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2124return'<p align="center">'.join(', ',map{2125"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2126}splice(@tags,0,$count)) .'</p>';2127}2128}21292130sub git_get_project_url_list {2131my$path=shift;21322133$git_dir="$projectroot/$path";2134open my$fd,"$git_dir/cloneurl"2135orreturnwantarray?2136@{ config_to_multi(git_get_project_config('url')) } :2137 config_to_multi(git_get_project_config('url'));2138my@git_project_url_list=map{chomp;$_} <$fd>;2139close$fd;21402141returnwantarray?@git_project_url_list: \@git_project_url_list;2142}21432144sub git_get_projects_list {2145my($filter) =@_;2146my@list;21472148$filter||='';2149$filter=~s/\.git$//;21502151my$check_forks= gitweb_check_feature('forks');21522153if(-d $projects_list) {2154# search in directory2155my$dir=$projects_list. ($filter?"/$filter":'');2156# remove the trailing "/"2157$dir=~s!/+$!!;2158my$pfxlen=length("$dir");2159my$pfxdepth= ($dir=~tr!/!!);21602161 File::Find::find({2162 follow_fast =>1,# follow symbolic links2163 follow_skip =>2,# ignore duplicates2164 dangling_symlinks =>0,# ignore dangling symlinks, silently2165 wanted =>sub{2166# skip project-list toplevel, if we get it.2167return if(m!^[/.]$!);2168# only directories can be git repositories2169return unless(-d $_);2170# don't traverse too deep (Find is super slow on os x)2171if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2172$File::Find::prune =1;2173return;2174}21752176my$subdir=substr($File::Find::name,$pfxlen+1);2177# we check related file in $projectroot2178my$path= ($filter?"$filter/":'') .$subdir;2179if(check_export_ok("$projectroot/$path")) {2180push@list, { path =>$path};2181$File::Find::prune =1;2182}2183},2184},"$dir");21852186}elsif(-f $projects_list) {2187# read from file(url-encoded):2188# 'git%2Fgit.git Linus+Torvalds'2189# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2190# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2191my%paths;2192open my($fd),$projects_listorreturn;2193 PROJECT:2194while(my$line= <$fd>) {2195chomp$line;2196my($path,$owner) =split' ',$line;2197$path= unescape($path);2198$owner= unescape($owner);2199if(!defined$path) {2200next;2201}2202if($filterne'') {2203# looking for forks;2204my$pfx=substr($path,0,length($filter));2205if($pfxne$filter) {2206next PROJECT;2207}2208my$sfx=substr($path,length($filter));2209if($sfx!~/^\/.*\.git$/) {2210next PROJECT;2211}2212}elsif($check_forks) {2213 PATH:2214foreachmy$filter(keys%paths) {2215# looking for forks;2216my$pfx=substr($path,0,length($filter));2217if($pfxne$filter) {2218next PATH;2219}2220my$sfx=substr($path,length($filter));2221if($sfx!~/^\/.*\.git$/) {2222next PATH;2223}2224# is a fork, don't include it in2225# the list2226next PROJECT;2227}2228}2229if(check_export_ok("$projectroot/$path")) {2230my$pr= {2231 path =>$path,2232 owner => to_utf8($owner),2233};2234push@list,$pr;2235(my$forks_path=$path) =~s/\.git$//;2236$paths{$forks_path}++;2237}2238}2239close$fd;2240}2241return@list;2242}22432244our$gitweb_project_owner=undef;2245sub git_get_project_list_from_file {22462247return if(defined$gitweb_project_owner);22482249$gitweb_project_owner= {};2250# read from file (url-encoded):2251# 'git%2Fgit.git Linus+Torvalds'2252# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2253# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2254if(-f $projects_list) {2255open(my$fd,$projects_list);2256while(my$line= <$fd>) {2257chomp$line;2258my($pr,$ow) =split' ',$line;2259$pr= unescape($pr);2260$ow= unescape($ow);2261$gitweb_project_owner->{$pr} = to_utf8($ow);2262}2263close$fd;2264}2265}22662267sub git_get_project_owner {2268my$project=shift;2269my$owner;22702271returnundefunless$project;2272$git_dir="$projectroot/$project";22732274if(!defined$gitweb_project_owner) {2275 git_get_project_list_from_file();2276}22772278if(exists$gitweb_project_owner->{$project}) {2279$owner=$gitweb_project_owner->{$project};2280}2281if(!defined$owner){2282$owner= git_get_project_config('owner');2283}2284if(!defined$owner) {2285$owner= get_file_owner("$git_dir");2286}22872288return$owner;2289}22902291sub git_get_last_activity {2292my($path) =@_;2293my$fd;22942295$git_dir="$projectroot/$path";2296open($fd,"-|", git_cmd(),'for-each-ref',2297'--format=%(committer)',2298'--sort=-committerdate',2299'--count=1',2300'refs/heads')orreturn;2301my$most_recent= <$fd>;2302close$fdorreturn;2303if(defined$most_recent&&2304$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2305my$timestamp=$1;2306my$age=time-$timestamp;2307return($age, age_string($age));2308}2309return(undef,undef);2310}23112312sub git_get_references {2313my$type=shift||"";2314my%refs;2315# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112316# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2317open my$fd,"-|", git_cmd(),"show-ref","--dereference",2318($type? ("--","refs/$type") : ())# use -- <pattern> if $type2319orreturn;23202321while(my$line= <$fd>) {2322chomp$line;2323if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2324if(defined$refs{$1}) {2325push@{$refs{$1}},$2;2326}else{2327$refs{$1} = [$2];2328}2329}2330}2331close$fdorreturn;2332return \%refs;2333}23342335sub git_get_rev_name_tags {2336my$hash=shift||returnundef;23372338open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2339orreturn;2340my$name_rev= <$fd>;2341close$fd;23422343if($name_rev=~ m|^$hash tags/(.*)$|) {2344return$1;2345}else{2346# catches also '$hash undefined' output2347returnundef;2348}2349}23502351## ----------------------------------------------------------------------2352## parse to hash functions23532354sub parse_date {2355my$epoch=shift;2356my$tz=shift||"-0000";23572358my%date;2359my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2360my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2361my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2362$date{'hour'} =$hour;2363$date{'minute'} =$min;2364$date{'mday'} =$mday;2365$date{'day'} =$days[$wday];2366$date{'month'} =$months[$mon];2367$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2368$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2369$date{'mday-time'} =sprintf"%d%s%02d:%02d",2370$mday,$months[$mon],$hour,$min;2371$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",23721900+$year,1+$mon,$mday,$hour,$min,$sec;23732374$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2375my$local=$epoch+ ((int$1+ ($2/60)) *3600);2376($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2377$date{'hour_local'} =$hour;2378$date{'minute_local'} =$min;2379$date{'tz_local'} =$tz;2380$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",23811900+$year,$mon+1,$mday,2382$hour,$min,$sec,$tz);2383return%date;2384}23852386sub parse_tag {2387my$tag_id=shift;2388my%tag;2389my@comment;23902391open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2392$tag{'id'} =$tag_id;2393while(my$line= <$fd>) {2394chomp$line;2395if($line=~m/^object ([0-9a-fA-F]{40})$/) {2396$tag{'object'} =$1;2397}elsif($line=~m/^type (.+)$/) {2398$tag{'type'} =$1;2399}elsif($line=~m/^tag (.+)$/) {2400$tag{'name'} =$1;2401}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2402$tag{'author'} =$1;2403$tag{'epoch'} =$2;2404$tag{'tz'} =$3;2405}elsif($line=~m/--BEGIN/) {2406push@comment,$line;2407last;2408}elsif($lineeq"") {2409last;2410}2411}2412push@comment, <$fd>;2413$tag{'comment'} = \@comment;2414close$fdorreturn;2415if(!defined$tag{'name'}) {2416return2417};2418return%tag2419}24202421sub parse_commit_text {2422my($commit_text,$withparents) =@_;2423my@commit_lines=split'\n',$commit_text;2424my%co;24252426pop@commit_lines;# Remove '\0'24272428if(!@commit_lines) {2429return;2430}24312432my$header=shift@commit_lines;2433if($header!~m/^[0-9a-fA-F]{40}/) {2434return;2435}2436($co{'id'},my@parents) =split' ',$header;2437while(my$line=shift@commit_lines) {2438last if$lineeq"\n";2439if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2440$co{'tree'} =$1;2441}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2442push@parents,$1;2443}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2444$co{'author'} =$1;2445$co{'author_epoch'} =$2;2446$co{'author_tz'} =$3;2447if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2448$co{'author_name'} =$1;2449$co{'author_email'} =$2;2450}else{2451$co{'author_name'} =$co{'author'};2452}2453}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2454$co{'committer'} =$1;2455$co{'committer_epoch'} =$2;2456$co{'committer_tz'} =$3;2457$co{'committer_name'} =$co{'committer'};2458if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2459$co{'committer_name'} =$1;2460$co{'committer_email'} =$2;2461}else{2462$co{'committer_name'} =$co{'committer'};2463}2464}2465}2466if(!defined$co{'tree'}) {2467return;2468};2469$co{'parents'} = \@parents;2470$co{'parent'} =$parents[0];24712472foreachmy$title(@commit_lines) {2473$title=~s/^ //;2474if($titlene"") {2475$co{'title'} = chop_str($title,80,5);2476# remove leading stuff of merges to make the interesting part visible2477if(length($title) >50) {2478$title=~s/^Automatic //;2479$title=~s/^merge (of|with) /Merge ... /i;2480if(length($title) >50) {2481$title=~s/(http|rsync):\/\///;2482}2483if(length($title) >50) {2484$title=~s/(master|www|rsync)\.//;2485}2486if(length($title) >50) {2487$title=~s/kernel.org:?//;2488}2489if(length($title) >50) {2490$title=~s/\/pub\/scm//;2491}2492}2493$co{'title_short'} = chop_str($title,50,5);2494last;2495}2496}2497if(!defined$co{'title'} ||$co{'title'}eq"") {2498$co{'title'} =$co{'title_short'} ='(no commit message)';2499}2500# remove added spaces2501foreachmy$line(@commit_lines) {2502$line=~s/^ //;2503}2504$co{'comment'} = \@commit_lines;25052506my$age=time-$co{'committer_epoch'};2507$co{'age'} =$age;2508$co{'age_string'} = age_string($age);2509my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2510if($age>60*60*24*7*2) {2511$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2512$co{'age_string_age'} =$co{'age_string'};2513}else{2514$co{'age_string_date'} =$co{'age_string'};2515$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2516}2517return%co;2518}25192520sub parse_commit {2521my($commit_id) =@_;2522my%co;25232524local$/="\0";25252526open my$fd,"-|", git_cmd(),"rev-list",2527"--parents",2528"--header",2529"--max-count=1",2530$commit_id,2531"--",2532or die_error(500,"Open git-rev-list failed");2533%co= parse_commit_text(<$fd>,1);2534close$fd;25352536return%co;2537}25382539sub parse_commits {2540my($commit_id,$maxcount,$skip,$filename,@args) =@_;2541my@cos;25422543$maxcount||=1;2544$skip||=0;25452546local$/="\0";25472548open my$fd,"-|", git_cmd(),"rev-list",2549"--header",2550@args,2551("--max-count=".$maxcount),2552("--skip=".$skip),2553@extra_options,2554$commit_id,2555"--",2556($filename? ($filename) : ())2557or die_error(500,"Open git-rev-list failed");2558while(my$line= <$fd>) {2559my%co= parse_commit_text($line);2560push@cos, \%co;2561}2562close$fd;25632564returnwantarray?@cos: \@cos;2565}25662567# parse line of git-diff-tree "raw" output2568sub parse_difftree_raw_line {2569my$line=shift;2570my%res;25712572# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2573# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2574if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2575$res{'from_mode'} =$1;2576$res{'to_mode'} =$2;2577$res{'from_id'} =$3;2578$res{'to_id'} =$4;2579$res{'status'} =$5;2580$res{'similarity'} =$6;2581if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2582($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2583}else{2584$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2585}2586}2587# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2588# combined diff (for merge commit)2589elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2590$res{'nparents'} =length($1);2591$res{'from_mode'} = [split(' ',$2) ];2592$res{'to_mode'} =pop@{$res{'from_mode'}};2593$res{'from_id'} = [split(' ',$3) ];2594$res{'to_id'} =pop@{$res{'from_id'}};2595$res{'status'} = [split('',$4) ];2596$res{'to_file'} = unquote($5);2597}2598# 'c512b523472485aef4fff9e57b229d9d243c967f'2599elsif($line=~m/^([0-9a-fA-F]{40})$/) {2600$res{'commit'} =$1;2601}26022603returnwantarray?%res: \%res;2604}26052606# wrapper: return parsed line of git-diff-tree "raw" output2607# (the argument might be raw line, or parsed info)2608sub parsed_difftree_line {2609my$line_or_ref=shift;26102611if(ref($line_or_ref)eq"HASH") {2612# pre-parsed (or generated by hand)2613return$line_or_ref;2614}else{2615return parse_difftree_raw_line($line_or_ref);2616}2617}26182619# parse line of git-ls-tree output2620sub parse_ls_tree_line ($;%) {2621my$line=shift;2622my%opts=@_;2623my%res;26242625#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2626$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;26272628$res{'mode'} =$1;2629$res{'type'} =$2;2630$res{'hash'} =$3;2631if($opts{'-z'}) {2632$res{'name'} =$4;2633}else{2634$res{'name'} = unquote($4);2635}26362637returnwantarray?%res: \%res;2638}26392640# generates _two_ hashes, references to which are passed as 2 and 3 argument2641sub parse_from_to_diffinfo {2642my($diffinfo,$from,$to,@parents) =@_;26432644if($diffinfo->{'nparents'}) {2645# combined diff2646$from->{'file'} = [];2647$from->{'href'} = [];2648 fill_from_file_info($diffinfo,@parents)2649unlessexists$diffinfo->{'from_file'};2650for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2651$from->{'file'}[$i] =2652defined$diffinfo->{'from_file'}[$i] ?2653$diffinfo->{'from_file'}[$i] :2654$diffinfo->{'to_file'};2655if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2656$from->{'href'}[$i] = href(action=>"blob",2657 hash_base=>$parents[$i],2658 hash=>$diffinfo->{'from_id'}[$i],2659 file_name=>$from->{'file'}[$i]);2660}else{2661$from->{'href'}[$i] =undef;2662}2663}2664}else{2665# ordinary (not combined) diff2666$from->{'file'} =$diffinfo->{'from_file'};2667if($diffinfo->{'status'}ne"A") {# not new (added) file2668$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2669 hash=>$diffinfo->{'from_id'},2670 file_name=>$from->{'file'});2671}else{2672delete$from->{'href'};2673}2674}26752676$to->{'file'} =$diffinfo->{'to_file'};2677if(!is_deleted($diffinfo)) {# file exists in result2678$to->{'href'} = href(action=>"blob", hash_base=>$hash,2679 hash=>$diffinfo->{'to_id'},2680 file_name=>$to->{'file'});2681}else{2682delete$to->{'href'};2683}2684}26852686## ......................................................................2687## parse to array of hashes functions26882689sub git_get_heads_list {2690my$limit=shift;2691my@headslist;26922693open my$fd,'-|', git_cmd(),'for-each-ref',2694($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2695'--format=%(objectname) %(refname) %(subject)%00%(committer)',2696'refs/heads'2697orreturn;2698while(my$line= <$fd>) {2699my%ref_item;27002701chomp$line;2702my($refinfo,$committerinfo) =split(/\0/,$line);2703my($hash,$name,$title) =split(' ',$refinfo,3);2704my($committer,$epoch,$tz) =2705($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2706$ref_item{'fullname'} =$name;2707$name=~s!^refs/heads/!!;27082709$ref_item{'name'} =$name;2710$ref_item{'id'} =$hash;2711$ref_item{'title'} =$title||'(no commit message)';2712$ref_item{'epoch'} =$epoch;2713if($epoch) {2714$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2715}else{2716$ref_item{'age'} ="unknown";2717}27182719push@headslist, \%ref_item;2720}2721close$fd;27222723returnwantarray?@headslist: \@headslist;2724}27252726sub git_get_tags_list {2727my$limit=shift;2728my@tagslist;27292730open my$fd,'-|', git_cmd(),'for-each-ref',2731($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2732'--format=%(objectname) %(objecttype) %(refname) '.2733'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2734'refs/tags'2735orreturn;2736while(my$line= <$fd>) {2737my%ref_item;27382739chomp$line;2740my($refinfo,$creatorinfo) =split(/\0/,$line);2741my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2742my($creator,$epoch,$tz) =2743($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2744$ref_item{'fullname'} =$name;2745$name=~s!^refs/tags/!!;27462747$ref_item{'type'} =$type;2748$ref_item{'id'} =$id;2749$ref_item{'name'} =$name;2750if($typeeq"tag") {2751$ref_item{'subject'} =$title;2752$ref_item{'reftype'} =$reftype;2753$ref_item{'refid'} =$refid;2754}else{2755$ref_item{'reftype'} =$type;2756$ref_item{'refid'} =$id;2757}27582759if($typeeq"tag"||$typeeq"commit") {2760$ref_item{'epoch'} =$epoch;2761if($epoch) {2762$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2763}else{2764$ref_item{'age'} ="unknown";2765}2766}27672768push@tagslist, \%ref_item;2769}2770close$fd;27712772returnwantarray?@tagslist: \@tagslist;2773}27742775## ----------------------------------------------------------------------2776## filesystem-related functions27772778sub get_file_owner {2779my$path=shift;27802781my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2782my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2783if(!defined$gcos) {2784returnundef;2785}2786my$owner=$gcos;2787$owner=~s/[,;].*$//;2788return to_utf8($owner);2789}27902791# assume that file exists2792sub insert_file {2793my$filename=shift;27942795open my$fd,'<',$filename;2796print map{ to_utf8($_) } <$fd>;2797close$fd;2798}27992800## ......................................................................2801## mimetype related functions28022803sub mimetype_guess_file {2804my$filename=shift;2805my$mimemap=shift;2806-r $mimemaporreturnundef;28072808my%mimemap;2809open(MIME,$mimemap)orreturnundef;2810while(<MIME>) {2811next ifm/^#/;# skip comments2812my($mime,$exts) =split(/\t+/);2813if(defined$exts) {2814my@exts=split(/\s+/,$exts);2815foreachmy$ext(@exts) {2816$mimemap{$ext} =$mime;2817}2818}2819}2820close(MIME);28212822$filename=~/\.([^.]*)$/;2823return$mimemap{$1};2824}28252826sub mimetype_guess {2827my$filename=shift;2828my$mime;2829$filename=~/\./orreturnundef;28302831if($mimetypes_file) {2832my$file=$mimetypes_file;2833if($file!~m!^/!) {# if it is relative path2834# it is relative to project2835$file="$projectroot/$project/$file";2836}2837$mime= mimetype_guess_file($filename,$file);2838}2839$mime||= mimetype_guess_file($filename,'/etc/mime.types');2840return$mime;2841}28422843sub blob_mimetype {2844my$fd=shift;2845my$filename=shift;28462847if($filename) {2848my$mime= mimetype_guess($filename);2849$mimeandreturn$mime;2850}28512852# just in case2853return$default_blob_plain_mimetypeunless$fd;28542855if(-T $fd) {2856return'text/plain';2857}elsif(!$filename) {2858return'application/octet-stream';2859}elsif($filename=~m/\.png$/i) {2860return'image/png';2861}elsif($filename=~m/\.gif$/i) {2862return'image/gif';2863}elsif($filename=~m/\.jpe?g$/i) {2864return'image/jpeg';2865}else{2866return'application/octet-stream';2867}2868}28692870sub blob_contenttype {2871my($fd,$file_name,$type) =@_;28722873$type||= blob_mimetype($fd,$file_name);2874if($typeeq'text/plain'&&defined$default_text_plain_charset) {2875$type.="; charset=$default_text_plain_charset";2876}28772878return$type;2879}28802881## ======================================================================2882## functions printing HTML: header, footer, error page28832884sub git_header_html {2885my$status=shift||"200 OK";2886my$expires=shift;28872888my$title="$site_name";2889if(defined$project) {2890$title.=" - ". to_utf8($project);2891if(defined$action) {2892$title.="/$action";2893if(defined$file_name) {2894$title.=" - ". esc_path($file_name);2895if($actioneq"tree"&&$file_name!~ m|/$|) {2896$title.="/";2897}2898}2899}2900}2901my$content_type;2902# require explicit support from the UA if we are to send the page as2903# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2904# we have to do this because MSIE sometimes globs '*/*', pretending to2905# support xhtml+xml but choking when it gets what it asked for.2906if(defined$cgi->http('HTTP_ACCEPT') &&2907$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2908$cgi->Accept('application/xhtml+xml') !=0) {2909$content_type='application/xhtml+xml';2910}else{2911$content_type='text/html';2912}2913print$cgi->header(-type=>$content_type, -charset =>'utf-8',2914-status=>$status, -expires =>$expires);2915my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2916print<<EOF;2917<?xml version="1.0" encoding="utf-8"?>2918<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2919<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2920<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2921<!-- git core binaries version$git_version-->2922<head>2923<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2924<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2925<meta name="robots" content="index, nofollow"/>2926<title>$title</title>2927EOF2928# the stylesheet, favicon etc urls won't work correctly with path_info2929# unless we set the appropriate base URL2930if($ENV{'PATH_INFO'}) {2931print"<base href=\"".esc_url($base_url)."\"/>\n";2932}2933# print out each stylesheet that exist, providing backwards capability2934# for those people who defined $stylesheet in a config file2935if(defined$stylesheet) {2936print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2937}else{2938foreachmy$stylesheet(@stylesheets) {2939next unless$stylesheet;2940print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2941}2942}2943if(defined$project) {2944my%href_params= get_feed_info();2945if(!exists$href_params{'-title'}) {2946$href_params{'-title'} ='log';2947}29482949foreachmy$formatqw(RSS Atom){2950my$type=lc($format);2951my%link_attr= (2952'-rel'=>'alternate',2953'-title'=>"$project-$href_params{'-title'} -$formatfeed",2954'-type'=>"application/$type+xml"2955);29562957$href_params{'action'} =$type;2958$link_attr{'-href'} = href(%href_params);2959print"<link ".2960"rel=\"$link_attr{'-rel'}\"".2961"title=\"$link_attr{'-title'}\"".2962"href=\"$link_attr{'-href'}\"".2963"type=\"$link_attr{'-type'}\"".2964"/>\n";29652966$href_params{'extra_options'} ='--no-merges';2967$link_attr{'-href'} = href(%href_params);2968$link_attr{'-title'} .=' (no merges)';2969print"<link ".2970"rel=\"$link_attr{'-rel'}\"".2971"title=\"$link_attr{'-title'}\"".2972"href=\"$link_attr{'-href'}\"".2973"type=\"$link_attr{'-type'}\"".2974"/>\n";2975}29762977}else{2978printf('<link rel="alternate" title="%sprojects list" '.2979'href="%s" type="text/plain; charset=utf-8" />'."\n",2980$site_name, href(project=>undef, action=>"project_index"));2981printf('<link rel="alternate" title="%sprojects feeds" '.2982'href="%s" type="text/x-opml" />'."\n",2983$site_name, href(project=>undef, action=>"opml"));2984}2985if(defined$favicon) {2986printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2987}29882989print"</head>\n".2990"<body>\n";29912992if(-f $site_header) {2993 insert_file($site_header);2994}29952996print"<div class=\"page_header\">\n".2997$cgi->a({-href => esc_url($logo_url),2998-title =>$logo_label},2999qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3000print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3001if(defined$project) {3002print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3003if(defined$action) {3004print" /$action";3005}3006print"\n";3007}3008print"</div>\n";30093010my$have_search= gitweb_check_feature('search');3011if(defined$project&&$have_search) {3012if(!defined$searchtext) {3013$searchtext="";3014}3015my$search_hash;3016if(defined$hash_base) {3017$search_hash=$hash_base;3018}elsif(defined$hash) {3019$search_hash=$hash;3020}else{3021$search_hash="HEAD";3022}3023my$action=$my_uri;3024my$use_pathinfo= gitweb_check_feature('pathinfo');3025if($use_pathinfo) {3026$action.="/".esc_url($project);3027}3028print$cgi->startform(-method=>"get", -action =>$action) .3029"<div class=\"search\">\n".3030(!$use_pathinfo&&3031$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3032$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3033$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3034$cgi->popup_menu(-name =>'st', -default=>'commit',3035-values=> ['commit','grep','author','committer','pickaxe']) .3036$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3037" search:\n",3038$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3039"<span title=\"Extended regular expression\">".3040$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3041-checked =>$search_use_regexp) .3042"</span>".3043"</div>".3044$cgi->end_form() ."\n";3045}3046}30473048sub git_footer_html {3049my$feed_class='rss_logo';30503051print"<div class=\"page_footer\">\n";3052if(defined$project) {3053my$descr= git_get_project_description($project);3054if(defined$descr) {3055print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3056}30573058my%href_params= get_feed_info();3059if(!%href_params) {3060$feed_class.=' generic';3061}3062$href_params{'-title'} ||='log';30633064foreachmy$formatqw(RSS Atom){3065$href_params{'action'} =lc($format);3066print$cgi->a({-href => href(%href_params),3067-title =>"$href_params{'-title'}$formatfeed",3068-class=>$feed_class},$format)."\n";3069}30703071}else{3072print$cgi->a({-href => href(project=>undef, action=>"opml"),3073-class=>$feed_class},"OPML") ." ";3074print$cgi->a({-href => href(project=>undef, action=>"project_index"),3075-class=>$feed_class},"TXT") ."\n";3076}3077print"</div>\n";# class="page_footer"30783079if(-f $site_footer) {3080 insert_file($site_footer);3081}30823083print"</body>\n".3084"</html>";3085}30863087# die_error(<http_status_code>, <error_message>)3088# Example: die_error(404, 'Hash not found')3089# By convention, use the following status codes (as defined in RFC 2616):3090# 400: Invalid or missing CGI parameters, or3091# requested object exists but has wrong type.3092# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3093# this server or project.3094# 404: Requested object/revision/project doesn't exist.3095# 500: The server isn't configured properly, or3096# an internal error occurred (e.g. failed assertions caused by bugs), or3097# an unknown error occurred (e.g. the git binary died unexpectedly).3098sub die_error {3099my$status=shift||500;3100my$error=shift||"Internal server error";31013102my%http_responses= (400=>'400 Bad Request',3103403=>'403 Forbidden',3104404=>'404 Not Found',3105500=>'500 Internal Server Error');3106 git_header_html($http_responses{$status});3107print<<EOF;3108<div class="page_body">3109<br /><br />3110$status-$error3111<br />3112</div>3113EOF3114 git_footer_html();3115exit;3116}31173118## ----------------------------------------------------------------------3119## functions printing or outputting HTML: navigation31203121sub git_print_page_nav {3122my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3123$extra=''if!defined$extra;# pager or formats31243125my@navs=qw(summary shortlog log commit commitdiff tree);3126if($suppress) {3127@navs=grep{$_ne$suppress}@navs;3128}31293130my%arg=map{$_=> {action=>$_} }@navs;3131if(defined$head) {3132for(qw(commit commitdiff)) {3133$arg{$_}{'hash'} =$head;3134}3135if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3136for(qw(shortlog log)) {3137$arg{$_}{'hash'} =$head;3138}3139}3140}31413142$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3143$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;31443145my@actions= gitweb_get_feature('actions');3146my%repl= (3147'%'=>'%',3148'n'=>$project,# project name3149'f'=>$git_dir,# project path within filesystem3150'h'=>$treehead||'',# current hash ('h' parameter)3151'b'=>$treebase||'',# hash base ('hb' parameter)3152);3153while(@actions) {3154my($label,$link,$pos) =splice(@actions,0,3);3155# insert3156@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3157# munch munch3158$link=~s/%([%nfhb])/$repl{$1}/g;3159$arg{$label}{'_href'} =$link;3160}31613162print"<div class=\"page_nav\">\n".3163(join" | ",3164map{$_eq$current?3165$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3166}@navs);3167print"<br/>\n$extra<br/>\n".3168"</div>\n";3169}31703171sub format_paging_nav {3172my($action,$hash,$head,$page,$has_next_link) =@_;3173my$paging_nav;317431753176if($hashne$head||$page) {3177$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3178}else{3179$paging_nav.="HEAD";3180}31813182if($page>0) {3183$paging_nav.=" ⋅ ".3184$cgi->a({-href => href(-replay=>1, page=>$page-1),3185-accesskey =>"p", -title =>"Alt-p"},"prev");3186}else{3187$paging_nav.=" ⋅ prev";3188}31893190if($has_next_link) {3191$paging_nav.=" ⋅ ".3192$cgi->a({-href => href(-replay=>1, page=>$page+1),3193-accesskey =>"n", -title =>"Alt-n"},"next");3194}else{3195$paging_nav.=" ⋅ next";3196}31973198return$paging_nav;3199}32003201## ......................................................................3202## functions printing or outputting HTML: div32033204sub git_print_header_div {3205my($action,$title,$hash,$hash_base) =@_;3206my%args= ();32073208$args{'action'} =$action;3209$args{'hash'} =$hashif$hash;3210$args{'hash_base'} =$hash_baseif$hash_base;32113212print"<div class=\"header\">\n".3213$cgi->a({-href => href(%args), -class=>"title"},3214$title?$title:$action) .3215"\n</div>\n";3216}32173218#sub git_print_authorship (\%) {3219sub git_print_authorship {3220my$co=shift;32213222my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3223print"<div class=\"author_date\">".3224 esc_html($co->{'author_name'}) .3225" [$ad{'rfc2822'}";3226if($ad{'hour_local'} <6) {3227printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3228$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3229}else{3230printf(" (%02d:%02d%s)",3231$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3232}3233print"]</div>\n";3234}32353236sub git_print_page_path {3237my$name=shift;3238my$type=shift;3239my$hb=shift;324032413242print"<div class=\"page_path\">";3243print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3244-title =>'tree root'}, to_utf8("[$project]"));3245print" / ";3246if(defined$name) {3247my@dirname=split'/',$name;3248my$basename=pop@dirname;3249my$fullname='';32503251foreachmy$dir(@dirname) {3252$fullname.= ($fullname?'/':'') .$dir;3253print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3254 hash_base=>$hb),3255-title =>$fullname}, esc_path($dir));3256print" / ";3257}3258if(defined$type&&$typeeq'blob') {3259print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3260 hash_base=>$hb),3261-title =>$name}, esc_path($basename));3262}elsif(defined$type&&$typeeq'tree') {3263print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3264 hash_base=>$hb),3265-title =>$name}, esc_path($basename));3266print" / ";3267}else{3268print esc_path($basename);3269}3270}3271print"<br/></div>\n";3272}32733274# sub git_print_log (\@;%) {3275sub git_print_log ($;%) {3276my$log=shift;3277my%opts=@_;32783279if($opts{'-remove_title'}) {3280# remove title, i.e. first line of log3281shift@$log;3282}3283# remove leading empty lines3284while(defined$log->[0] &&$log->[0]eq"") {3285shift@$log;3286}32873288# print log3289my$signoff=0;3290my$empty=0;3291foreachmy$line(@$log) {3292if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3293$signoff=1;3294$empty=0;3295if(!$opts{'-remove_signoff'}) {3296print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3297next;3298}else{3299# remove signoff lines3300next;3301}3302}else{3303$signoff=0;3304}33053306# print only one empty line3307# do not print empty line after signoff3308if($lineeq"") {3309next if($empty||$signoff);3310$empty=1;3311}else{3312$empty=0;3313}33143315print format_log_line_html($line) ."<br/>\n";3316}33173318if($opts{'-final_empty_line'}) {3319# end with single empty line3320print"<br/>\n"unless$empty;3321}3322}33233324# return link target (what link points to)3325sub git_get_link_target {3326my$hash=shift;3327my$link_target;33283329# read link3330open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3331orreturn;3332{3333local$/;3334$link_target= <$fd>;3335}3336close$fd3337orreturn;33383339return$link_target;3340}33413342# given link target, and the directory (basedir) the link is in,3343# return target of link relative to top directory (top tree);3344# return undef if it is not possible (including absolute links).3345sub normalize_link_target {3346my($link_target,$basedir,$hash_base) =@_;33473348# we can normalize symlink target only if $hash_base is provided3349return unless$hash_base;33503351# absolute symlinks (beginning with '/') cannot be normalized3352return if(substr($link_target,0,1)eq'/');33533354# normalize link target to path from top (root) tree (dir)3355my$path;3356if($basedir) {3357$path=$basedir.'/'.$link_target;3358}else{3359# we are in top (root) tree (dir)3360$path=$link_target;3361}33623363# remove //, /./, and /../3364my@path_parts;3365foreachmy$part(split('/',$path)) {3366# discard '.' and ''3367next if(!$part||$parteq'.');3368# handle '..'3369if($parteq'..') {3370if(@path_parts) {3371pop@path_parts;3372}else{3373# link leads outside repository (outside top dir)3374return;3375}3376}else{3377push@path_parts,$part;3378}3379}3380$path=join('/',@path_parts);33813382return$path;3383}33843385# print tree entry (row of git_tree), but without encompassing <tr> element3386sub git_print_tree_entry {3387my($t,$basedir,$hash_base,$have_blame) =@_;33883389my%base_key= ();3390$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33913392# The format of a table row is: mode list link. Where mode is3393# the mode of the entry, list is the name of the entry, an href,3394# and link is the action links of the entry.33953396print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3397if($t->{'type'}eq"blob") {3398print"<td class=\"list\">".3399$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3400 file_name=>"$basedir$t->{'name'}",%base_key),3401-class=>"list"}, esc_path($t->{'name'}));3402if(S_ISLNK(oct$t->{'mode'})) {3403my$link_target= git_get_link_target($t->{'hash'});3404if($link_target) {3405my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3406if(defined$norm_target) {3407print" -> ".3408$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3409 file_name=>$norm_target),3410-title =>$norm_target}, esc_path($link_target));3411}else{3412print" -> ". esc_path($link_target);3413}3414}3415}3416print"</td>\n";3417print"<td class=\"link\">";3418print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3419 file_name=>"$basedir$t->{'name'}",%base_key)},3420"blob");3421if($have_blame) {3422print" | ".3423$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3424 file_name=>"$basedir$t->{'name'}",%base_key)},3425"blame");3426}3427if(defined$hash_base) {3428print" | ".3429$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3430 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3431"history");3432}3433print" | ".3434$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3435 file_name=>"$basedir$t->{'name'}")},3436"raw");3437print"</td>\n";34383439}elsif($t->{'type'}eq"tree") {3440print"<td class=\"list\">";3441print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3442 file_name=>"$basedir$t->{'name'}",%base_key)},3443 esc_path($t->{'name'}));3444print"</td>\n";3445print"<td class=\"link\">";3446print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3447 file_name=>"$basedir$t->{'name'}",%base_key)},3448"tree");3449if(defined$hash_base) {3450print" | ".3451$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3452 file_name=>"$basedir$t->{'name'}")},3453"history");3454}3455print"</td>\n";3456}else{3457# unknown object: we can only present history for it3458# (this includes 'commit' object, i.e. submodule support)3459print"<td class=\"list\">".3460 esc_path($t->{'name'}) .3461"</td>\n";3462print"<td class=\"link\">";3463if(defined$hash_base) {3464print$cgi->a({-href => href(action=>"history",3465 hash_base=>$hash_base,3466 file_name=>"$basedir$t->{'name'}")},3467"history");3468}3469print"</td>\n";3470}3471}34723473## ......................................................................3474## functions printing large fragments of HTML34753476# get pre-image filenames for merge (combined) diff3477sub fill_from_file_info {3478my($diff,@parents) =@_;34793480$diff->{'from_file'} = [ ];3481$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3482for(my$i=0;$i<$diff->{'nparents'};$i++) {3483if($diff->{'status'}[$i]eq'R'||3484$diff->{'status'}[$i]eq'C') {3485$diff->{'from_file'}[$i] =3486 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3487}3488}34893490return$diff;3491}34923493# is current raw difftree line of file deletion3494sub is_deleted {3495my$diffinfo=shift;34963497return$diffinfo->{'to_id'}eq('0' x 40);3498}34993500# does patch correspond to [previous] difftree raw line3501# $diffinfo - hashref of parsed raw diff format3502# $patchinfo - hashref of parsed patch diff format3503# (the same keys as in $diffinfo)3504sub is_patch_split {3505my($diffinfo,$patchinfo) =@_;35063507returndefined$diffinfo&&defined$patchinfo3508&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3509}351035113512sub git_difftree_body {3513my($difftree,$hash,@parents) =@_;3514my($parent) =$parents[0];3515my$have_blame= gitweb_check_feature('blame');3516print"<div class=\"list_head\">\n";3517if($#{$difftree} >10) {3518print(($#{$difftree} +1) ." files changed:\n");3519}3520print"</div>\n";35213522print"<table class=\"".3523(@parents>1?"combined ":"") .3524"diff_tree\">\n";35253526# header only for combined diff in 'commitdiff' view3527my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3528if($has_header) {3529# table header3530print"<thead><tr>\n".3531"<th></th><th></th>\n";# filename, patchN link3532for(my$i=0;$i<@parents;$i++) {3533my$par=$parents[$i];3534print"<th>".3535$cgi->a({-href => href(action=>"commitdiff",3536 hash=>$hash, hash_parent=>$par),3537-title =>'commitdiff to parent number '.3538($i+1) .': '.substr($par,0,7)},3539$i+1) .3540" </th>\n";3541}3542print"</tr></thead>\n<tbody>\n";3543}35443545my$alternate=1;3546my$patchno=0;3547foreachmy$line(@{$difftree}) {3548my$diff= parsed_difftree_line($line);35493550if($alternate) {3551print"<tr class=\"dark\">\n";3552}else{3553print"<tr class=\"light\">\n";3554}3555$alternate^=1;35563557if(exists$diff->{'nparents'}) {# combined diff35583559 fill_from_file_info($diff,@parents)3560unlessexists$diff->{'from_file'};35613562if(!is_deleted($diff)) {3563# file exists in the result (child) commit3564print"<td>".3565$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3566 file_name=>$diff->{'to_file'},3567 hash_base=>$hash),3568-class=>"list"}, esc_path($diff->{'to_file'})) .3569"</td>\n";3570}else{3571print"<td>".3572 esc_path($diff->{'to_file'}) .3573"</td>\n";3574}35753576if($actioneq'commitdiff') {3577# link to patch3578$patchno++;3579print"<td class=\"link\">".3580$cgi->a({-href =>"#patch$patchno"},"patch") .3581" | ".3582"</td>\n";3583}35843585my$has_history=0;3586my$not_deleted=0;3587for(my$i=0;$i<$diff->{'nparents'};$i++) {3588my$hash_parent=$parents[$i];3589my$from_hash=$diff->{'from_id'}[$i];3590my$from_path=$diff->{'from_file'}[$i];3591my$status=$diff->{'status'}[$i];35923593$has_history||= ($statusne'A');3594$not_deleted||= ($statusne'D');35953596if($statuseq'A') {3597print"<td class=\"link\"align=\"right\"> | </td>\n";3598}elsif($statuseq'D') {3599print"<td class=\"link\">".3600$cgi->a({-href => href(action=>"blob",3601 hash_base=>$hash,3602 hash=>$from_hash,3603 file_name=>$from_path)},3604"blob". ($i+1)) .3605" | </td>\n";3606}else{3607if($diff->{'to_id'}eq$from_hash) {3608print"<td class=\"link nochange\">";3609}else{3610print"<td class=\"link\">";3611}3612print$cgi->a({-href => href(action=>"blobdiff",3613 hash=>$diff->{'to_id'},3614 hash_parent=>$from_hash,3615 hash_base=>$hash,3616 hash_parent_base=>$hash_parent,3617 file_name=>$diff->{'to_file'},3618 file_parent=>$from_path)},3619"diff". ($i+1)) .3620" | </td>\n";3621}3622}36233624print"<td class=\"link\">";3625if($not_deleted) {3626print$cgi->a({-href => href(action=>"blob",3627 hash=>$diff->{'to_id'},3628 file_name=>$diff->{'to_file'},3629 hash_base=>$hash)},3630"blob");3631print" | "if($has_history);3632}3633if($has_history) {3634print$cgi->a({-href => href(action=>"history",3635 file_name=>$diff->{'to_file'},3636 hash_base=>$hash)},3637"history");3638}3639print"</td>\n";36403641print"</tr>\n";3642next;# instead of 'else' clause, to avoid extra indent3643}3644# else ordinary diff36453646my($to_mode_oct,$to_mode_str,$to_file_type);3647my($from_mode_oct,$from_mode_str,$from_file_type);3648if($diff->{'to_mode'}ne('0' x 6)) {3649$to_mode_oct=oct$diff->{'to_mode'};3650if(S_ISREG($to_mode_oct)) {# only for regular file3651$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3652}3653$to_file_type= file_type($diff->{'to_mode'});3654}3655if($diff->{'from_mode'}ne('0' x 6)) {3656$from_mode_oct=oct$diff->{'from_mode'};3657if(S_ISREG($to_mode_oct)) {# only for regular file3658$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3659}3660$from_file_type= file_type($diff->{'from_mode'});3661}36623663if($diff->{'status'}eq"A") {# created3664my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3665$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3666$mode_chng.="]</span>";3667print"<td>";3668print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3669 hash_base=>$hash, file_name=>$diff->{'file'}),3670-class=>"list"}, esc_path($diff->{'file'}));3671print"</td>\n";3672print"<td>$mode_chng</td>\n";3673print"<td class=\"link\">";3674if($actioneq'commitdiff') {3675# link to patch3676$patchno++;3677print$cgi->a({-href =>"#patch$patchno"},"patch");3678print" | ";3679}3680print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3681 hash_base=>$hash, file_name=>$diff->{'file'})},3682"blob");3683print"</td>\n";36843685}elsif($diff->{'status'}eq"D") {# deleted3686my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3687print"<td>";3688print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3689 hash_base=>$parent, file_name=>$diff->{'file'}),3690-class=>"list"}, esc_path($diff->{'file'}));3691print"</td>\n";3692print"<td>$mode_chng</td>\n";3693print"<td class=\"link\">";3694if($actioneq'commitdiff') {3695# link to patch3696$patchno++;3697print$cgi->a({-href =>"#patch$patchno"},"patch");3698print" | ";3699}3700print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3701 hash_base=>$parent, file_name=>$diff->{'file'})},3702"blob") ." | ";3703if($have_blame) {3704print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3705 file_name=>$diff->{'file'})},3706"blame") ." | ";3707}3708print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3709 file_name=>$diff->{'file'})},3710"history");3711print"</td>\n";37123713}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3714my$mode_chnge="";3715if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3716$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3717if($from_file_typene$to_file_type) {3718$mode_chnge.=" from$from_file_typeto$to_file_type";3719}3720if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3721if($from_mode_str&&$to_mode_str) {3722$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3723}elsif($to_mode_str) {3724$mode_chnge.=" mode:$to_mode_str";3725}3726}3727$mode_chnge.="]</span>\n";3728}3729print"<td>";3730print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3731 hash_base=>$hash, file_name=>$diff->{'file'}),3732-class=>"list"}, esc_path($diff->{'file'}));3733print"</td>\n";3734print"<td>$mode_chnge</td>\n";3735print"<td class=\"link\">";3736if($actioneq'commitdiff') {3737# link to patch3738$patchno++;3739print$cgi->a({-href =>"#patch$patchno"},"patch") .3740" | ";3741}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3742# "commit" view and modified file (not onlu mode changed)3743print$cgi->a({-href => href(action=>"blobdiff",3744 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3745 hash_base=>$hash, hash_parent_base=>$parent,3746 file_name=>$diff->{'file'})},3747"diff") .3748" | ";3749}3750print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3751 hash_base=>$hash, file_name=>$diff->{'file'})},3752"blob") ." | ";3753if($have_blame) {3754print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3755 file_name=>$diff->{'file'})},3756"blame") ." | ";3757}3758print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3759 file_name=>$diff->{'file'})},3760"history");3761print"</td>\n";37623763}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3764my%status_name= ('R'=>'moved','C'=>'copied');3765my$nstatus=$status_name{$diff->{'status'}};3766my$mode_chng="";3767if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3768# mode also for directories, so we cannot use $to_mode_str3769$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3770}3771print"<td>".3772$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3773 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3774-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3775"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3776$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3777 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3778-class=>"list"}, esc_path($diff->{'from_file'})) .3779" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3780"<td class=\"link\">";3781if($actioneq'commitdiff') {3782# link to patch3783$patchno++;3784print$cgi->a({-href =>"#patch$patchno"},"patch") .3785" | ";3786}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3787# "commit" view and modified file (not only pure rename or copy)3788print$cgi->a({-href => href(action=>"blobdiff",3789 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3790 hash_base=>$hash, hash_parent_base=>$parent,3791 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3792"diff") .3793" | ";3794}3795print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3796 hash_base=>$parent, file_name=>$diff->{'to_file'})},3797"blob") ." | ";3798if($have_blame) {3799print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3800 file_name=>$diff->{'to_file'})},3801"blame") ." | ";3802}3803print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3804 file_name=>$diff->{'to_file'})},3805"history");3806print"</td>\n";38073808}# we should not encounter Unmerged (U) or Unknown (X) status3809print"</tr>\n";3810}3811print"</tbody>"if$has_header;3812print"</table>\n";3813}38143815sub git_patchset_body {3816my($fd,$difftree,$hash,@hash_parents) =@_;3817my($hash_parent) =$hash_parents[0];38183819my$is_combined= (@hash_parents>1);3820my$patch_idx=0;3821my$patch_number=0;3822my$patch_line;3823my$diffinfo;3824my$to_name;3825my(%from,%to);38263827print"<div class=\"patchset\">\n";38283829# skip to first patch3830while($patch_line= <$fd>) {3831chomp$patch_line;38323833last if($patch_line=~m/^diff /);3834}38353836 PATCH:3837while($patch_line) {38383839# parse "git diff" header line3840if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3841# $1 is from_name, which we do not use3842$to_name= unquote($2);3843$to_name=~s!^b/!!;3844}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3845# $1 is 'cc' or 'combined', which we do not use3846$to_name= unquote($2);3847}else{3848$to_name=undef;3849}38503851# check if current patch belong to current raw line3852# and parse raw git-diff line if needed3853if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3854# this is continuation of a split patch3855print"<div class=\"patch cont\">\n";3856}else{3857# advance raw git-diff output if needed3858$patch_idx++ifdefined$diffinfo;38593860# read and prepare patch information3861$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38623863# compact combined diff output can have some patches skipped3864# find which patch (using pathname of result) we are at now;3865if($is_combined) {3866while($to_namene$diffinfo->{'to_file'}) {3867print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3868 format_diff_cc_simplified($diffinfo,@hash_parents) .3869"</div>\n";# class="patch"38703871$patch_idx++;3872$patch_number++;38733874last if$patch_idx>$#$difftree;3875$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3876}3877}38783879# modifies %from, %to hashes3880 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);38813882# this is first patch for raw difftree line with $patch_idx index3883# we index @$difftree array from 0, but number patches from 13884print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3885}38863887# git diff header3888#assert($patch_line =~ m/^diff /) if DEBUG;3889#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3890$patch_number++;3891# print "git diff" header3892print format_git_diff_header_line($patch_line,$diffinfo,3893 \%from, \%to);38943895# print extended diff header3896print"<div class=\"diff extended_header\">\n";3897 EXTENDED_HEADER:3898while($patch_line= <$fd>) {3899chomp$patch_line;39003901last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);39023903print format_extended_diff_header_line($patch_line,$diffinfo,3904 \%from, \%to);3905}3906print"</div>\n";# class="diff extended_header"39073908# from-file/to-file diff header3909if(!$patch_line) {3910print"</div>\n";# class="patch"3911last PATCH;3912}3913next PATCH if($patch_line=~m/^diff /);3914#assert($patch_line =~ m/^---/) if DEBUG;39153916my$last_patch_line=$patch_line;3917$patch_line= <$fd>;3918chomp$patch_line;3919#assert($patch_line =~ m/^\+\+\+/) if DEBUG;39203921print format_diff_from_to_header($last_patch_line,$patch_line,3922$diffinfo, \%from, \%to,3923@hash_parents);39243925# the patch itself3926 LINE:3927while($patch_line= <$fd>) {3928chomp$patch_line;39293930next PATCH if($patch_line=~m/^diff /);39313932print format_diff_line($patch_line, \%from, \%to);3933}39343935}continue{3936print"</div>\n";# class="patch"3937}39383939# for compact combined (--cc) format, with chunk and patch simpliciaction3940# patchset might be empty, but there might be unprocessed raw lines3941for(++$patch_idxif$patch_number>0;3942$patch_idx<@$difftree;3943++$patch_idx) {3944# read and prepare patch information3945$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39463947# generate anchor for "patch" links in difftree / whatchanged part3948print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3949 format_diff_cc_simplified($diffinfo,@hash_parents) .3950"</div>\n";# class="patch"39513952$patch_number++;3953}39543955if($patch_number==0) {3956if(@hash_parents>1) {3957print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3958}else{3959print"<div class=\"diff nodifferences\">No differences found</div>\n";3960}3961}39623963print"</div>\n";# class="patchset"3964}39653966# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39673968# fills project list info (age, description, owner, forks) for each3969# project in the list, removing invalid projects from returned list3970# NOTE: modifies $projlist, but does not remove entries from it3971sub fill_project_list_info {3972my($projlist,$check_forks) =@_;3973my@projects;39743975my$show_ctags= gitweb_check_feature('ctags');3976 PROJECT:3977foreachmy$pr(@$projlist) {3978my(@activity) = git_get_last_activity($pr->{'path'});3979unless(@activity) {3980next PROJECT;3981}3982($pr->{'age'},$pr->{'age_string'}) =@activity;3983if(!defined$pr->{'descr'}) {3984my$descr= git_get_project_description($pr->{'path'}) ||"";3985$descr= to_utf8($descr);3986$pr->{'descr_long'} =$descr;3987$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3988}3989if(!defined$pr->{'owner'}) {3990$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3991}3992if($check_forks) {3993my$pname=$pr->{'path'};3994if(($pname=~s/\.git$//) &&3995($pname!~/\/$/) &&3996(-d "$projectroot/$pname")) {3997$pr->{'forks'} ="-d$projectroot/$pname";3998}else{3999$pr->{'forks'} =0;4000}4001}4002$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4003push@projects,$pr;4004}40054006return@projects;4007}40084009# print 'sort by' <th> element, generating 'sort by $name' replay link4010# if that order is not selected4011sub print_sort_th {4012my($name,$order,$header) =@_;4013$header||=ucfirst($name);40144015if($ordereq$name) {4016print"<th>$header</th>\n";4017}else{4018print"<th>".4019$cgi->a({-href => href(-replay=>1, order=>$name),4020-class=>"header"},$header) .4021"</th>\n";4022}4023}40244025sub git_project_list_body {4026# actually uses global variable $project4027my($projlist,$order,$from,$to,$extra,$no_header) =@_;40284029my$check_forks= gitweb_check_feature('forks');4030my@projects= fill_project_list_info($projlist,$check_forks);40314032$order||=$default_projects_order;4033$from=0unlessdefined$from;4034$to=$#projectsif(!defined$to||$#projects<$to);40354036my%order_info= (4037 project => { key =>'path', type =>'str'},4038 descr => { key =>'descr_long', type =>'str'},4039 owner => { key =>'owner', type =>'str'},4040 age => { key =>'age', type =>'num'}4041);4042my$oi=$order_info{$order};4043if($oi->{'type'}eq'str') {4044@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4045}else{4046@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4047}40484049my$show_ctags= gitweb_check_feature('ctags');4050if($show_ctags) {4051my%ctags;4052foreachmy$p(@projects) {4053foreachmy$ct(keys%{$p->{'ctags'}}) {4054$ctags{$ct} +=$p->{'ctags'}->{$ct};4055}4056}4057my$cloud= git_populate_project_tagcloud(\%ctags);4058print git_show_project_tagcloud($cloud,64);4059}40604061print"<table class=\"project_list\">\n";4062unless($no_header) {4063print"<tr>\n";4064if($check_forks) {4065print"<th></th>\n";4066}4067 print_sort_th('project',$order,'Project');4068 print_sort_th('descr',$order,'Description');4069 print_sort_th('owner',$order,'Owner');4070 print_sort_th('age',$order,'Last Change');4071print"<th></th>\n".# for links4072"</tr>\n";4073}4074my$alternate=1;4075my$tagfilter=$cgi->param('by_tag');4076for(my$i=$from;$i<=$to;$i++) {4077my$pr=$projects[$i];40784079next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4080next if$searchtextand not$pr->{'path'} =~/$searchtext/4081and not$pr->{'descr_long'} =~/$searchtext/;4082# Weed out forks or non-matching entries of search4083if($check_forks) {4084my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4085$forkbase="^$forkbase"if$forkbase;4086next ifnot$searchtextand not$tagfilterand$show_ctags4087and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4088}40894090if($alternate) {4091print"<tr class=\"dark\">\n";4092}else{4093print"<tr class=\"light\">\n";4094}4095$alternate^=1;4096if($check_forks) {4097print"<td>";4098if($pr->{'forks'}) {4099print"<!--$pr->{'forks'} -->\n";4100print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4101}4102print"</td>\n";4103}4104print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4105-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4106"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4107-class=>"list", -title =>$pr->{'descr_long'}},4108 esc_html($pr->{'descr'})) ."</td>\n".4109"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4110print"<td class=\"". age_class($pr->{'age'}) ."\">".4111(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4112"<td class=\"link\">".4113$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4114$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4115$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4116$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4117($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4118"</td>\n".4119"</tr>\n";4120}4121if(defined$extra) {4122print"<tr>\n";4123if($check_forks) {4124print"<td></td>\n";4125}4126print"<td colspan=\"5\">$extra</td>\n".4127"</tr>\n";4128}4129print"</table>\n";4130}41314132sub git_shortlog_body {4133# uses global variable $project4134my($commitlist,$from,$to,$refs,$extra) =@_;41354136$from=0unlessdefined$from;4137$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41384139print"<table class=\"shortlog\">\n";4140my$alternate=1;4141for(my$i=$from;$i<=$to;$i++) {4142my%co= %{$commitlist->[$i]};4143my$commit=$co{'id'};4144my$ref= format_ref_marker($refs,$commit);4145if($alternate) {4146print"<tr class=\"dark\">\n";4147}else{4148print"<tr class=\"light\">\n";4149}4150$alternate^=1;4151my$author= chop_and_escape_str($co{'author_name'},10);4152# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4153print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4154"<td><i>".$author."</i></td>\n".4155"<td>";4156print format_subject_html($co{'title'},$co{'title_short'},4157 href(action=>"commit", hash=>$commit),$ref);4158print"</td>\n".4159"<td class=\"link\">".4160$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4161$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4162$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4163my$snapshot_links= format_snapshot_links($commit);4164if(defined$snapshot_links) {4165print" | ".$snapshot_links;4166}4167print"</td>\n".4168"</tr>\n";4169}4170if(defined$extra) {4171print"<tr>\n".4172"<td colspan=\"4\">$extra</td>\n".4173"</tr>\n";4174}4175print"</table>\n";4176}41774178sub git_history_body {4179# Warning: assumes constant type (blob or tree) during history4180my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;41814182$from=0unlessdefined$from;4183$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});41844185print"<table class=\"history\">\n";4186my$alternate=1;4187for(my$i=$from;$i<=$to;$i++) {4188my%co= %{$commitlist->[$i]};4189if(!%co) {4190next;4191}4192my$commit=$co{'id'};41934194my$ref= format_ref_marker($refs,$commit);41954196if($alternate) {4197print"<tr class=\"dark\">\n";4198}else{4199print"<tr class=\"light\">\n";4200}4201$alternate^=1;4202# shortlog uses chop_str($co{'author_name'}, 10)4203my$author= chop_and_escape_str($co{'author_name'},15,3);4204print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4205"<td><i>".$author."</i></td>\n".4206"<td>";4207# originally git_history used chop_str($co{'title'}, 50)4208print format_subject_html($co{'title'},$co{'title_short'},4209 href(action=>"commit", hash=>$commit),$ref);4210print"</td>\n".4211"<td class=\"link\">".4212$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4213$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");42144215if($ftypeeq'blob') {4216my$blob_current= git_get_hash_by_path($hash_base,$file_name);4217my$blob_parent= git_get_hash_by_path($commit,$file_name);4218if(defined$blob_current&&defined$blob_parent&&4219$blob_currentne$blob_parent) {4220print" | ".4221$cgi->a({-href => href(action=>"blobdiff",4222 hash=>$blob_current, hash_parent=>$blob_parent,4223 hash_base=>$hash_base, hash_parent_base=>$commit,4224 file_name=>$file_name)},4225"diff to current");4226}4227}4228print"</td>\n".4229"</tr>\n";4230}4231if(defined$extra) {4232print"<tr>\n".4233"<td colspan=\"4\">$extra</td>\n".4234"</tr>\n";4235}4236print"</table>\n";4237}42384239sub git_tags_body {4240# uses global variable $project4241my($taglist,$from,$to,$extra) =@_;4242$from=0unlessdefined$from;4243$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);42444245print"<table class=\"tags\">\n";4246my$alternate=1;4247for(my$i=$from;$i<=$to;$i++) {4248my$entry=$taglist->[$i];4249my%tag=%$entry;4250my$comment=$tag{'subject'};4251my$comment_short;4252if(defined$comment) {4253$comment_short= chop_str($comment,30,5);4254}4255if($alternate) {4256print"<tr class=\"dark\">\n";4257}else{4258print"<tr class=\"light\">\n";4259}4260$alternate^=1;4261if(defined$tag{'age'}) {4262print"<td><i>$tag{'age'}</i></td>\n";4263}else{4264print"<td></td>\n";4265}4266print"<td>".4267$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4268-class=>"list name"}, esc_html($tag{'name'})) .4269"</td>\n".4270"<td>";4271if(defined$comment) {4272print format_subject_html($comment,$comment_short,4273 href(action=>"tag", hash=>$tag{'id'}));4274}4275print"</td>\n".4276"<td class=\"selflink\">";4277if($tag{'type'}eq"tag") {4278print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4279}else{4280print" ";4281}4282print"</td>\n".4283"<td class=\"link\">"." | ".4284$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4285if($tag{'reftype'}eq"commit") {4286print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4287" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4288}elsif($tag{'reftype'}eq"blob") {4289print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4290}4291print"</td>\n".4292"</tr>";4293}4294if(defined$extra) {4295print"<tr>\n".4296"<td colspan=\"5\">$extra</td>\n".4297"</tr>\n";4298}4299print"</table>\n";4300}43014302sub git_heads_body {4303# uses global variable $project4304my($headlist,$head,$from,$to,$extra) =@_;4305$from=0unlessdefined$from;4306$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);43074308print"<table class=\"heads\">\n";4309my$alternate=1;4310for(my$i=$from;$i<=$to;$i++) {4311my$entry=$headlist->[$i];4312my%ref=%$entry;4313my$curr=$ref{'id'}eq$head;4314if($alternate) {4315print"<tr class=\"dark\">\n";4316}else{4317print"<tr class=\"light\">\n";4318}4319$alternate^=1;4320print"<td><i>$ref{'age'}</i></td>\n".4321($curr?"<td class=\"current_head\">":"<td>") .4322$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4323-class=>"list name"},esc_html($ref{'name'})) .4324"</td>\n".4325"<td class=\"link\">".4326$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4327$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4328$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4329"</td>\n".4330"</tr>";4331}4332if(defined$extra) {4333print"<tr>\n".4334"<td colspan=\"3\">$extra</td>\n".4335"</tr>\n";4336}4337print"</table>\n";4338}43394340sub git_search_grep_body {4341my($commitlist,$from,$to,$extra) =@_;4342$from=0unlessdefined$from;4343$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43444345print"<table class=\"commit_search\">\n";4346my$alternate=1;4347for(my$i=$from;$i<=$to;$i++) {4348my%co= %{$commitlist->[$i]};4349if(!%co) {4350next;4351}4352my$commit=$co{'id'};4353if($alternate) {4354print"<tr class=\"dark\">\n";4355}else{4356print"<tr class=\"light\">\n";4357}4358$alternate^=1;4359my$author= chop_and_escape_str($co{'author_name'},15,5);4360print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4361"<td><i>".$author."</i></td>\n".4362"<td>".4363$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4364-class=>"list subject"},4365 chop_and_escape_str($co{'title'},50) ."<br/>");4366my$comment=$co{'comment'};4367foreachmy$line(@$comment) {4368if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4369my($lead,$match,$trail) = ($1,$2,$3);4370$match= chop_str($match,70,5,'center');4371my$contextlen=int((80-length($match))/2);4372$contextlen=30if($contextlen>30);4373$lead= chop_str($lead,$contextlen,10,'left');4374$trail= chop_str($trail,$contextlen,10,'right');43754376$lead= esc_html($lead);4377$match= esc_html($match);4378$trail= esc_html($trail);43794380print"$lead<span class=\"match\">$match</span>$trail<br />";4381}4382}4383print"</td>\n".4384"<td class=\"link\">".4385$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4386" | ".4387$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4388" | ".4389$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4390print"</td>\n".4391"</tr>\n";4392}4393if(defined$extra) {4394print"<tr>\n".4395"<td colspan=\"3\">$extra</td>\n".4396"</tr>\n";4397}4398print"</table>\n";4399}44004401## ======================================================================4402## ======================================================================4403## actions44044405sub git_project_list {4406my$order=$input_params{'order'};4407if(defined$order&&$order!~m/none|project|descr|owner|age/) {4408 die_error(400,"Unknown order parameter");4409}44104411my@list= git_get_projects_list();4412if(!@list) {4413 die_error(404,"No projects found");4414}44154416 git_header_html();4417if(-f $home_text) {4418print"<div class=\"index_include\">\n";4419 insert_file($home_text);4420print"</div>\n";4421}4422print$cgi->startform(-method=>"get") .4423"<p class=\"projsearch\">Search:\n".4424$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4425"</p>".4426$cgi->end_form() ."\n";4427 git_project_list_body(\@list,$order);4428 git_footer_html();4429}44304431sub git_forks {4432my$order=$input_params{'order'};4433if(defined$order&&$order!~m/none|project|descr|owner|age/) {4434 die_error(400,"Unknown order parameter");4435}44364437my@list= git_get_projects_list($project);4438if(!@list) {4439 die_error(404,"No forks found");4440}44414442 git_header_html();4443 git_print_page_nav('','');4444 git_print_header_div('summary',"$projectforks");4445 git_project_list_body(\@list,$order);4446 git_footer_html();4447}44484449sub git_project_index {4450my@projects= git_get_projects_list($project);44514452print$cgi->header(4453-type =>'text/plain',4454-charset =>'utf-8',4455-content_disposition =>'inline; filename="index.aux"');44564457foreachmy$pr(@projects) {4458if(!exists$pr->{'owner'}) {4459$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4460}44614462my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4463# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4464$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4465$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4466$path=~s/ /\+/g;4467$owner=~s/ /\+/g;44684469print"$path$owner\n";4470}4471}44724473sub git_summary {4474my$descr= git_get_project_description($project) ||"none";4475my%co= parse_commit("HEAD");4476my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4477my$head=$co{'id'};44784479my$owner= git_get_project_owner($project);44804481my$refs= git_get_references();4482# These get_*_list functions return one more to allow us to see if4483# there are more ...4484my@taglist= git_get_tags_list(16);4485my@headlist= git_get_heads_list(16);4486my@forklist;4487my$check_forks= gitweb_check_feature('forks');44884489if($check_forks) {4490@forklist= git_get_projects_list($project);4491}44924493 git_header_html();4494 git_print_page_nav('summary','',$head);44954496print"<div class=\"title\"> </div>\n";4497print"<table class=\"projects_list\">\n".4498"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4499"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4500if(defined$cd{'rfc2822'}) {4501print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4502}45034504# use per project git URL list in $projectroot/$project/cloneurl4505# or make project git URL from git base URL and project name4506my$url_tag="URL";4507my@url_list= git_get_project_url_list($project);4508@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4509foreachmy$git_url(@url_list) {4510next unless$git_url;4511print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4512$url_tag="";4513}45144515# Tag cloud4516my$show_ctags= gitweb_check_feature('ctags');4517if($show_ctags) {4518my$ctags= git_get_project_ctags($project);4519my$cloud= git_populate_project_tagcloud($ctags);4520print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4521print"</td>\n<td>"unless%$ctags;4522print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4523print"</td>\n<td>"if%$ctags;4524print git_show_project_tagcloud($cloud,48);4525print"</td></tr>";4526}45274528print"</table>\n";45294530# If XSS prevention is on, we don't include README.html.4531# TODO: Allow a readme in some safe format.4532if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4533print"<div class=\"title\">readme</div>\n".4534"<div class=\"readme\">\n";4535 insert_file("$projectroot/$project/README.html");4536print"\n</div>\n";# class="readme"4537}45384539# we need to request one more than 16 (0..15) to check if4540# those 16 are all4541my@commitlist=$head? parse_commits($head,17) : ();4542if(@commitlist) {4543 git_print_header_div('shortlog');4544 git_shortlog_body(\@commitlist,0,15,$refs,4545$#commitlist<=15?undef:4546$cgi->a({-href => href(action=>"shortlog")},"..."));4547}45484549if(@taglist) {4550 git_print_header_div('tags');4551 git_tags_body(\@taglist,0,15,4552$#taglist<=15?undef:4553$cgi->a({-href => href(action=>"tags")},"..."));4554}45554556if(@headlist) {4557 git_print_header_div('heads');4558 git_heads_body(\@headlist,$head,0,15,4559$#headlist<=15?undef:4560$cgi->a({-href => href(action=>"heads")},"..."));4561}45624563if(@forklist) {4564 git_print_header_div('forks');4565 git_project_list_body(\@forklist,'age',0,15,4566$#forklist<=15?undef:4567$cgi->a({-href => href(action=>"forks")},"..."),4568'no_header');4569}45704571 git_footer_html();4572}45734574sub git_tag {4575my$head= git_get_head_hash($project);4576 git_header_html();4577 git_print_page_nav('','',$head,undef,$head);4578my%tag= parse_tag($hash);45794580if(!%tag) {4581 die_error(404,"Unknown tag object");4582}45834584 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4585print"<div class=\"title_text\">\n".4586"<table class=\"object_header\">\n".4587"<tr>\n".4588"<td>object</td>\n".4589"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4590$tag{'object'}) ."</td>\n".4591"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4592$tag{'type'}) ."</td>\n".4593"</tr>\n";4594if(defined($tag{'author'})) {4595my%ad= parse_date($tag{'epoch'},$tag{'tz'});4596print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4597print"<tr><td></td><td>".$ad{'rfc2822'} .4598sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4599"</td></tr>\n";4600}4601print"</table>\n\n".4602"</div>\n";4603print"<div class=\"page_body\">";4604my$comment=$tag{'comment'};4605foreachmy$line(@$comment) {4606chomp$line;4607print esc_html($line, -nbsp=>1) ."<br/>\n";4608}4609print"</div>\n";4610 git_footer_html();4611}46124613sub git_blame {4614# permissions4615 gitweb_check_feature('blame')4616or die_error(403,"Blame view not allowed");46174618# error checking4619 die_error(400,"No file name given")unless$file_name;4620$hash_base||= git_get_head_hash($project);4621 die_error(404,"Couldn't find base commit")unless$hash_base;4622my%co= parse_commit($hash_base)4623or die_error(404,"Commit not found");4624my$ftype="blob";4625if(!defined$hash) {4626$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4627or die_error(404,"Error looking up file");4628}else{4629$ftype= git_get_type($hash);4630if($ftype!~"blob") {4631 die_error(400,"Object is not a blob");4632}4633}46344635# run git-blame --porcelain4636open my$fd,"-|", git_cmd(),"blame",'-p',4637$hash_base,'--',$file_name4638or die_error(500,"Open git-blame failed");46394640# page header4641 git_header_html();4642my$formats_nav=4643$cgi->a({-href => href(action=>"blob", -replay=>1)},4644"blob") .4645" | ".4646$cgi->a({-href => href(action=>"history", -replay=>1)},4647"history") .4648" | ".4649$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4650"HEAD");4651 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4652 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4653 git_print_page_path($file_name,$ftype,$hash_base);46544655# page body4656my@rev_color=qw(light2 dark2);4657my$num_colors=scalar(@rev_color);4658my$current_color=0;4659my%metainfo= ();46604661print<<HTML;4662<div class="page_body">4663<table class="blame">4664<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4665HTML4666 LINE:4667while(my$line= <$fd>) {4668chomp$line;4669# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4670# no <lines in group> for subsequent lines in group of lines4671my($full_rev,$orig_lineno,$lineno,$group_size) =4672($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4673if(!exists$metainfo{$full_rev}) {4674$metainfo{$full_rev} = {};4675}4676my$meta=$metainfo{$full_rev};4677my$data;4678while($data= <$fd>) {4679chomp$data;4680last if($data=~s/^\t//);# contents of line4681if($data=~/^(\S+) (.*)$/) {4682$meta->{$1} =$2;4683}4684}4685my$short_rev=substr($full_rev,0,8);4686my$author=$meta->{'author'};4687my%date=4688 parse_date($meta->{'author-time'},$meta->{'author-tz'});4689my$date=$date{'iso-tz'};4690if($group_size) {4691$current_color= ($current_color+1) %$num_colors;4692}4693print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4694if($group_size) {4695print"<td class=\"sha1\"";4696print" title=\"". esc_html($author) .",$date\"";4697print" rowspan=\"$group_size\""if($group_size>1);4698print">";4699print$cgi->a({-href => href(action=>"commit",4700 hash=>$full_rev,4701 file_name=>$file_name)},4702 esc_html($short_rev));4703print"</td>\n";4704}4705my$parent_commit;4706if(!exists$meta->{'parent'}) {4707open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4708or die_error(500,"Open git-rev-parse failed");4709$parent_commit= <$dd>;4710close$dd;4711chomp($parent_commit);4712$meta->{'parent'} =$parent_commit;4713}else{4714$parent_commit=$meta->{'parent'};4715}4716my$blamed= href(action =>'blame',4717 file_name =>$meta->{'filename'},4718 hash_base =>$parent_commit);4719print"<td class=\"linenr\">";4720print$cgi->a({ -href =>"$blamed#l$orig_lineno",4721-class=>"linenr"},4722 esc_html($lineno));4723print"</td>";4724print"<td class=\"pre\">". esc_html($data) ."</td>\n";4725print"</tr>\n";4726}4727print"</table>\n";4728print"</div>";4729close$fd4730or print"Reading blob failed\n";47314732# page footer4733 git_footer_html();4734}47354736sub git_tags {4737my$head= git_get_head_hash($project);4738 git_header_html();4739 git_print_page_nav('','',$head,undef,$head);4740 git_print_header_div('summary',$project);47414742my@tagslist= git_get_tags_list();4743if(@tagslist) {4744 git_tags_body(\@tagslist);4745}4746 git_footer_html();4747}47484749sub git_heads {4750my$head= git_get_head_hash($project);4751 git_header_html();4752 git_print_page_nav('','',$head,undef,$head);4753 git_print_header_div('summary',$project);47544755my@headslist= git_get_heads_list();4756if(@headslist) {4757 git_heads_body(\@headslist,$head);4758}4759 git_footer_html();4760}47614762sub git_blob_plain {4763my$type=shift;4764my$expires;47654766if(!defined$hash) {4767if(defined$file_name) {4768my$base=$hash_base|| git_get_head_hash($project);4769$hash= git_get_hash_by_path($base,$file_name,"blob")4770or die_error(404,"Cannot find file");4771}else{4772 die_error(400,"No file name defined");4773}4774}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4775# blobs defined by non-textual hash id's can be cached4776$expires="+1d";4777}47784779open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4780or die_error(500,"Open git-cat-file blob '$hash' failed");47814782# content-type (can include charset)4783$type= blob_contenttype($fd,$file_name,$type);47844785# "save as" filename, even when no $file_name is given4786my$save_as="$hash";4787if(defined$file_name) {4788$save_as=$file_name;4789}elsif($type=~m/^text\//) {4790$save_as.='.txt';4791}47924793# With XSS prevention on, blobs of all types except a few known safe4794# ones are served with "Content-Disposition: attachment" to make sure4795# they don't run in our security domain. For certain image types,4796# blob view writes an <img> tag referring to blob_plain view, and we4797# want to be sure not to break that by serving the image as an4798# attachment (though Firefox 3 doesn't seem to care).4799my$sandbox=$prevent_xss&&4800$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;48014802print$cgi->header(4803-type =>$type,4804-expires =>$expires,4805-content_disposition =>4806($sandbox?'attachment':'inline')4807.'; filename="'.$save_as.'"');4808undef$/;4809binmode STDOUT,':raw';4810print<$fd>;4811binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4812$/="\n";4813close$fd;4814}48154816sub git_blob {4817my$expires;48184819if(!defined$hash) {4820if(defined$file_name) {4821my$base=$hash_base|| git_get_head_hash($project);4822$hash= git_get_hash_by_path($base,$file_name,"blob")4823or die_error(404,"Cannot find file");4824}else{4825 die_error(400,"No file name defined");4826}4827}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4828# blobs defined by non-textual hash id's can be cached4829$expires="+1d";4830}48314832my$have_blame= gitweb_check_feature('blame');4833open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4834or die_error(500,"Couldn't cat$file_name,$hash");4835my$mimetype= blob_mimetype($fd,$file_name);4836if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4837close$fd;4838return git_blob_plain($mimetype);4839}4840# we can have blame only for text/* mimetype4841$have_blame&&= ($mimetype=~m!^text/!);48424843 git_header_html(undef,$expires);4844my$formats_nav='';4845if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4846if(defined$file_name) {4847if($have_blame) {4848$formats_nav.=4849$cgi->a({-href => href(action=>"blame", -replay=>1)},4850"blame") .4851" | ";4852}4853$formats_nav.=4854$cgi->a({-href => href(action=>"history", -replay=>1)},4855"history") .4856" | ".4857$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4858"raw") .4859" | ".4860$cgi->a({-href => href(action=>"blob",4861 hash_base=>"HEAD", file_name=>$file_name)},4862"HEAD");4863}else{4864$formats_nav.=4865$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4866"raw");4867}4868 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4869 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4870}else{4871print"<div class=\"page_nav\">\n".4872"<br/><br/></div>\n".4873"<div class=\"title\">$hash</div>\n";4874}4875 git_print_page_path($file_name,"blob",$hash_base);4876print"<div class=\"page_body\">\n";4877if($mimetype=~m!^image/!) {4878print qq!<img type="$mimetype"!;4879if($file_name) {4880print qq! alt="$file_name" title="$file_name"!;4881}4882print qq! src="! .4883 href(action=>"blob_plain", hash=>$hash,4884 hash_base=>$hash_base, file_name=>$file_name) .4885 qq!"/>\n!;4886}else{4887my$nr;4888while(my$line= <$fd>) {4889chomp$line;4890$nr++;4891$line= untabify($line);4892printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4893$nr,$nr,$nr, esc_html($line, -nbsp=>1);4894}4895}4896close$fd4897or print"Reading blob failed.\n";4898print"</div>";4899 git_footer_html();4900}49014902sub git_tree {4903if(!defined$hash_base) {4904$hash_base="HEAD";4905}4906if(!defined$hash) {4907if(defined$file_name) {4908$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4909}else{4910$hash=$hash_base;4911}4912}4913 die_error(404,"No such tree")unlessdefined($hash);4914$/="\0";4915open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4916or die_error(500,"Open git-ls-tree failed");4917my@entries=map{chomp;$_} <$fd>;4918close$fdor die_error(404,"Reading tree failed");4919$/="\n";49204921my$refs= git_get_references();4922my$ref= format_ref_marker($refs,$hash_base);4923 git_header_html();4924my$basedir='';4925my$have_blame= gitweb_check_feature('blame');4926if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4927my@views_nav= ();4928if(defined$file_name) {4929push@views_nav,4930$cgi->a({-href => href(action=>"history", -replay=>1)},4931"history"),4932$cgi->a({-href => href(action=>"tree",4933 hash_base=>"HEAD", file_name=>$file_name)},4934"HEAD"),4935}4936my$snapshot_links= format_snapshot_links($hash);4937if(defined$snapshot_links) {4938# FIXME: Should be available when we have no hash base as well.4939push@views_nav,$snapshot_links;4940}4941 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4942 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4943}else{4944undef$hash_base;4945print"<div class=\"page_nav\">\n";4946print"<br/><br/></div>\n";4947print"<div class=\"title\">$hash</div>\n";4948}4949if(defined$file_name) {4950$basedir=$file_name;4951if($basedirne''&&substr($basedir, -1)ne'/') {4952$basedir.='/';4953}4954 git_print_page_path($file_name,'tree',$hash_base);4955}4956print"<div class=\"page_body\">\n";4957print"<table class=\"tree\">\n";4958my$alternate=1;4959# '..' (top directory) link if possible4960if(defined$hash_base&&4961defined$file_name&&$file_name=~m![^/]+$!) {4962if($alternate) {4963print"<tr class=\"dark\">\n";4964}else{4965print"<tr class=\"light\">\n";4966}4967$alternate^=1;49684969my$up=$file_name;4970$up=~s!/?[^/]+$!!;4971undef$upunless$up;4972# based on git_print_tree_entry4973print'<td class="mode">'. mode_str('040000') ."</td>\n";4974print'<td class="list">';4975print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4976 file_name=>$up)},4977"..");4978print"</td>\n";4979print"<td class=\"link\"></td>\n";49804981print"</tr>\n";4982}4983foreachmy$line(@entries) {4984my%t= parse_ls_tree_line($line, -z =>1);49854986if($alternate) {4987print"<tr class=\"dark\">\n";4988}else{4989print"<tr class=\"light\">\n";4990}4991$alternate^=1;49924993 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);49944995print"</tr>\n";4996}4997print"</table>\n".4998"</div>";4999 git_footer_html();5000}50015002sub git_snapshot {5003my$format=$input_params{'snapshot_format'};5004if(!@snapshot_fmts) {5005 die_error(403,"Snapshots not allowed");5006}5007# default to first supported snapshot format5008$format||=$snapshot_fmts[0];5009if($format!~m/^[a-z0-9]+$/) {5010 die_error(400,"Invalid snapshot format parameter");5011}elsif(!exists($known_snapshot_formats{$format})) {5012 die_error(400,"Unknown snapshot format");5013}elsif(!grep($_eq$format,@snapshot_fmts)) {5014 die_error(403,"Unsupported snapshot format");5015}50165017if(!defined$hash) {5018$hash= git_get_head_hash($project);5019}50205021my$name=$project;5022$name=~ s,([^/])/*\.git$,$1,;5023$name= basename($name);5024my$filename= to_utf8($name);5025$name=~s/\047/\047\\\047\047/g;5026my$cmd;5027$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5028$cmd= quote_command(5029 git_cmd(),'archive',5030"--format=$known_snapshot_formats{$format}{'format'}",5031"--prefix=$name/",$hash);5032if(exists$known_snapshot_formats{$format}{'compressor'}) {5033$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5034}50355036print$cgi->header(5037-type =>$known_snapshot_formats{$format}{'type'},5038-content_disposition =>'inline; filename="'."$filename".'"',5039-status =>'200 OK');50405041open my$fd,"-|",$cmd5042or die_error(500,"Execute git-archive failed");5043binmode STDOUT,':raw';5044print<$fd>;5045binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5046close$fd;5047}50485049sub git_log {5050my$head= git_get_head_hash($project);5051if(!defined$hash) {5052$hash=$head;5053}5054if(!defined$page) {5055$page=0;5056}5057my$refs= git_get_references();50585059my@commitlist= parse_commits($hash,101, (100*$page));50605061my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);50625063my($patch_max) = gitweb_get_feature('patches');5064if($patch_max) {5065if($patch_max<0||@commitlist<=$patch_max) {5066$paging_nav.=" ⋅ ".5067$cgi->a({-href => href(action=>"patches", -replay=>1)},5068"patches");5069}5070}50715072 git_header_html();5073 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);50745075if(!@commitlist) {5076my%co= parse_commit($hash);50775078 git_print_header_div('summary',$project);5079print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5080}5081my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5082for(my$i=0;$i<=$to;$i++) {5083my%co= %{$commitlist[$i]};5084next if!%co;5085my$commit=$co{'id'};5086my$ref= format_ref_marker($refs,$commit);5087my%ad= parse_date($co{'author_epoch'});5088 git_print_header_div('commit',5089"<span class=\"age\">$co{'age_string'}</span>".5090 esc_html($co{'title'}) .$ref,5091$commit);5092print"<div class=\"title_text\">\n".5093"<div class=\"log_link\">\n".5094$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5095" | ".5096$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5097" | ".5098$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5099"<br/>\n".5100"</div>\n".5101"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".5102"</div>\n";51035104print"<div class=\"log_body\">\n";5105 git_print_log($co{'comment'}, -final_empty_line=>1);5106print"</div>\n";5107}5108if($#commitlist>=100) {5109print"<div class=\"page_nav\">\n";5110print$cgi->a({-href => href(-replay=>1, page=>$page+1),5111-accesskey =>"n", -title =>"Alt-n"},"next");5112print"</div>\n";5113}5114 git_footer_html();5115}51165117sub git_commit {5118$hash||=$hash_base||"HEAD";5119my%co= parse_commit($hash)5120or die_error(404,"Unknown commit object");5121my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5122my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});51235124my$parent=$co{'parent'};5125my$parents=$co{'parents'};# listref51265127# we need to prepare $formats_nav before any parameter munging5128my$formats_nav;5129if(!defined$parent) {5130# --root commitdiff5131$formats_nav.='(initial)';5132}elsif(@$parents==1) {5133# single parent commit5134$formats_nav.=5135'(parent: '.5136$cgi->a({-href => href(action=>"commit",5137 hash=>$parent)},5138 esc_html(substr($parent,0,7))) .5139')';5140}else{5141# merge commit5142$formats_nav.=5143'(merge: '.5144join(' ',map{5145$cgi->a({-href => href(action=>"commit",5146 hash=>$_)},5147 esc_html(substr($_,0,7)));5148}@$parents) .5149')';5150}5151if(gitweb_check_feature('patches')) {5152$formats_nav.=" | ".5153$cgi->a({-href => href(action=>"patch", -replay=>1)},5154"patch");5155}51565157if(!defined$parent) {5158$parent="--root";5159}5160my@difftree;5161open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5162@diff_opts,5163(@$parents<=1?$parent:'-c'),5164$hash,"--"5165or die_error(500,"Open git-diff-tree failed");5166@difftree=map{chomp;$_} <$fd>;5167close$fdor die_error(404,"Reading git-diff-tree failed");51685169# non-textual hash id's can be cached5170my$expires;5171if($hash=~m/^[0-9a-fA-F]{40}$/) {5172$expires="+1d";5173}5174my$refs= git_get_references();5175my$ref= format_ref_marker($refs,$co{'id'});51765177 git_header_html(undef,$expires);5178 git_print_page_nav('commit','',5179$hash,$co{'tree'},$hash,5180$formats_nav);51815182if(defined$co{'parent'}) {5183 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5184}else{5185 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5186}5187print"<div class=\"title_text\">\n".5188"<table class=\"object_header\">\n";5189print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5190"<tr>".5191"<td></td><td>$ad{'rfc2822'}";5192if($ad{'hour_local'} <6) {5193printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5194$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5195}else{5196printf(" (%02d:%02d%s)",5197$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5198}5199print"</td>".5200"</tr>\n";5201print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5202print"<tr><td></td><td>$cd{'rfc2822'}".5203sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5204"</td></tr>\n";5205print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5206print"<tr>".5207"<td>tree</td>".5208"<td class=\"sha1\">".5209$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5210class=>"list"},$co{'tree'}) .5211"</td>".5212"<td class=\"link\">".5213$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5214"tree");5215my$snapshot_links= format_snapshot_links($hash);5216if(defined$snapshot_links) {5217print" | ".$snapshot_links;5218}5219print"</td>".5220"</tr>\n";52215222foreachmy$par(@$parents) {5223print"<tr>".5224"<td>parent</td>".5225"<td class=\"sha1\">".5226$cgi->a({-href => href(action=>"commit", hash=>$par),5227class=>"list"},$par) .5228"</td>".5229"<td class=\"link\">".5230$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5231" | ".5232$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5233"</td>".5234"</tr>\n";5235}5236print"</table>".5237"</div>\n";52385239print"<div class=\"page_body\">\n";5240 git_print_log($co{'comment'});5241print"</div>\n";52425243 git_difftree_body(\@difftree,$hash,@$parents);52445245 git_footer_html();5246}52475248sub git_object {5249# object is defined by:5250# - hash or hash_base alone5251# - hash_base and file_name5252my$type;52535254# - hash or hash_base alone5255if($hash|| ($hash_base&& !defined$file_name)) {5256my$object_id=$hash||$hash_base;52575258open my$fd,"-|", quote_command(5259 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5260or die_error(404,"Object does not exist");5261$type= <$fd>;5262chomp$type;5263close$fd5264or die_error(404,"Object does not exist");52655266# - hash_base and file_name5267}elsif($hash_base&&defined$file_name) {5268$file_name=~ s,/+$,,;52695270system(git_cmd(),"cat-file",'-e',$hash_base) ==05271or die_error(404,"Base object does not exist");52725273# here errors should not hapen5274open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5275or die_error(500,"Open git-ls-tree failed");5276my$line= <$fd>;5277close$fd;52785279#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5280unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5281 die_error(404,"File or directory for given base does not exist");5282}5283$type=$2;5284$hash=$3;5285}else{5286 die_error(400,"Not enough information to find object");5287}52885289print$cgi->redirect(-uri => href(action=>$type, -full=>1,5290 hash=>$hash, hash_base=>$hash_base,5291 file_name=>$file_name),5292-status =>'302 Found');5293}52945295sub git_blobdiff {5296my$format=shift||'html';52975298my$fd;5299my@difftree;5300my%diffinfo;5301my$expires;53025303# preparing $fd and %diffinfo for git_patchset_body5304# new style URI5305if(defined$hash_base&&defined$hash_parent_base) {5306if(defined$file_name) {5307# read raw output5308open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5309$hash_parent_base,$hash_base,5310"--", (defined$file_parent?$file_parent: ()),$file_name5311or die_error(500,"Open git-diff-tree failed");5312@difftree=map{chomp;$_} <$fd>;5313close$fd5314or die_error(404,"Reading git-diff-tree failed");5315@difftree5316or die_error(404,"Blob diff not found");53175318}elsif(defined$hash&&5319$hash=~/[0-9a-fA-F]{40}/) {5320# try to find filename from $hash53215322# read filtered raw output5323open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5324$hash_parent_base,$hash_base,"--"5325or die_error(500,"Open git-diff-tree failed");5326@difftree=5327# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5328# $hash == to_id5329grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5330map{chomp;$_} <$fd>;5331close$fd5332or die_error(404,"Reading git-diff-tree failed");5333@difftree5334or die_error(404,"Blob diff not found");53355336}else{5337 die_error(400,"Missing one of the blob diff parameters");5338}53395340if(@difftree>1) {5341 die_error(400,"Ambiguous blob diff specification");5342}53435344%diffinfo= parse_difftree_raw_line($difftree[0]);5345$file_parent||=$diffinfo{'from_file'} ||$file_name;5346$file_name||=$diffinfo{'to_file'};53475348$hash_parent||=$diffinfo{'from_id'};5349$hash||=$diffinfo{'to_id'};53505351# non-textual hash id's can be cached5352if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5353$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5354$expires='+1d';5355}53565357# open patch output5358open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5359'-p', ($formateq'html'?"--full-index": ()),5360$hash_parent_base,$hash_base,5361"--", (defined$file_parent?$file_parent: ()),$file_name5362or die_error(500,"Open git-diff-tree failed");5363}53645365# old/legacy style URI -- not generated anymore since 1.4.3.5366if(!%diffinfo) {5367 die_error('404 Not Found',"Missing one of the blob diff parameters")5368}53695370# header5371if($formateq'html') {5372my$formats_nav=5373$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5374"raw");5375 git_header_html(undef,$expires);5376if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5377 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5378 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5379}else{5380print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5381print"<div class=\"title\">$hashvs$hash_parent</div>\n";5382}5383if(defined$file_name) {5384 git_print_page_path($file_name,"blob",$hash_base);5385}else{5386print"<div class=\"page_path\"></div>\n";5387}53885389}elsif($formateq'plain') {5390print$cgi->header(5391-type =>'text/plain',5392-charset =>'utf-8',5393-expires =>$expires,5394-content_disposition =>'inline; filename="'."$file_name".'.patch"');53955396print"X-Git-Url: ".$cgi->self_url() ."\n\n";53975398}else{5399 die_error(400,"Unknown blobdiff format");5400}54015402# patch5403if($formateq'html') {5404print"<div class=\"page_body\">\n";54055406 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5407close$fd;54085409print"</div>\n";# class="page_body"5410 git_footer_html();54115412}else{5413while(my$line= <$fd>) {5414$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5415$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;54165417print$line;54185419last if$line=~m!^\+\+\+!;5420}5421local$/=undef;5422print<$fd>;5423close$fd;5424}5425}54265427sub git_blobdiff_plain {5428 git_blobdiff('plain');5429}54305431sub git_commitdiff {5432my%params=@_;5433my$format=$params{-format} ||'html';54345435my($patch_max) = gitweb_get_feature('patches');5436if($formateq'patch') {5437 die_error(403,"Patch view not allowed")unless$patch_max;5438}54395440$hash||=$hash_base||"HEAD";5441my%co= parse_commit($hash)5442or die_error(404,"Unknown commit object");54435444# choose format for commitdiff for merge5445if(!defined$hash_parent&& @{$co{'parents'}} >1) {5446$hash_parent='--cc';5447}5448# we need to prepare $formats_nav before almost any parameter munging5449my$formats_nav;5450if($formateq'html') {5451$formats_nav=5452$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5453"raw");5454if($patch_max) {5455$formats_nav.=" | ".5456$cgi->a({-href => href(action=>"patch", -replay=>1)},5457"patch");5458}54595460if(defined$hash_parent&&5461$hash_parentne'-c'&&$hash_parentne'--cc') {5462# commitdiff with two commits given5463my$hash_parent_short=$hash_parent;5464if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5465$hash_parent_short=substr($hash_parent,0,7);5466}5467$formats_nav.=5468' (from';5469for(my$i=0;$i< @{$co{'parents'}};$i++) {5470if($co{'parents'}[$i]eq$hash_parent) {5471$formats_nav.=' parent '. ($i+1);5472last;5473}5474}5475$formats_nav.=': '.5476$cgi->a({-href => href(action=>"commitdiff",5477 hash=>$hash_parent)},5478 esc_html($hash_parent_short)) .5479')';5480}elsif(!$co{'parent'}) {5481# --root commitdiff5482$formats_nav.=' (initial)';5483}elsif(scalar@{$co{'parents'}} ==1) {5484# single parent commit5485$formats_nav.=5486' (parent: '.5487$cgi->a({-href => href(action=>"commitdiff",5488 hash=>$co{'parent'})},5489 esc_html(substr($co{'parent'},0,7))) .5490')';5491}else{5492# merge commit5493if($hash_parenteq'--cc') {5494$formats_nav.=' | '.5495$cgi->a({-href => href(action=>"commitdiff",5496 hash=>$hash, hash_parent=>'-c')},5497'combined');5498}else{# $hash_parent eq '-c'5499$formats_nav.=' | '.5500$cgi->a({-href => href(action=>"commitdiff",5501 hash=>$hash, hash_parent=>'--cc')},5502'compact');5503}5504$formats_nav.=5505' (merge: '.5506join(' ',map{5507$cgi->a({-href => href(action=>"commitdiff",5508 hash=>$_)},5509 esc_html(substr($_,0,7)));5510} @{$co{'parents'}} ) .5511')';5512}5513}55145515my$hash_parent_param=$hash_parent;5516if(!defined$hash_parent_param) {5517# --cc for multiple parents, --root for parentless5518$hash_parent_param=5519@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5520}55215522# read commitdiff5523my$fd;5524my@difftree;5525if($formateq'html') {5526open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5527"--no-commit-id","--patch-with-raw","--full-index",5528$hash_parent_param,$hash,"--"5529or die_error(500,"Open git-diff-tree failed");55305531while(my$line= <$fd>) {5532chomp$line;5533# empty line ends raw part of diff-tree output5534last unless$line;5535push@difftree,scalar parse_difftree_raw_line($line);5536}55375538}elsif($formateq'plain') {5539open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5540'-p',$hash_parent_param,$hash,"--"5541or die_error(500,"Open git-diff-tree failed");5542}elsif($formateq'patch') {5543# For commit ranges, we limit the output to the number of5544# patches specified in the 'patches' feature.5545# For single commits, we limit the output to a single patch,5546# diverging from the git-format-patch default.5547my@commit_spec= ();5548if($hash_parent) {5549if($patch_max>0) {5550push@commit_spec,"-$patch_max";5551}5552push@commit_spec,'-n',"$hash_parent..$hash";5553}else{5554if($params{-single}) {5555push@commit_spec,'-1';5556}else{5557if($patch_max>0) {5558push@commit_spec,"-$patch_max";5559}5560push@commit_spec,"-n";5561}5562push@commit_spec,'--root',$hash;5563}5564open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5565'--stdout',@commit_spec5566or die_error(500,"Open git-format-patch failed");5567}else{5568 die_error(400,"Unknown commitdiff format");5569}55705571# non-textual hash id's can be cached5572my$expires;5573if($hash=~m/^[0-9a-fA-F]{40}$/) {5574$expires="+1d";5575}55765577# write commit message5578if($formateq'html') {5579my$refs= git_get_references();5580my$ref= format_ref_marker($refs,$co{'id'});55815582 git_header_html(undef,$expires);5583 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5584 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5585 git_print_authorship(\%co);5586print"<div class=\"page_body\">\n";5587if(@{$co{'comment'}} >1) {5588print"<div class=\"log\">\n";5589 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5590print"</div>\n";# class="log"5591}55925593}elsif($formateq'plain') {5594my$refs= git_get_references("tags");5595my$tagname= git_get_rev_name_tags($hash);5596my$filename= basename($project) ."-$hash.patch";55975598print$cgi->header(5599-type =>'text/plain',5600-charset =>'utf-8',5601-expires =>$expires,5602-content_disposition =>'inline; filename="'."$filename".'"');5603my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5604print"From: ". to_utf8($co{'author'}) ."\n";5605print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5606print"Subject: ". to_utf8($co{'title'}) ."\n";56075608print"X-Git-Tag:$tagname\n"if$tagname;5609print"X-Git-Url: ".$cgi->self_url() ."\n\n";56105611foreachmy$line(@{$co{'comment'}}) {5612print to_utf8($line) ."\n";5613}5614print"---\n\n";5615}elsif($formateq'patch') {5616my$filename= basename($project) ."-$hash.patch";56175618print$cgi->header(5619-type =>'text/plain',5620-charset =>'utf-8',5621-expires =>$expires,5622-content_disposition =>'inline; filename="'."$filename".'"');5623}56245625# write patch5626if($formateq'html') {5627my$use_parents= !defined$hash_parent||5628$hash_parenteq'-c'||$hash_parenteq'--cc';5629 git_difftree_body(\@difftree,$hash,5630$use_parents? @{$co{'parents'}} :$hash_parent);5631print"<br/>\n";56325633 git_patchset_body($fd, \@difftree,$hash,5634$use_parents? @{$co{'parents'}} :$hash_parent);5635close$fd;5636print"</div>\n";# class="page_body"5637 git_footer_html();56385639}elsif($formateq'plain') {5640local$/=undef;5641print<$fd>;5642close$fd5643or print"Reading git-diff-tree failed\n";5644}elsif($formateq'patch') {5645local$/=undef;5646print<$fd>;5647close$fd5648or print"Reading git-format-patch failed\n";5649}5650}56515652sub git_commitdiff_plain {5653 git_commitdiff(-format =>'plain');5654}56555656# format-patch-style patches5657sub git_patch {5658 git_commitdiff(-format =>'patch', -single=>1);5659}56605661sub git_patches {5662 git_commitdiff(-format =>'patch');5663}56645665sub git_history {5666if(!defined$hash_base) {5667$hash_base= git_get_head_hash($project);5668}5669if(!defined$page) {5670$page=0;5671}5672my$ftype;5673my%co= parse_commit($hash_base)5674or die_error(404,"Unknown commit object");56755676my$refs= git_get_references();5677my$limit=sprintf("--max-count=%i", (100* ($page+1)));56785679my@commitlist= parse_commits($hash_base,101, (100*$page),5680$file_name,"--full-history")5681or die_error(404,"No such file or directory on given branch");56825683if(!defined$hash&&defined$file_name) {5684# some commits could have deleted file in question,5685# and not have it in tree, but one of them has to have it5686for(my$i=0;$i<=@commitlist;$i++) {5687$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5688last ifdefined$hash;5689}5690}5691if(defined$hash) {5692$ftype= git_get_type($hash);5693}5694if(!defined$ftype) {5695 die_error(500,"Unknown type of object");5696}56975698my$paging_nav='';5699if($page>0) {5700$paging_nav.=5701$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5702 file_name=>$file_name)},5703"first");5704$paging_nav.=" ⋅ ".5705$cgi->a({-href => href(-replay=>1, page=>$page-1),5706-accesskey =>"p", -title =>"Alt-p"},"prev");5707}else{5708$paging_nav.="first";5709$paging_nav.=" ⋅ prev";5710}5711my$next_link='';5712if($#commitlist>=100) {5713$next_link=5714$cgi->a({-href => href(-replay=>1, page=>$page+1),5715-accesskey =>"n", -title =>"Alt-n"},"next");5716$paging_nav.=" ⋅$next_link";5717}else{5718$paging_nav.=" ⋅ next";5719}57205721 git_header_html();5722 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5723 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5724 git_print_page_path($file_name,$ftype,$hash_base);57255726 git_history_body(\@commitlist,0,99,5727$refs,$hash_base,$ftype,$next_link);57285729 git_footer_html();5730}57315732sub git_search {5733 gitweb_check_feature('search')or die_error(403,"Search is disabled");5734if(!defined$searchtext) {5735 die_error(400,"Text field is empty");5736}5737if(!defined$hash) {5738$hash= git_get_head_hash($project);5739}5740my%co= parse_commit($hash);5741if(!%co) {5742 die_error(404,"Unknown commit object");5743}5744if(!defined$page) {5745$page=0;5746}57475748$searchtype||='commit';5749if($searchtypeeq'pickaxe') {5750# pickaxe may take all resources of your box and run for several minutes5751# with every query - so decide by yourself how public you make this feature5752 gitweb_check_feature('pickaxe')5753or die_error(403,"Pickaxe is disabled");5754}5755if($searchtypeeq'grep') {5756 gitweb_check_feature('grep')5757or die_error(403,"Grep is disabled");5758}57595760 git_header_html();57615762if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5763my$greptype;5764if($searchtypeeq'commit') {5765$greptype="--grep=";5766}elsif($searchtypeeq'author') {5767$greptype="--author=";5768}elsif($searchtypeeq'committer') {5769$greptype="--committer=";5770}5771$greptype.=$searchtext;5772my@commitlist= parse_commits($hash,101, (100*$page),undef,5773$greptype,'--regexp-ignore-case',5774$search_use_regexp?'--extended-regexp':'--fixed-strings');57755776my$paging_nav='';5777if($page>0) {5778$paging_nav.=5779$cgi->a({-href => href(action=>"search", hash=>$hash,5780 searchtext=>$searchtext,5781 searchtype=>$searchtype)},5782"first");5783$paging_nav.=" ⋅ ".5784$cgi->a({-href => href(-replay=>1, page=>$page-1),5785-accesskey =>"p", -title =>"Alt-p"},"prev");5786}else{5787$paging_nav.="first";5788$paging_nav.=" ⋅ prev";5789}5790my$next_link='';5791if($#commitlist>=100) {5792$next_link=5793$cgi->a({-href => href(-replay=>1, page=>$page+1),5794-accesskey =>"n", -title =>"Alt-n"},"next");5795$paging_nav.=" ⋅$next_link";5796}else{5797$paging_nav.=" ⋅ next";5798}57995800if($#commitlist>=100) {5801}58025803 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5804 git_print_header_div('commit', esc_html($co{'title'}),$hash);5805 git_search_grep_body(\@commitlist,0,99,$next_link);5806}58075808if($searchtypeeq'pickaxe') {5809 git_print_page_nav('','',$hash,$co{'tree'},$hash);5810 git_print_header_div('commit', esc_html($co{'title'}),$hash);58115812print"<table class=\"pickaxe search\">\n";5813my$alternate=1;5814$/="\n";5815open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5816'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5817($search_use_regexp?'--pickaxe-regex': ());5818undef%co;5819my@files;5820while(my$line= <$fd>) {5821chomp$line;5822next unless$line;58235824my%set= parse_difftree_raw_line($line);5825if(defined$set{'commit'}) {5826# finish previous commit5827if(%co) {5828print"</td>\n".5829"<td class=\"link\">".5830$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5831" | ".5832$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5833print"</td>\n".5834"</tr>\n";5835}58365837if($alternate) {5838print"<tr class=\"dark\">\n";5839}else{5840print"<tr class=\"light\">\n";5841}5842$alternate^=1;5843%co= parse_commit($set{'commit'});5844my$author= chop_and_escape_str($co{'author_name'},15,5);5845print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5846"<td><i>$author</i></td>\n".5847"<td>".5848$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5849-class=>"list subject"},5850 chop_and_escape_str($co{'title'},50) ."<br/>");5851}elsif(defined$set{'to_id'}) {5852next if($set{'to_id'} =~m/^0{40}$/);58535854print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5855 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5856-class=>"list"},5857"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5858"<br/>\n";5859}5860}5861close$fd;58625863# finish last commit (warning: repetition!)5864if(%co) {5865print"</td>\n".5866"<td class=\"link\">".5867$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5868" | ".5869$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5870print"</td>\n".5871"</tr>\n";5872}58735874print"</table>\n";5875}58765877if($searchtypeeq'grep') {5878 git_print_page_nav('','',$hash,$co{'tree'},$hash);5879 git_print_header_div('commit', esc_html($co{'title'}),$hash);58805881print"<table class=\"grep_search\">\n";5882my$alternate=1;5883my$matches=0;5884$/="\n";5885open my$fd,"-|", git_cmd(),'grep','-n',5886$search_use_regexp? ('-E','-i') :'-F',5887$searchtext,$co{'tree'};5888my$lastfile='';5889while(my$line= <$fd>) {5890chomp$line;5891my($file,$lno,$ltext,$binary);5892last if($matches++>1000);5893if($line=~/^Binary file (.+) matches$/) {5894$file=$1;5895$binary=1;5896}else{5897(undef,$file,$lno,$ltext) =split(/:/,$line,4);5898}5899if($filene$lastfile) {5900$lastfileand print"</td></tr>\n";5901if($alternate++) {5902print"<tr class=\"dark\">\n";5903}else{5904print"<tr class=\"light\">\n";5905}5906print"<td class=\"list\">".5907$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5908 file_name=>"$file"),5909-class=>"list"}, esc_path($file));5910print"</td><td>\n";5911$lastfile=$file;5912}5913if($binary) {5914print"<div class=\"binary\">Binary file</div>\n";5915}else{5916$ltext= untabify($ltext);5917if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5918$ltext= esc_html($1, -nbsp=>1);5919$ltext.='<span class="match">';5920$ltext.= esc_html($2, -nbsp=>1);5921$ltext.='</span>';5922$ltext.= esc_html($3, -nbsp=>1);5923}else{5924$ltext= esc_html($ltext, -nbsp=>1);5925}5926print"<div class=\"pre\">".5927$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5928 file_name=>"$file").'#l'.$lno,5929-class=>"linenr"},sprintf('%4i',$lno))5930.' '.$ltext."</div>\n";5931}5932}5933if($lastfile) {5934print"</td></tr>\n";5935if($matches>1000) {5936print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5937}5938}else{5939print"<div class=\"diff nodifferences\">No matches found</div>\n";5940}5941close$fd;59425943print"</table>\n";5944}5945 git_footer_html();5946}59475948sub git_search_help {5949 git_header_html();5950 git_print_page_nav('','',$hash,$hash,$hash);5951print<<EOT;5952<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5953regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5954the pattern entered is recognized as the POSIX extended5955<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5956insensitive).</p>5957<dl>5958<dt><b>commit</b></dt>5959<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5960EOT5961my$have_grep= gitweb_check_feature('grep');5962if($have_grep) {5963print<<EOT;5964<dt><b>grep</b></dt>5965<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5966 a different one) are searched for the given pattern. On large trees, this search can take5967a while and put some strain on the server, so please use it with some consideration. Note that5968due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5969case-sensitive.</dd>5970EOT5971}5972print<<EOT;5973<dt><b>author</b></dt>5974<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5975<dt><b>committer</b></dt>5976<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5977EOT5978my$have_pickaxe= gitweb_check_feature('pickaxe');5979if($have_pickaxe) {5980print<<EOT;5981<dt><b>pickaxe</b></dt>5982<dd>All commits that caused the string to appear or disappear from any file (changes that5983added, removed or "modified" the string) will be listed. This search can take a while and5984takes a lot of strain on the server, so please use it wisely. Note that since you may be5985interested even in changes just changing the case as well, this search is case sensitive.</dd>5986EOT5987}5988print"</dl>\n";5989 git_footer_html();5990}59915992sub git_shortlog {5993my$head= git_get_head_hash($project);5994if(!defined$hash) {5995$hash=$head;5996}5997if(!defined$page) {5998$page=0;5999}6000my$refs= git_get_references();60016002my$commit_hash=$hash;6003if(defined$hash_parent) {6004$commit_hash="$hash_parent..$hash";6005}6006my@commitlist= parse_commits($commit_hash,101, (100*$page));60076008my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6009my$next_link='';6010if($#commitlist>=100) {6011$next_link=6012$cgi->a({-href => href(-replay=>1, page=>$page+1),6013-accesskey =>"n", -title =>"Alt-n"},"next");6014}6015my$patch_max= gitweb_check_feature('patches');6016if($patch_max) {6017if($patch_max<0||@commitlist<=$patch_max) {6018$paging_nav.=" ⋅ ".6019$cgi->a({-href => href(action=>"patches", -replay=>1)},6020"patches");6021}6022}60236024 git_header_html();6025 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6026 git_print_header_div('summary',$project);60276028 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);60296030 git_footer_html();6031}60326033## ......................................................................6034## feeds (RSS, Atom; OPML)60356036sub git_feed {6037my$format=shift||'atom';6038my$have_blame= gitweb_check_feature('blame');60396040# Atom: http://www.atomenabled.org/developers/syndication/6041# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6042if($formatne'rss'&&$formatne'atom') {6043 die_error(400,"Unknown web feed format");6044}60456046# log/feed of current (HEAD) branch, log of given branch, history of file/directory6047my$head=$hash||'HEAD';6048my@commitlist= parse_commits($head,150,0,$file_name);60496050my%latest_commit;6051my%latest_date;6052my$content_type="application/$format+xml";6053if(defined$cgi->http('HTTP_ACCEPT') &&6054$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6055# browser (feed reader) prefers text/xml6056$content_type='text/xml';6057}6058if(defined($commitlist[0])) {6059%latest_commit= %{$commitlist[0]};6060my$latest_epoch=$latest_commit{'committer_epoch'};6061%latest_date= parse_date($latest_epoch);6062my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6063if(defined$if_modified) {6064my$since;6065if(eval{require HTTP::Date;1; }) {6066$since= HTTP::Date::str2time($if_modified);6067}elsif(eval{require Time::ParseDate;1; }) {6068$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6069}6070if(defined$since&&$latest_epoch<=$since) {6071print$cgi->header(6072-type =>$content_type,6073-charset =>'utf-8',6074-last_modified =>$latest_date{'rfc2822'},6075-status =>'304 Not Modified');6076return;6077}6078}6079print$cgi->header(6080-type =>$content_type,6081-charset =>'utf-8',6082-last_modified =>$latest_date{'rfc2822'});6083}else{6084print$cgi->header(6085-type =>$content_type,6086-charset =>'utf-8');6087}60886089# Optimization: skip generating the body if client asks only6090# for Last-Modified date.6091return if($cgi->request_method()eq'HEAD');60926093# header variables6094my$title="$site_name-$project/$action";6095my$feed_type='log';6096if(defined$hash) {6097$title.=" - '$hash'";6098$feed_type='branch log';6099if(defined$file_name) {6100$title.=" ::$file_name";6101$feed_type='history';6102}6103}elsif(defined$file_name) {6104$title.=" -$file_name";6105$feed_type='history';6106}6107$title.="$feed_type";6108my$descr= git_get_project_description($project);6109if(defined$descr) {6110$descr= esc_html($descr);6111}else{6112$descr="$project".6113($formateq'rss'?'RSS':'Atom') .6114" feed";6115}6116my$owner= git_get_project_owner($project);6117$owner= esc_html($owner);61186119#header6120my$alt_url;6121if(defined$file_name) {6122$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6123}elsif(defined$hash) {6124$alt_url= href(-full=>1, action=>"log", hash=>$hash);6125}else{6126$alt_url= href(-full=>1, action=>"summary");6127}6128print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6129if($formateq'rss') {6130print<<XML;6131<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6132<channel>6133XML6134print"<title>$title</title>\n".6135"<link>$alt_url</link>\n".6136"<description>$descr</description>\n".6137"<language>en</language>\n".6138# project owner is responsible for 'editorial' content6139"<managingEditor>$owner</managingEditor>\n";6140if(defined$logo||defined$favicon) {6141# prefer the logo to the favicon, since RSS6142# doesn't allow both6143my$img= esc_url($logo||$favicon);6144print"<image>\n".6145"<url>$img</url>\n".6146"<title>$title</title>\n".6147"<link>$alt_url</link>\n".6148"</image>\n";6149}6150if(%latest_date) {6151print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6152print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6153}6154print"<generator>gitweb v.$version/$git_version</generator>\n";6155}elsif($formateq'atom') {6156print<<XML;6157<feed xmlns="http://www.w3.org/2005/Atom">6158XML6159print"<title>$title</title>\n".6160"<subtitle>$descr</subtitle>\n".6161'<link rel="alternate" type="text/html" href="'.6162$alt_url.'" />'."\n".6163'<link rel="self" type="'.$content_type.'" href="'.6164$cgi->self_url() .'" />'."\n".6165"<id>". href(-full=>1) ."</id>\n".6166# use project owner for feed author6167"<author><name>$owner</name></author>\n";6168if(defined$favicon) {6169print"<icon>". esc_url($favicon) ."</icon>\n";6170}6171if(defined$logo_url) {6172# not twice as wide as tall: 72 x 27 pixels6173print"<logo>". esc_url($logo) ."</logo>\n";6174}6175if(!%latest_date) {6176# dummy date to keep the feed valid until commits trickle in:6177print"<updated>1970-01-01T00:00:00Z</updated>\n";6178}else{6179print"<updated>$latest_date{'iso-8601'}</updated>\n";6180}6181print"<generator version='$version/$git_version'>gitweb</generator>\n";6182}61836184# contents6185for(my$i=0;$i<=$#commitlist;$i++) {6186my%co= %{$commitlist[$i]};6187my$commit=$co{'id'};6188# we read 150, we always show 30 and the ones more recent than 48 hours6189if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6190last;6191}6192my%cd= parse_date($co{'author_epoch'});61936194# get list of changed files6195open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6196$co{'parent'} ||"--root",6197$co{'id'},"--", (defined$file_name?$file_name: ())6198ornext;6199my@difftree=map{chomp;$_} <$fd>;6200close$fd6201ornext;62026203# print element (entry, item)6204my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6205if($formateq'rss') {6206print"<item>\n".6207"<title>". esc_html($co{'title'}) ."</title>\n".6208"<author>". esc_html($co{'author'}) ."</author>\n".6209"<pubDate>$cd{'rfc2822'}</pubDate>\n".6210"<guid isPermaLink=\"true\">$co_url</guid>\n".6211"<link>$co_url</link>\n".6212"<description>". esc_html($co{'title'}) ."</description>\n".6213"<content:encoded>".6214"<![CDATA[\n";6215}elsif($formateq'atom') {6216print"<entry>\n".6217"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6218"<updated>$cd{'iso-8601'}</updated>\n".6219"<author>\n".6220" <name>". esc_html($co{'author_name'}) ."</name>\n";6221if($co{'author_email'}) {6222print" <email>". esc_html($co{'author_email'}) ."</email>\n";6223}6224print"</author>\n".6225# use committer for contributor6226"<contributor>\n".6227" <name>". esc_html($co{'committer_name'}) ."</name>\n";6228if($co{'committer_email'}) {6229print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6230}6231print"</contributor>\n".6232"<published>$cd{'iso-8601'}</published>\n".6233"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6234"<id>$co_url</id>\n".6235"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6236"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6237}6238my$comment=$co{'comment'};6239print"<pre>\n";6240foreachmy$line(@$comment) {6241$line= esc_html($line);6242print"$line\n";6243}6244print"</pre><ul>\n";6245foreachmy$difftree_line(@difftree) {6246my%difftree= parse_difftree_raw_line($difftree_line);6247next if!$difftree{'from_id'};62486249my$file=$difftree{'file'} ||$difftree{'to_file'};62506251print"<li>".6252"[".6253$cgi->a({-href => href(-full=>1, action=>"blobdiff",6254 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6255 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6256 file_name=>$file, file_parent=>$difftree{'from_file'}),6257-title =>"diff"},'D');6258if($have_blame) {6259print$cgi->a({-href => href(-full=>1, action=>"blame",6260 file_name=>$file, hash_base=>$commit),6261-title =>"blame"},'B');6262}6263# if this is not a feed of a file history6264if(!defined$file_name||$file_namene$file) {6265print$cgi->a({-href => href(-full=>1, action=>"history",6266 file_name=>$file, hash=>$commit),6267-title =>"history"},'H');6268}6269$file= esc_path($file);6270print"] ".6271"$file</li>\n";6272}6273if($formateq'rss') {6274print"</ul>]]>\n".6275"</content:encoded>\n".6276"</item>\n";6277}elsif($formateq'atom') {6278print"</ul>\n</div>\n".6279"</content>\n".6280"</entry>\n";6281}6282}62836284# end of feed6285if($formateq'rss') {6286print"</channel>\n</rss>\n";6287}elsif($formateq'atom') {6288print"</feed>\n";6289}6290}62916292sub git_rss {6293 git_feed('rss');6294}62956296sub git_atom {6297 git_feed('atom');6298}62996300sub git_opml {6301my@list= git_get_projects_list();63026303print$cgi->header(6304-type =>'text/xml',6305-charset =>'utf-8',6306-content_disposition =>'inline; filename="opml.xml"');63076308print<<XML;6309<?xml version="1.0" encoding="utf-8"?>6310<opml version="1.0">6311<head>6312 <title>$site_nameOPML Export</title>6313</head>6314<body>6315<outline text="git RSS feeds">6316XML63176318foreachmy$pr(@list) {6319my%proj=%$pr;6320my$head= git_get_head_hash($proj{'path'});6321if(!defined$head) {6322next;6323}6324$git_dir="$projectroot/$proj{'path'}";6325my%co= parse_commit($head);6326if(!%co) {6327next;6328}63296330my$path= esc_html(chop_str($proj{'path'},25,5));6331my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6332my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6333print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6334}6335print<<XML;6336</outline>6337</body>6338</opml>6339XML6340}