1#!/usr/bin/perl 2 3# gitweb - simple web interface to track changes in git repositories 4# 5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> 6# (C) 2005, Christian Gierke 7# 8# This program is licensed under the GPLv2 9 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21BEGIN{ 22 CGI->compile()if$ENV{'MOD_PERL'}; 23} 24 25our$cgi= new CGI; 26our$version="++GIT_VERSION++"; 27our$my_url=$cgi->url(); 28our$my_uri=$cgi->url(-absolute =>1); 29 30# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 31# needed and used only for URLs with nonempty PATH_INFO 32our$base_url=$my_url; 33 34# When the script is used as DirectoryIndex, the URL does not contain the name 35# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 36# have to do it ourselves. We make $path_info global because it's also used 37# later on. 38# 39# Another issue with the script being the DirectoryIndex is that the resulting 40# $my_url data is not the full script URL: this is good, because we want 41# generated links to keep implying the script name if it wasn't explicitly 42# indicated in the URL we're handling, but it means that $my_url cannot be used 43# as base URL. 44# Therefore, if we needed to strip PATH_INFO, then we know that we have 45# to build the base URL ourselves: 46our$path_info=$ENV{"PATH_INFO"}; 47if($path_info) { 48if($my_url=~ s,\Q$path_info\E$,, && 49$my_uri=~ s,\Q$path_info\E$,, && 50defined$ENV{'SCRIPT_NAME'}) { 51$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 52} 53} 54 55# core git executable to use 56# this can just be "git" if your webserver has a sensible PATH 57our$GIT="++GIT_BINDIR++/git"; 58 59# absolute fs-path which will be prepended to the project path 60#our $projectroot = "/pub/scm"; 61our$projectroot="++GITWEB_PROJECTROOT++"; 62 63# fs traversing limit for getting project list 64# the number is relative to the projectroot 65our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 66 67# target of the home link on top of all pages 68our$home_link=$my_uri||"/"; 69 70# string of the home link on top of all pages 71our$home_link_str="++GITWEB_HOME_LINK_STR++"; 72 73# name of your site or organization to appear in page titles 74# replace this with something more descriptive for clearer bookmarks 75our$site_name="++GITWEB_SITENAME++" 76|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 77 78# filename of html text to include at top of each page 79our$site_header="++GITWEB_SITE_HEADER++"; 80# html text to include at home page 81our$home_text="++GITWEB_HOMETEXT++"; 82# filename of html text to include at bottom of each page 83our$site_footer="++GITWEB_SITE_FOOTER++"; 84 85# URI of stylesheets 86our@stylesheets= ("++GITWEB_CSS++"); 87# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 88our$stylesheet=undef; 89# URI of GIT logo (72x27 size) 90our$logo="++GITWEB_LOGO++"; 91# URI of GIT favicon, assumed to be image/png type 92our$favicon="++GITWEB_FAVICON++"; 93 94# URI and label (title) of GIT logo link 95#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 96#our $logo_label = "git documentation"; 97our$logo_url="http://git.or.cz/"; 98our$logo_label="git homepage"; 99 100# source of projects list 101our$projects_list="++GITWEB_LIST++"; 102 103# the width (in characters) of the projects list "Description" column 104our$projects_list_description_width=25; 105 106# default order of projects list 107# valid values are none, project, descr, owner, and age 108our$default_projects_order="project"; 109 110# show repository only if this file exists 111# (only effective if this variable evaluates to true) 112our$export_ok="++GITWEB_EXPORT_OK++"; 113 114# show repository only if this subroutine returns true 115# when given the path to the project, for example: 116# sub { return -e "$_[0]/git-daemon-export-ok"; } 117our$export_auth_hook=undef; 118 119# only allow viewing of repositories also shown on the overview page 120our$strict_export="++GITWEB_STRICT_EXPORT++"; 121 122# list of git base URLs used for URL to where fetch project from, 123# i.e. full URL is "$git_base_url/$project" 124our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 125 126# default blob_plain mimetype and default charset for text/plain blob 127our$default_blob_plain_mimetype='text/plain'; 128our$default_text_plain_charset=undef; 129 130# file to use for guessing MIME types before trying /etc/mime.types 131# (relative to the current git repository) 132our$mimetypes_file=undef; 133 134# assume this charset if line contains non-UTF-8 characters; 135# it should be valid encoding (see Encoding::Supported(3pm) for list), 136# for which encoding all byte sequences are valid, for example 137# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 138# could be even 'utf-8' for the old behavior) 139our$fallback_encoding='latin1'; 140 141# rename detection options for git-diff and git-diff-tree 142# - default is '-M', with the cost proportional to 143# (number of removed files) * (number of new files). 144# - more costly is '-C' (which implies '-M'), with the cost proportional to 145# (number of changed files + number of removed files) * (number of new files) 146# - even more costly is '-C', '--find-copies-harder' with cost 147# (number of files in the original tree) * (number of new files) 148# - one might want to include '-B' option, e.g. '-B', '-M' 149our@diff_opts= ('-M');# taken from git_commit 150 151# Disables features that would allow repository owners to inject script into 152# the gitweb domain. 153our$prevent_xss=0; 154 155# information about snapshot formats that gitweb is capable of serving 156our%known_snapshot_formats= ( 157# name => { 158# 'display' => display name, 159# 'type' => mime type, 160# 'suffix' => filename suffix, 161# 'format' => --format for git-archive, 162# 'compressor' => [compressor command and arguments] 163# (array reference, optional)} 164# 165'tgz'=> { 166'display'=>'tar.gz', 167'type'=>'application/x-gzip', 168'suffix'=>'.tar.gz', 169'format'=>'tar', 170'compressor'=> ['gzip']}, 171 172'tbz2'=> { 173'display'=>'tar.bz2', 174'type'=>'application/x-bzip2', 175'suffix'=>'.tar.bz2', 176'format'=>'tar', 177'compressor'=> ['bzip2']}, 178 179'zip'=> { 180'display'=>'zip', 181'type'=>'application/x-zip', 182'suffix'=>'.zip', 183'format'=>'zip'}, 184); 185 186# Aliases so we understand old gitweb.snapshot values in repository 187# configuration. 188our%known_snapshot_format_aliases= ( 189'gzip'=>'tgz', 190'bzip2'=>'tbz2', 191 192# backward compatibility: legacy gitweb config support 193'x-gzip'=>undef,'gz'=>undef, 194'x-bzip2'=>undef,'bz2'=>undef, 195'x-zip'=>undef,''=>undef, 196); 197 198# You define site-wide feature defaults here; override them with 199# $GITWEB_CONFIG as necessary. 200our%feature= ( 201# feature => { 202# 'sub' => feature-sub (subroutine), 203# 'override' => allow-override (boolean), 204# 'default' => [ default options...] (array reference)} 205# 206# if feature is overridable (it means that allow-override has true value), 207# then feature-sub will be called with default options as parameters; 208# return value of feature-sub indicates if to enable specified feature 209# 210# if there is no 'sub' key (no feature-sub), then feature cannot be 211# overriden 212# 213# use gitweb_get_feature(<feature>) to retrieve the <feature> value 214# (an array) or gitweb_check_feature(<feature>) to check if <feature> 215# is enabled 216 217# Enable the 'blame' blob view, showing the last commit that modified 218# each line in the file. This can be very CPU-intensive. 219 220# To enable system wide have in $GITWEB_CONFIG 221# $feature{'blame'}{'default'} = [1]; 222# To have project specific config enable override in $GITWEB_CONFIG 223# $feature{'blame'}{'override'} = 1; 224# and in project config gitweb.blame = 0|1; 225'blame'=> { 226'sub'=>sub{ feature_bool('blame',@_) }, 227'override'=>0, 228'default'=> [0]}, 229 230# Enable the 'snapshot' link, providing a compressed archive of any 231# tree. This can potentially generate high traffic if you have large 232# project. 233 234# Value is a list of formats defined in %known_snapshot_formats that 235# you wish to offer. 236# To disable system wide have in $GITWEB_CONFIG 237# $feature{'snapshot'}{'default'} = []; 238# To have project specific config enable override in $GITWEB_CONFIG 239# $feature{'snapshot'}{'override'} = 1; 240# and in project config, a comma-separated list of formats or "none" 241# to disable. Example: gitweb.snapshot = tbz2,zip; 242'snapshot'=> { 243'sub'=> \&feature_snapshot, 244'override'=>0, 245'default'=> ['tgz']}, 246 247# Enable text search, which will list the commits which match author, 248# committer or commit text to a given string. Enabled by default. 249# Project specific override is not supported. 250'search'=> { 251'override'=>0, 252'default'=> [1]}, 253 254# Enable grep search, which will list the files in currently selected 255# tree containing the given string. Enabled by default. This can be 256# potentially CPU-intensive, of course. 257 258# To enable system wide have in $GITWEB_CONFIG 259# $feature{'grep'}{'default'} = [1]; 260# To have project specific config enable override in $GITWEB_CONFIG 261# $feature{'grep'}{'override'} = 1; 262# and in project config gitweb.grep = 0|1; 263'grep'=> { 264'sub'=>sub{ feature_bool('grep',@_) }, 265'override'=>0, 266'default'=> [1]}, 267 268# Enable the pickaxe search, which will list the commits that modified 269# a given string in a file. This can be practical and quite faster 270# alternative to 'blame', but still potentially CPU-intensive. 271 272# To enable system wide have in $GITWEB_CONFIG 273# $feature{'pickaxe'}{'default'} = [1]; 274# To have project specific config enable override in $GITWEB_CONFIG 275# $feature{'pickaxe'}{'override'} = 1; 276# and in project config gitweb.pickaxe = 0|1; 277'pickaxe'=> { 278'sub'=>sub{ feature_bool('pickaxe',@_) }, 279'override'=>0, 280'default'=> [1]}, 281 282# Make gitweb use an alternative format of the URLs which can be 283# more readable and natural-looking: project name is embedded 284# directly in the path and the query string contains other 285# auxiliary information. All gitweb installations recognize 286# URL in either format; this configures in which formats gitweb 287# generates links. 288 289# To enable system wide have in $GITWEB_CONFIG 290# $feature{'pathinfo'}{'default'} = [1]; 291# Project specific override is not supported. 292 293# Note that you will need to change the default location of CSS, 294# favicon, logo and possibly other files to an absolute URL. Also, 295# if gitweb.cgi serves as your indexfile, you will need to force 296# $my_uri to contain the script name in your $GITWEB_CONFIG. 297'pathinfo'=> { 298'override'=>0, 299'default'=> [0]}, 300 301# Make gitweb consider projects in project root subdirectories 302# to be forks of existing projects. Given project $projname.git, 303# projects matching $projname/*.git will not be shown in the main 304# projects list, instead a '+' mark will be added to $projname 305# there and a 'forks' view will be enabled for the project, listing 306# all the forks. If project list is taken from a file, forks have 307# to be listed after the main project. 308 309# To enable system wide have in $GITWEB_CONFIG 310# $feature{'forks'}{'default'} = [1]; 311# Project specific override is not supported. 312'forks'=> { 313'override'=>0, 314'default'=> [0]}, 315 316# Insert custom links to the action bar of all project pages. 317# This enables you mainly to link to third-party scripts integrating 318# into gitweb; e.g. git-browser for graphical history representation 319# or custom web-based repository administration interface. 320 321# The 'default' value consists of a list of triplets in the form 322# (label, link, position) where position is the label after which 323# to insert the link and link is a format string where %n expands 324# to the project name, %f to the project path within the filesystem, 325# %h to the current hash (h gitweb parameter) and %b to the current 326# hash base (hb gitweb parameter); %% expands to %. 327 328# To enable system wide have in $GITWEB_CONFIG e.g. 329# $feature{'actions'}{'default'} = [('graphiclog', 330# '/git-browser/by-commit.html?r=%n', 'summary')]; 331# Project specific override is not supported. 332'actions'=> { 333'override'=>0, 334'default'=> []}, 335 336# Allow gitweb scan project content tags described in ctags/ 337# of project repository, and display the popular Web 2.0-ish 338# "tag cloud" near the project list. Note that this is something 339# COMPLETELY different from the normal Git tags. 340 341# gitweb by itself can show existing tags, but it does not handle 342# tagging itself; you need an external application for that. 343# For an example script, check Girocco's cgi/tagproj.cgi. 344# You may want to install the HTML::TagCloud Perl module to get 345# a pretty tag cloud instead of just a list of tags. 346 347# To enable system wide have in $GITWEB_CONFIG 348# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 349# Project specific override is not supported. 350'ctags'=> { 351'override'=>0, 352'default'=> [0]}, 353 354# The maximum number of patches in a patchset generated in patch 355# view. Set this to 0 or undef to disable patch view, or to a 356# negative number to remove any limit. 357 358# To disable system wide have in $GITWEB_CONFIG 359# $feature{'patches'}{'default'} = [0]; 360# To have project specific config enable override in $GITWEB_CONFIG 361# $feature{'patches'}{'override'} = 1; 362# and in project config gitweb.patches = 0|n; 363# where n is the maximum number of patches allowed in a patchset. 364'patches'=> { 365'sub'=> \&feature_patches, 366'override'=>0, 367'default'=> [16]}, 368); 369 370sub gitweb_get_feature { 371my($name) =@_; 372return unlessexists$feature{$name}; 373my($sub,$override,@defaults) = ( 374$feature{$name}{'sub'}, 375$feature{$name}{'override'}, 376@{$feature{$name}{'default'}}); 377if(!$override) {return@defaults; } 378if(!defined$sub) { 379warn"feature$nameis not overrideable"; 380return@defaults; 381} 382return$sub->(@defaults); 383} 384 385# A wrapper to check if a given feature is enabled. 386# With this, you can say 387# 388# my $bool_feat = gitweb_check_feature('bool_feat'); 389# gitweb_check_feature('bool_feat') or somecode; 390# 391# instead of 392# 393# my ($bool_feat) = gitweb_get_feature('bool_feat'); 394# (gitweb_get_feature('bool_feat'))[0] or somecode; 395# 396sub gitweb_check_feature { 397return(gitweb_get_feature(@_))[0]; 398} 399 400 401sub feature_bool { 402my$key=shift; 403my($val) = git_get_project_config($key,'--bool'); 404 405if(!defined$val) { 406return($_[0]); 407}elsif($valeq'true') { 408return(1); 409}elsif($valeq'false') { 410return(0); 411} 412} 413 414sub feature_snapshot { 415my(@fmts) =@_; 416 417my($val) = git_get_project_config('snapshot'); 418 419if($val) { 420@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 421} 422 423return@fmts; 424} 425 426sub feature_patches { 427my@val= (git_get_project_config('patches','--int')); 428 429if(@val) { 430return@val; 431} 432 433return($_[0]); 434} 435 436# checking HEAD file with -e is fragile if the repository was 437# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 438# and then pruned. 439sub check_head_link { 440my($dir) =@_; 441my$headfile="$dir/HEAD"; 442return((-e $headfile) || 443(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 444} 445 446sub check_export_ok { 447my($dir) =@_; 448return(check_head_link($dir) && 449(!$export_ok|| -e "$dir/$export_ok") && 450(!$export_auth_hook||$export_auth_hook->($dir))); 451} 452 453# process alternate names for backward compatibility 454# filter out unsupported (unknown) snapshot formats 455sub filter_snapshot_fmts { 456my@fmts=@_; 457 458@fmts=map{ 459exists$known_snapshot_format_aliases{$_} ? 460$known_snapshot_format_aliases{$_} :$_}@fmts; 461@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 462 463} 464 465our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 466if(-e $GITWEB_CONFIG) { 467do$GITWEB_CONFIG; 468}else{ 469our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 470do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 471} 472 473# version of the core git binary 474our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 475 476$projects_list||=$projectroot; 477 478# ====================================================================== 479# input validation and dispatch 480 481# input parameters can be collected from a variety of sources (presently, CGI 482# and PATH_INFO), so we define an %input_params hash that collects them all 483# together during validation: this allows subsequent uses (e.g. href()) to be 484# agnostic of the parameter origin 485 486our%input_params= (); 487 488# input parameters are stored with the long parameter name as key. This will 489# also be used in the href subroutine to convert parameters to their CGI 490# equivalent, and since the href() usage is the most frequent one, we store 491# the name -> CGI key mapping here, instead of the reverse. 492# 493# XXX: Warning: If you touch this, check the search form for updating, 494# too. 495 496our@cgi_param_mapping= ( 497 project =>"p", 498 action =>"a", 499 file_name =>"f", 500 file_parent =>"fp", 501 hash =>"h", 502 hash_parent =>"hp", 503 hash_base =>"hb", 504 hash_parent_base =>"hpb", 505 page =>"pg", 506 order =>"o", 507 searchtext =>"s", 508 searchtype =>"st", 509 snapshot_format =>"sf", 510 extra_options =>"opt", 511 search_use_regexp =>"sr", 512); 513our%cgi_param_mapping=@cgi_param_mapping; 514 515# we will also need to know the possible actions, for validation 516our%actions= ( 517"blame"=> \&git_blame, 518"blobdiff"=> \&git_blobdiff, 519"blobdiff_plain"=> \&git_blobdiff_plain, 520"blob"=> \&git_blob, 521"blob_plain"=> \&git_blob_plain, 522"commitdiff"=> \&git_commitdiff, 523"commitdiff_plain"=> \&git_commitdiff_plain, 524"commit"=> \&git_commit, 525"forks"=> \&git_forks, 526"heads"=> \&git_heads, 527"history"=> \&git_history, 528"log"=> \&git_log, 529"patch"=> \&git_patch, 530"patches"=> \&git_patches, 531"rss"=> \&git_rss, 532"atom"=> \&git_atom, 533"search"=> \&git_search, 534"search_help"=> \&git_search_help, 535"shortlog"=> \&git_shortlog, 536"summary"=> \&git_summary, 537"tag"=> \&git_tag, 538"tags"=> \&git_tags, 539"tree"=> \&git_tree, 540"snapshot"=> \&git_snapshot, 541"object"=> \&git_object, 542# those below don't need $project 543"opml"=> \&git_opml, 544"project_list"=> \&git_project_list, 545"project_index"=> \&git_project_index, 546); 547 548# finally, we have the hash of allowed extra_options for the commands that 549# allow them 550our%allowed_options= ( 551"--no-merges"=> [qw(rss atom log shortlog history)], 552); 553 554# fill %input_params with the CGI parameters. All values except for 'opt' 555# should be single values, but opt can be an array. We should probably 556# build an array of parameters that can be multi-valued, but since for the time 557# being it's only this one, we just single it out 558while(my($name,$symbol) =each%cgi_param_mapping) { 559if($symboleq'opt') { 560$input_params{$name} = [$cgi->param($symbol) ]; 561}else{ 562$input_params{$name} =$cgi->param($symbol); 563} 564} 565 566# now read PATH_INFO and update the parameter list for missing parameters 567sub evaluate_path_info { 568return ifdefined$input_params{'project'}; 569return if!$path_info; 570$path_info=~ s,^/+,,; 571return if!$path_info; 572 573# find which part of PATH_INFO is project 574my$project=$path_info; 575$project=~ s,/+$,,; 576while($project&& !check_head_link("$projectroot/$project")) { 577$project=~ s,/*[^/]*$,,; 578} 579return unless$project; 580$input_params{'project'} =$project; 581 582# do not change any parameters if an action is given using the query string 583return if$input_params{'action'}; 584$path_info=~ s,^\Q$project\E/*,,; 585 586# next, check if we have an action 587my$action=$path_info; 588$action=~ s,/.*$,,; 589if(exists$actions{$action}) { 590$path_info=~ s,^$action/*,,; 591$input_params{'action'} =$action; 592} 593 594# list of actions that want hash_base instead of hash, but can have no 595# pathname (f) parameter 596my@wants_base= ( 597'tree', 598'history', 599); 600 601# we want to catch 602# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 603my($parentrefname,$parentpathname,$refname,$pathname) = 604($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 605 606# first, analyze the 'current' part 607if(defined$pathname) { 608# we got "branch:filename" or "branch:dir/" 609# we could use git_get_type(branch:pathname), but: 610# - it needs $git_dir 611# - it does a git() call 612# - the convention of terminating directories with a slash 613# makes it superfluous 614# - embedding the action in the PATH_INFO would make it even 615# more superfluous 616$pathname=~ s,^/+,,; 617if(!$pathname||substr($pathname, -1)eq"/") { 618$input_params{'action'} ||="tree"; 619$pathname=~ s,/$,,; 620}else{ 621# the default action depends on whether we had parent info 622# or not 623if($parentrefname) { 624$input_params{'action'} ||="blobdiff_plain"; 625}else{ 626$input_params{'action'} ||="blob_plain"; 627} 628} 629$input_params{'hash_base'} ||=$refname; 630$input_params{'file_name'} ||=$pathname; 631}elsif(defined$refname) { 632# we got "branch". In this case we have to choose if we have to 633# set hash or hash_base. 634# 635# Most of the actions without a pathname only want hash to be 636# set, except for the ones specified in @wants_base that want 637# hash_base instead. It should also be noted that hand-crafted 638# links having 'history' as an action and no pathname or hash 639# set will fail, but that happens regardless of PATH_INFO. 640$input_params{'action'} ||="shortlog"; 641if(grep{$_eq$input_params{'action'} }@wants_base) { 642$input_params{'hash_base'} ||=$refname; 643}else{ 644$input_params{'hash'} ||=$refname; 645} 646} 647 648# next, handle the 'parent' part, if present 649if(defined$parentrefname) { 650# a missing pathspec defaults to the 'current' filename, allowing e.g. 651# someproject/blobdiff/oldrev..newrev:/filename 652if($parentpathname) { 653$parentpathname=~ s,^/+,,; 654$parentpathname=~ s,/$,,; 655$input_params{'file_parent'} ||=$parentpathname; 656}else{ 657$input_params{'file_parent'} ||=$input_params{'file_name'}; 658} 659# we assume that hash_parent_base is wanted if a path was specified, 660# or if the action wants hash_base instead of hash 661if(defined$input_params{'file_parent'} || 662grep{$_eq$input_params{'action'} }@wants_base) { 663$input_params{'hash_parent_base'} ||=$parentrefname; 664}else{ 665$input_params{'hash_parent'} ||=$parentrefname; 666} 667} 668 669# for the snapshot action, we allow URLs in the form 670# $project/snapshot/$hash.ext 671# where .ext determines the snapshot and gets removed from the 672# passed $refname to provide the $hash. 673# 674# To be able to tell that $refname includes the format extension, we 675# require the following two conditions to be satisfied: 676# - the hash input parameter MUST have been set from the $refname part 677# of the URL (i.e. they must be equal) 678# - the snapshot format MUST NOT have been defined already (e.g. from 679# CGI parameter sf) 680# It's also useless to try any matching unless $refname has a dot, 681# so we check for that too 682if(defined$input_params{'action'} && 683$input_params{'action'}eq'snapshot'&& 684defined$refname&&index($refname,'.') != -1&& 685$refnameeq$input_params{'hash'} && 686!defined$input_params{'snapshot_format'}) { 687# We loop over the known snapshot formats, checking for 688# extensions. Allowed extensions are both the defined suffix 689# (which includes the initial dot already) and the snapshot 690# format key itself, with a prepended dot 691while(my($fmt,$opt) =each%known_snapshot_formats) { 692my$hash=$refname; 693my$sfx; 694$hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//; 695next unless$sfx=$1; 696# a valid suffix was found, so set the snapshot format 697# and reset the hash parameter 698$input_params{'snapshot_format'} =$fmt; 699$input_params{'hash'} =$hash; 700# we also set the format suffix to the one requested 701# in the URL: this way a request for e.g. .tgz returns 702# a .tgz instead of a .tar.gz 703$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 704last; 705} 706} 707} 708evaluate_path_info(); 709 710our$action=$input_params{'action'}; 711if(defined$action) { 712if(!validate_action($action)) { 713 die_error(400,"Invalid action parameter"); 714} 715} 716 717# parameters which are pathnames 718our$project=$input_params{'project'}; 719if(defined$project) { 720if(!validate_project($project)) { 721undef$project; 722 die_error(404,"No such project"); 723} 724} 725 726our$file_name=$input_params{'file_name'}; 727if(defined$file_name) { 728if(!validate_pathname($file_name)) { 729 die_error(400,"Invalid file parameter"); 730} 731} 732 733our$file_parent=$input_params{'file_parent'}; 734if(defined$file_parent) { 735if(!validate_pathname($file_parent)) { 736 die_error(400,"Invalid file parent parameter"); 737} 738} 739 740# parameters which are refnames 741our$hash=$input_params{'hash'}; 742if(defined$hash) { 743if(!validate_refname($hash)) { 744 die_error(400,"Invalid hash parameter"); 745} 746} 747 748our$hash_parent=$input_params{'hash_parent'}; 749if(defined$hash_parent) { 750if(!validate_refname($hash_parent)) { 751 die_error(400,"Invalid hash parent parameter"); 752} 753} 754 755our$hash_base=$input_params{'hash_base'}; 756if(defined$hash_base) { 757if(!validate_refname($hash_base)) { 758 die_error(400,"Invalid hash base parameter"); 759} 760} 761 762our@extra_options= @{$input_params{'extra_options'}}; 763# @extra_options is always defined, since it can only be (currently) set from 764# CGI, and $cgi->param() returns the empty array in array context if the param 765# is not set 766foreachmy$opt(@extra_options) { 767if(not exists$allowed_options{$opt}) { 768 die_error(400,"Invalid option parameter"); 769} 770if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 771 die_error(400,"Invalid option parameter for this action"); 772} 773} 774 775our$hash_parent_base=$input_params{'hash_parent_base'}; 776if(defined$hash_parent_base) { 777if(!validate_refname($hash_parent_base)) { 778 die_error(400,"Invalid hash parent base parameter"); 779} 780} 781 782# other parameters 783our$page=$input_params{'page'}; 784if(defined$page) { 785if($page=~m/[^0-9]/) { 786 die_error(400,"Invalid page parameter"); 787} 788} 789 790our$searchtype=$input_params{'searchtype'}; 791if(defined$searchtype) { 792if($searchtype=~m/[^a-z]/) { 793 die_error(400,"Invalid searchtype parameter"); 794} 795} 796 797our$search_use_regexp=$input_params{'search_use_regexp'}; 798 799our$searchtext=$input_params{'searchtext'}; 800our$search_regexp; 801if(defined$searchtext) { 802if(length($searchtext) <2) { 803 die_error(403,"At least two characters are required for search parameter"); 804} 805$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 806} 807 808# path to the current git repository 809our$git_dir; 810$git_dir="$projectroot/$project"if$project; 811 812# list of supported snapshot formats 813our@snapshot_fmts= gitweb_get_feature('snapshot'); 814@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 815 816# dispatch 817if(!defined$action) { 818if(defined$hash) { 819$action= git_get_type($hash); 820}elsif(defined$hash_base&&defined$file_name) { 821$action= git_get_type("$hash_base:$file_name"); 822}elsif(defined$project) { 823$action='summary'; 824}else{ 825$action='project_list'; 826} 827} 828if(!defined($actions{$action})) { 829 die_error(400,"Unknown action"); 830} 831if($action!~m/^(opml|project_list|project_index)$/&& 832!$project) { 833 die_error(400,"Project needed"); 834} 835$actions{$action}->(); 836exit; 837 838## ====================================================================== 839## action links 840 841sub href { 842my%params=@_; 843# default is to use -absolute url() i.e. $my_uri 844my$href=$params{-full} ?$my_url:$my_uri; 845 846$params{'project'} =$projectunlessexists$params{'project'}; 847 848if($params{-replay}) { 849while(my($name,$symbol) =each%cgi_param_mapping) { 850if(!exists$params{$name}) { 851$params{$name} =$input_params{$name}; 852} 853} 854} 855 856my$use_pathinfo= gitweb_check_feature('pathinfo'); 857if($use_pathinfoand defined$params{'project'}) { 858# try to put as many parameters as possible in PATH_INFO: 859# - project name 860# - action 861# - hash_parent or hash_parent_base:/file_parent 862# - hash or hash_base:/filename 863# - the snapshot_format as an appropriate suffix 864 865# When the script is the root DirectoryIndex for the domain, 866# $href here would be something like http://gitweb.example.com/ 867# Thus, we strip any trailing / from $href, to spare us double 868# slashes in the final URL 869$href=~ s,/$,,; 870 871# Then add the project name, if present 872$href.="/".esc_url($params{'project'}); 873delete$params{'project'}; 874 875# since we destructively absorb parameters, we keep this 876# boolean that remembers if we're handling a snapshot 877my$is_snapshot=$params{'action'}eq'snapshot'; 878 879# Summary just uses the project path URL, any other action is 880# added to the URL 881if(defined$params{'action'}) { 882$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 883delete$params{'action'}; 884} 885 886# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 887# stripping nonexistent or useless pieces 888$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 889||$params{'hash_parent'} ||$params{'hash'}); 890if(defined$params{'hash_base'}) { 891if(defined$params{'hash_parent_base'}) { 892$href.= esc_url($params{'hash_parent_base'}); 893# skip the file_parent if it's the same as the file_name 894delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 895if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 896$href.=":/".esc_url($params{'file_parent'}); 897delete$params{'file_parent'}; 898} 899$href.=".."; 900delete$params{'hash_parent'}; 901delete$params{'hash_parent_base'}; 902}elsif(defined$params{'hash_parent'}) { 903$href.= esc_url($params{'hash_parent'}).".."; 904delete$params{'hash_parent'}; 905} 906 907$href.= esc_url($params{'hash_base'}); 908if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 909$href.=":/".esc_url($params{'file_name'}); 910delete$params{'file_name'}; 911} 912delete$params{'hash'}; 913delete$params{'hash_base'}; 914}elsif(defined$params{'hash'}) { 915$href.= esc_url($params{'hash'}); 916delete$params{'hash'}; 917} 918 919# If the action was a snapshot, we can absorb the 920# snapshot_format parameter too 921if($is_snapshot) { 922my$fmt=$params{'snapshot_format'}; 923# snapshot_format should always be defined when href() 924# is called, but just in case some code forgets, we 925# fall back to the default 926$fmt||=$snapshot_fmts[0]; 927$href.=$known_snapshot_formats{$fmt}{'suffix'}; 928delete$params{'snapshot_format'}; 929} 930} 931 932# now encode the parameters explicitly 933my@result= (); 934for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 935my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 936if(defined$params{$name}) { 937if(ref($params{$name})eq"ARRAY") { 938foreachmy$par(@{$params{$name}}) { 939push@result,$symbol."=". esc_param($par); 940} 941}else{ 942push@result,$symbol."=". esc_param($params{$name}); 943} 944} 945} 946$href.="?".join(';',@result)ifscalar@result; 947 948return$href; 949} 950 951 952## ====================================================================== 953## validation, quoting/unquoting and escaping 954 955sub validate_action { 956my$input=shift||returnundef; 957returnundefunlessexists$actions{$input}; 958return$input; 959} 960 961sub validate_project { 962my$input=shift||returnundef; 963if(!validate_pathname($input) || 964!(-d "$projectroot/$input") || 965!check_export_ok("$projectroot/$input") || 966($strict_export&& !project_in_list($input))) { 967returnundef; 968}else{ 969return$input; 970} 971} 972 973sub validate_pathname { 974my$input=shift||returnundef; 975 976# no '.' or '..' as elements of path, i.e. no '.' nor '..' 977# at the beginning, at the end, and between slashes. 978# also this catches doubled slashes 979if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 980returnundef; 981} 982# no null characters 983if($input=~m!\0!) { 984returnundef; 985} 986return$input; 987} 988 989sub validate_refname { 990my$input=shift||returnundef; 991 992# textual hashes are O.K. 993if($input=~m/^[0-9a-fA-F]{40}$/) { 994return$input; 995} 996# it must be correct pathname 997$input= validate_pathname($input) 998orreturnundef; 999# restrictions on ref name according to git-check-ref-format1000if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1001returnundef;1002}1003return$input;1004}10051006# decode sequences of octets in utf8 into Perl's internal form,1007# which is utf-8 with utf8 flag set if needed. gitweb writes out1008# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1009sub to_utf8 {1010my$str=shift;1011if(utf8::valid($str)) {1012 utf8::decode($str);1013return$str;1014}else{1015return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1016}1017}10181019# quote unsafe chars, but keep the slash, even when it's not1020# correct, but quoted slashes look too horrible in bookmarks1021sub esc_param {1022my$str=shift;1023$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1024$str=~s/\+/%2B/g;1025$str=~s/ /\+/g;1026return$str;1027}10281029# quote unsafe chars in whole URL, so some charactrs cannot be quoted1030sub esc_url {1031my$str=shift;1032$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1033$str=~s/\+/%2B/g;1034$str=~s/ /\+/g;1035return$str;1036}10371038# replace invalid utf8 character with SUBSTITUTION sequence1039sub esc_html {1040my$str=shift;1041my%opts=@_;10421043$str= to_utf8($str);1044$str=$cgi->escapeHTML($str);1045if($opts{'-nbsp'}) {1046$str=~s/ / /g;1047}1048$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1049return$str;1050}10511052# quote control characters and escape filename to HTML1053sub esc_path {1054my$str=shift;1055my%opts=@_;10561057$str= to_utf8($str);1058$str=$cgi->escapeHTML($str);1059if($opts{'-nbsp'}) {1060$str=~s/ / /g;1061}1062$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1063return$str;1064}10651066# Make control characters "printable", using character escape codes (CEC)1067sub quot_cec {1068my$cntrl=shift;1069my%opts=@_;1070my%es= (# character escape codes, aka escape sequences1071"\t"=>'\t',# tab (HT)1072"\n"=>'\n',# line feed (LF)1073"\r"=>'\r',# carrige return (CR)1074"\f"=>'\f',# form feed (FF)1075"\b"=>'\b',# backspace (BS)1076"\a"=>'\a',# alarm (bell) (BEL)1077"\e"=>'\e',# escape (ESC)1078"\013"=>'\v',# vertical tab (VT)1079"\000"=>'\0',# nul character (NUL)1080);1081my$chr= ( (exists$es{$cntrl})1082?$es{$cntrl}1083:sprintf('\%2x',ord($cntrl)) );1084if($opts{-nohtml}) {1085return$chr;1086}else{1087return"<span class=\"cntrl\">$chr</span>";1088}1089}10901091# Alternatively use unicode control pictures codepoints,1092# Unicode "printable representation" (PR)1093sub quot_upr {1094my$cntrl=shift;1095my%opts=@_;10961097my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1098if($opts{-nohtml}) {1099return$chr;1100}else{1101return"<span class=\"cntrl\">$chr</span>";1102}1103}11041105# git may return quoted and escaped filenames1106sub unquote {1107my$str=shift;11081109sub unq {1110my$seq=shift;1111my%es= (# character escape codes, aka escape sequences1112't'=>"\t",# tab (HT, TAB)1113'n'=>"\n",# newline (NL)1114'r'=>"\r",# return (CR)1115'f'=>"\f",# form feed (FF)1116'b'=>"\b",# backspace (BS)1117'a'=>"\a",# alarm (bell) (BEL)1118'e'=>"\e",# escape (ESC)1119'v'=>"\013",# vertical tab (VT)1120);11211122if($seq=~m/^[0-7]{1,3}$/) {1123# octal char sequence1124returnchr(oct($seq));1125}elsif(exists$es{$seq}) {1126# C escape sequence, aka character escape code1127return$es{$seq};1128}1129# quoted ordinary character1130return$seq;1131}11321133if($str=~m/^"(.*)"$/) {1134# needs unquoting1135$str=$1;1136$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1137}1138return$str;1139}11401141# escape tabs (convert tabs to spaces)1142sub untabify {1143my$line=shift;11441145while((my$pos=index($line,"\t")) != -1) {1146if(my$count= (8- ($pos%8))) {1147my$spaces=' ' x $count;1148$line=~s/\t/$spaces/;1149}1150}11511152return$line;1153}11541155sub project_in_list {1156my$project=shift;1157my@list= git_get_projects_list();1158return@list&&scalar(grep{$_->{'path'}eq$project}@list);1159}11601161## ----------------------------------------------------------------------1162## HTML aware string manipulation11631164# Try to chop given string on a word boundary between position1165# $len and $len+$add_len. If there is no word boundary there,1166# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1167# (marking chopped part) would be longer than given string.1168sub chop_str {1169my$str=shift;1170my$len=shift;1171my$add_len=shift||10;1172my$where=shift||'right';# 'left' | 'center' | 'right'11731174# Make sure perl knows it is utf8 encoded so we don't1175# cut in the middle of a utf8 multibyte char.1176$str= to_utf8($str);11771178# allow only $len chars, but don't cut a word if it would fit in $add_len1179# if it doesn't fit, cut it if it's still longer than the dots we would add1180# remove chopped character entities entirely11811182# when chopping in the middle, distribute $len into left and right part1183# return early if chopping wouldn't make string shorter1184if($whereeq'center') {1185return$strif($len+5>=length($str));# filler is length 51186$len=int($len/2);1187}else{1188return$strif($len+4>=length($str));# filler is length 41189}11901191# regexps: ending and beginning with word part up to $add_len1192my$endre=qr/.{$len}\w{0,$add_len}/;1193my$begre=qr/\w{0,$add_len}.{$len}/;11941195if($whereeq'left') {1196$str=~m/^(.*?)($begre)$/;1197my($lead,$body) = ($1,$2);1198if(length($lead) >4) {1199$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1200$lead=" ...";1201}1202return"$lead$body";12031204}elsif($whereeq'center') {1205$str=~m/^($endre)(.*)$/;1206my($left,$str) = ($1,$2);1207$str=~m/^(.*?)($begre)$/;1208my($mid,$right) = ($1,$2);1209if(length($mid) >5) {1210$left=~s/&[^;]*$//;1211$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1212$mid=" ... ";1213}1214return"$left$mid$right";12151216}else{1217$str=~m/^($endre)(.*)$/;1218my$body=$1;1219my$tail=$2;1220if(length($tail) >4) {1221$body=~s/&[^;]*$//;1222$tail="... ";1223}1224return"$body$tail";1225}1226}12271228# takes the same arguments as chop_str, but also wraps a <span> around the1229# result with a title attribute if it does get chopped. Additionally, the1230# string is HTML-escaped.1231sub chop_and_escape_str {1232my($str) =@_;12331234my$chopped= chop_str(@_);1235if($choppedeq$str) {1236return esc_html($chopped);1237}else{1238$str=~s/[[:cntrl:]]/?/g;1239return$cgi->span({-title=>$str}, esc_html($chopped));1240}1241}12421243## ----------------------------------------------------------------------1244## functions returning short strings12451246# CSS class for given age value (in seconds)1247sub age_class {1248my$age=shift;12491250if(!defined$age) {1251return"noage";1252}elsif($age<60*60*2) {1253return"age0";1254}elsif($age<60*60*24*2) {1255return"age1";1256}else{1257return"age2";1258}1259}12601261# convert age in seconds to "nn units ago" string1262sub age_string {1263my$age=shift;1264my$age_str;12651266if($age>60*60*24*365*2) {1267$age_str= (int$age/60/60/24/365);1268$age_str.=" years ago";1269}elsif($age>60*60*24*(365/12)*2) {1270$age_str=int$age/60/60/24/(365/12);1271$age_str.=" months ago";1272}elsif($age>60*60*24*7*2) {1273$age_str=int$age/60/60/24/7;1274$age_str.=" weeks ago";1275}elsif($age>60*60*24*2) {1276$age_str=int$age/60/60/24;1277$age_str.=" days ago";1278}elsif($age>60*60*2) {1279$age_str=int$age/60/60;1280$age_str.=" hours ago";1281}elsif($age>60*2) {1282$age_str=int$age/60;1283$age_str.=" min ago";1284}elsif($age>2) {1285$age_str=int$age;1286$age_str.=" sec ago";1287}else{1288$age_str.=" right now";1289}1290return$age_str;1291}12921293useconstant{1294 S_IFINVALID =>0030000,1295 S_IFGITLINK =>0160000,1296};12971298# submodule/subproject, a commit object reference1299sub S_ISGITLINK {1300my$mode=shift;13011302return(($mode& S_IFMT) == S_IFGITLINK)1303}13041305# convert file mode in octal to symbolic file mode string1306sub mode_str {1307my$mode=oct shift;13081309if(S_ISGITLINK($mode)) {1310return'm---------';1311}elsif(S_ISDIR($mode& S_IFMT)) {1312return'drwxr-xr-x';1313}elsif(S_ISLNK($mode)) {1314return'lrwxrwxrwx';1315}elsif(S_ISREG($mode)) {1316# git cares only about the executable bit1317if($mode& S_IXUSR) {1318return'-rwxr-xr-x';1319}else{1320return'-rw-r--r--';1321};1322}else{1323return'----------';1324}1325}13261327# convert file mode in octal to file type string1328sub file_type {1329my$mode=shift;13301331if($mode!~m/^[0-7]+$/) {1332return$mode;1333}else{1334$mode=oct$mode;1335}13361337if(S_ISGITLINK($mode)) {1338return"submodule";1339}elsif(S_ISDIR($mode& S_IFMT)) {1340return"directory";1341}elsif(S_ISLNK($mode)) {1342return"symlink";1343}elsif(S_ISREG($mode)) {1344return"file";1345}else{1346return"unknown";1347}1348}13491350# convert file mode in octal to file type description string1351sub file_type_long {1352my$mode=shift;13531354if($mode!~m/^[0-7]+$/) {1355return$mode;1356}else{1357$mode=oct$mode;1358}13591360if(S_ISGITLINK($mode)) {1361return"submodule";1362}elsif(S_ISDIR($mode& S_IFMT)) {1363return"directory";1364}elsif(S_ISLNK($mode)) {1365return"symlink";1366}elsif(S_ISREG($mode)) {1367if($mode& S_IXUSR) {1368return"executable";1369}else{1370return"file";1371};1372}else{1373return"unknown";1374}1375}137613771378## ----------------------------------------------------------------------1379## functions returning short HTML fragments, or transforming HTML fragments1380## which don't belong to other sections13811382# format line of commit message.1383sub format_log_line_html {1384my$line=shift;13851386$line= esc_html($line, -nbsp=>1);1387$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1388$cgi->a({-href => href(action=>"object", hash=>$1),1389-class=>"text"},$1);1390}eg;13911392return$line;1393}13941395# format marker of refs pointing to given object13961397# the destination action is chosen based on object type and current context:1398# - for annotated tags, we choose the tag view unless it's the current view1399# already, in which case we go to shortlog view1400# - for other refs, we keep the current view if we're in history, shortlog or1401# log view, and select shortlog otherwise1402sub format_ref_marker {1403my($refs,$id) =@_;1404my$markers='';14051406if(defined$refs->{$id}) {1407foreachmy$ref(@{$refs->{$id}}) {1408# this code exploits the fact that non-lightweight tags are the1409# only indirect objects, and that they are the only objects for which1410# we want to use tag instead of shortlog as action1411my($type,$name) =qw();1412my$indirect= ($ref=~s/\^\{\}$//);1413# e.g. tags/v2.6.11 or heads/next1414if($ref=~m!^(.*?)s?/(.*)$!) {1415$type=$1;1416$name=$2;1417}else{1418$type="ref";1419$name=$ref;1420}14211422my$class=$type;1423$class.=" indirect"if$indirect;14241425my$dest_action="shortlog";14261427if($indirect) {1428$dest_action="tag"unless$actioneq"tag";1429}elsif($action=~/^(history|(short)?log)$/) {1430$dest_action=$action;1431}14321433my$dest="";1434$dest.="refs/"unless$ref=~ m!^refs/!;1435$dest.=$ref;14361437my$link=$cgi->a({1438-href => href(1439 action=>$dest_action,1440 hash=>$dest1441)},$name);14421443$markers.=" <span class=\"$class\"title=\"$ref\">".1444$link."</span>";1445}1446}14471448if($markers) {1449return' <span class="refs">'.$markers.'</span>';1450}else{1451return"";1452}1453}14541455# format, perhaps shortened and with markers, title line1456sub format_subject_html {1457my($long,$short,$href,$extra) =@_;1458$extra=''unlessdefined($extra);14591460if(length($short) <length($long)) {1461$long=~s/[[:cntrl:]]/?/g;1462return$cgi->a({-href =>$href, -class=>"list subject",1463-title => to_utf8($long)},1464 esc_html($short) .$extra);1465}else{1466return$cgi->a({-href =>$href, -class=>"list subject"},1467 esc_html($long) .$extra);1468}1469}14701471# format git diff header line, i.e. "diff --(git|combined|cc) ..."1472sub format_git_diff_header_line {1473my$line=shift;1474my$diffinfo=shift;1475my($from,$to) =@_;14761477if($diffinfo->{'nparents'}) {1478# combined diff1479$line=~s!^(diff (.*?) )"?.*$!$1!;1480if($to->{'href'}) {1481$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1482 esc_path($to->{'file'}));1483}else{# file was deleted (no href)1484$line.= esc_path($to->{'file'});1485}1486}else{1487# "ordinary" diff1488$line=~s!^(diff (.*?) )"?a/.*$!$1!;1489if($from->{'href'}) {1490$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1491'a/'. esc_path($from->{'file'}));1492}else{# file was added (no href)1493$line.='a/'. esc_path($from->{'file'});1494}1495$line.=' ';1496if($to->{'href'}) {1497$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1498'b/'. esc_path($to->{'file'}));1499}else{# file was deleted1500$line.='b/'. esc_path($to->{'file'});1501}1502}15031504return"<div class=\"diff header\">$line</div>\n";1505}15061507# format extended diff header line, before patch itself1508sub format_extended_diff_header_line {1509my$line=shift;1510my$diffinfo=shift;1511my($from,$to) =@_;15121513# match <path>1514if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1515$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1516 esc_path($from->{'file'}));1517}1518if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1519$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1520 esc_path($to->{'file'}));1521}1522# match single <mode>1523if($line=~m/\s(\d{6})$/) {1524$line.='<span class="info"> ('.1525 file_type_long($1) .1526')</span>';1527}1528# match <hash>1529if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1530# can match only for combined diff1531$line='index ';1532for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1533if($from->{'href'}[$i]) {1534$line.=$cgi->a({-href=>$from->{'href'}[$i],1535-class=>"hash"},1536substr($diffinfo->{'from_id'}[$i],0,7));1537}else{1538$line.='0' x 7;1539}1540# separator1541$line.=','if($i<$diffinfo->{'nparents'} -1);1542}1543$line.='..';1544if($to->{'href'}) {1545$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1546substr($diffinfo->{'to_id'},0,7));1547}else{1548$line.='0' x 7;1549}15501551}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1552# can match only for ordinary diff1553my($from_link,$to_link);1554if($from->{'href'}) {1555$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1556substr($diffinfo->{'from_id'},0,7));1557}else{1558$from_link='0' x 7;1559}1560if($to->{'href'}) {1561$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1562substr($diffinfo->{'to_id'},0,7));1563}else{1564$to_link='0' x 7;1565}1566my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1567$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1568}15691570return$line."<br/>\n";1571}15721573# format from-file/to-file diff header1574sub format_diff_from_to_header {1575my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1576my$line;1577my$result='';15781579$line=$from_line;1580#assert($line =~ m/^---/) if DEBUG;1581# no extra formatting for "^--- /dev/null"1582if(!$diffinfo->{'nparents'}) {1583# ordinary (single parent) diff1584if($line=~m!^--- "?a/!) {1585if($from->{'href'}) {1586$line='--- a/'.1587$cgi->a({-href=>$from->{'href'}, -class=>"path"},1588 esc_path($from->{'file'}));1589}else{1590$line='--- a/'.1591 esc_path($from->{'file'});1592}1593}1594$result.= qq!<div class="diff from_file">$line</div>\n!;15951596}else{1597# combined diff (merge commit)1598for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1599if($from->{'href'}[$i]) {1600$line='--- '.1601$cgi->a({-href=>href(action=>"blobdiff",1602 hash_parent=>$diffinfo->{'from_id'}[$i],1603 hash_parent_base=>$parents[$i],1604 file_parent=>$from->{'file'}[$i],1605 hash=>$diffinfo->{'to_id'},1606 hash_base=>$hash,1607 file_name=>$to->{'file'}),1608-class=>"path",1609-title=>"diff". ($i+1)},1610$i+1) .1611'/'.1612$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1613 esc_path($from->{'file'}[$i]));1614}else{1615$line='--- /dev/null';1616}1617$result.= qq!<div class="diff from_file">$line</div>\n!;1618}1619}16201621$line=$to_line;1622#assert($line =~ m/^\+\+\+/) if DEBUG;1623# no extra formatting for "^+++ /dev/null"1624if($line=~m!^\+\+\+ "?b/!) {1625if($to->{'href'}) {1626$line='+++ b/'.1627$cgi->a({-href=>$to->{'href'}, -class=>"path"},1628 esc_path($to->{'file'}));1629}else{1630$line='+++ b/'.1631 esc_path($to->{'file'});1632}1633}1634$result.= qq!<div class="diff to_file">$line</div>\n!;16351636return$result;1637}16381639# create note for patch simplified by combined diff1640sub format_diff_cc_simplified {1641my($diffinfo,@parents) =@_;1642my$result='';16431644$result.="<div class=\"diff header\">".1645"diff --cc ";1646if(!is_deleted($diffinfo)) {1647$result.=$cgi->a({-href => href(action=>"blob",1648 hash_base=>$hash,1649 hash=>$diffinfo->{'to_id'},1650 file_name=>$diffinfo->{'to_file'}),1651-class=>"path"},1652 esc_path($diffinfo->{'to_file'}));1653}else{1654$result.= esc_path($diffinfo->{'to_file'});1655}1656$result.="</div>\n".# class="diff header"1657"<div class=\"diff nodifferences\">".1658"Simple merge".1659"</div>\n";# class="diff nodifferences"16601661return$result;1662}16631664# format patch (diff) line (not to be used for diff headers)1665sub format_diff_line {1666my$line=shift;1667my($from,$to) =@_;1668my$diff_class="";16691670chomp$line;16711672if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1673# combined diff1674my$prefix=substr($line,0,scalar@{$from->{'href'}});1675if($line=~m/^\@{3}/) {1676$diff_class=" chunk_header";1677}elsif($line=~m/^\\/) {1678$diff_class=" incomplete";1679}elsif($prefix=~tr/+/+/) {1680$diff_class=" add";1681}elsif($prefix=~tr/-/-/) {1682$diff_class=" rem";1683}1684}else{1685# assume ordinary diff1686my$char=substr($line,0,1);1687if($chareq'+') {1688$diff_class=" add";1689}elsif($chareq'-') {1690$diff_class=" rem";1691}elsif($chareq'@') {1692$diff_class=" chunk_header";1693}elsif($chareq"\\") {1694$diff_class=" incomplete";1695}1696}1697$line= untabify($line);1698if($from&&$to&&$line=~m/^\@{2} /) {1699my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1700$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;17011702$from_lines=0unlessdefined$from_lines;1703$to_lines=0unlessdefined$to_lines;17041705if($from->{'href'}) {1706$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1707-class=>"list"},$from_text);1708}1709if($to->{'href'}) {1710$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1711-class=>"list"},$to_text);1712}1713$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1714"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1715return"<div class=\"diff$diff_class\">$line</div>\n";1716}elsif($from&&$to&&$line=~m/^\@{3}/) {1717my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1718my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);17191720@from_text=split(' ',$ranges);1721for(my$i=0;$i<@from_text; ++$i) {1722($from_start[$i],$from_nlines[$i]) =1723(split(',',substr($from_text[$i],1)),0);1724}17251726$to_text=pop@from_text;1727$to_start=pop@from_start;1728$to_nlines=pop@from_nlines;17291730$line="<span class=\"chunk_info\">$prefix";1731for(my$i=0;$i<@from_text; ++$i) {1732if($from->{'href'}[$i]) {1733$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1734-class=>"list"},$from_text[$i]);1735}else{1736$line.=$from_text[$i];1737}1738$line.=" ";1739}1740if($to->{'href'}) {1741$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1742-class=>"list"},$to_text);1743}else{1744$line.=$to_text;1745}1746$line.="$prefix</span>".1747"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1748return"<div class=\"diff$diff_class\">$line</div>\n";1749}1750return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1751}17521753# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1754# linked. Pass the hash of the tree/commit to snapshot.1755sub format_snapshot_links {1756my($hash) =@_;1757my$num_fmts=@snapshot_fmts;1758if($num_fmts>1) {1759# A parenthesized list of links bearing format names.1760# e.g. "snapshot (_tar.gz_ _zip_)"1761return"snapshot (".join(' ',map1762$cgi->a({1763-href => href(1764 action=>"snapshot",1765 hash=>$hash,1766 snapshot_format=>$_1767)1768},$known_snapshot_formats{$_}{'display'})1769,@snapshot_fmts) .")";1770}elsif($num_fmts==1) {1771# A single "snapshot" link whose tooltip bears the format name.1772# i.e. "_snapshot_"1773my($fmt) =@snapshot_fmts;1774return1775$cgi->a({1776-href => href(1777 action=>"snapshot",1778 hash=>$hash,1779 snapshot_format=>$fmt1780),1781-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1782},"snapshot");1783}else{# $num_fmts == 01784returnundef;1785}1786}17871788## ......................................................................1789## functions returning values to be passed, perhaps after some1790## transformation, to other functions; e.g. returning arguments to href()17911792# returns hash to be passed to href to generate gitweb URL1793# in -title key it returns description of link1794sub get_feed_info {1795my$format=shift||'Atom';1796my%res= (action =>lc($format));17971798# feed links are possible only for project views1799return unless(defined$project);1800# some views should link to OPML, or to generic project feed,1801# or don't have specific feed yet (so they should use generic)1802return if($action=~/^(?:tags|heads|forks|tag|search)$/x);18031804my$branch;1805# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1806# from tag links; this also makes possible to detect branch links1807if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1808(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1809$branch=$1;1810}1811# find log type for feed description (title)1812my$type='log';1813if(defined$file_name) {1814$type="history of$file_name";1815$type.="/"if($actioneq'tree');1816$type.=" on '$branch'"if(defined$branch);1817}else{1818$type="log of$branch"if(defined$branch);1819}18201821$res{-title} =$type;1822$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1823$res{'file_name'} =$file_name;18241825return%res;1826}18271828## ----------------------------------------------------------------------1829## git utility subroutines, invoking git commands18301831# returns path to the core git executable and the --git-dir parameter as list1832sub git_cmd {1833return$GIT,'--git-dir='.$git_dir;1834}18351836# quote the given arguments for passing them to the shell1837# quote_command("command", "arg 1", "arg with ' and ! characters")1838# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1839# Try to avoid using this function wherever possible.1840sub quote_command {1841returnjoin(' ',1842map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1843}18441845# get HEAD ref of given project as hash1846sub git_get_head_hash {1847my$project=shift;1848my$o_git_dir=$git_dir;1849my$retval=undef;1850$git_dir="$projectroot/$project";1851if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1852my$head= <$fd>;1853close$fd;1854if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1855$retval=$1;1856}1857}1858if(defined$o_git_dir) {1859$git_dir=$o_git_dir;1860}1861return$retval;1862}18631864# get type of given object1865sub git_get_type {1866my$hash=shift;18671868open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1869my$type= <$fd>;1870close$fdorreturn;1871chomp$type;1872return$type;1873}18741875# repository configuration1876our$config_file='';1877our%config;18781879# store multiple values for single key as anonymous array reference1880# single values stored directly in the hash, not as [ <value> ]1881sub hash_set_multi {1882my($hash,$key,$value) =@_;18831884if(!exists$hash->{$key}) {1885$hash->{$key} =$value;1886}elsif(!ref$hash->{$key}) {1887$hash->{$key} = [$hash->{$key},$value];1888}else{1889push@{$hash->{$key}},$value;1890}1891}18921893# return hash of git project configuration1894# optionally limited to some section, e.g. 'gitweb'1895sub git_parse_project_config {1896my$section_regexp=shift;1897my%config;18981899local$/="\0";19001901open my$fh,"-|", git_cmd(),"config",'-z','-l',1902orreturn;19031904while(my$keyval= <$fh>) {1905chomp$keyval;1906my($key,$value) =split(/\n/,$keyval,2);19071908 hash_set_multi(\%config,$key,$value)1909if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1910}1911close$fh;19121913return%config;1914}19151916# convert config value to boolean: 'true' or 'false'1917# no value, number > 0, 'true' and 'yes' values are true1918# rest of values are treated as false (never as error)1919sub config_to_bool {1920my$val=shift;19211922return1if!defined$val;# section.key19231924# strip leading and trailing whitespace1925$val=~s/^\s+//;1926$val=~s/\s+$//;19271928return(($val=~/^\d+$/&&$val) ||# section.key = 11929($val=~/^(?:true|yes)$/i));# section.key = true1930}19311932# convert config value to simple decimal number1933# an optional value suffix of 'k', 'm', or 'g' will cause the value1934# to be multiplied by 1024, 1048576, or 10737418241935sub config_to_int {1936my$val=shift;19371938# strip leading and trailing whitespace1939$val=~s/^\s+//;1940$val=~s/\s+$//;19411942if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1943$unit=lc($unit);1944# unknown unit is treated as 11945return$num* ($uniteq'g'?1073741824:1946$uniteq'm'?1048576:1947$uniteq'k'?1024:1);1948}1949return$val;1950}19511952# convert config value to array reference, if needed1953sub config_to_multi {1954my$val=shift;19551956returnref($val) ?$val: (defined($val) ? [$val] : []);1957}19581959sub git_get_project_config {1960my($key,$type) =@_;19611962# key sanity check1963return unless($key);1964$key=~s/^gitweb\.//;1965return if($key=~m/\W/);19661967# type sanity check1968if(defined$type) {1969$type=~s/^--//;1970$type=undef1971unless($typeeq'bool'||$typeeq'int');1972}19731974# get config1975if(!defined$config_file||1976$config_filene"$git_dir/config") {1977%config= git_parse_project_config('gitweb');1978$config_file="$git_dir/config";1979}19801981# check if config variable (key) exists1982return unlessexists$config{"gitweb.$key"};19831984# ensure given type1985if(!defined$type) {1986return$config{"gitweb.$key"};1987}elsif($typeeq'bool') {1988# backward compatibility: 'git config --bool' returns true/false1989return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1990}elsif($typeeq'int') {1991return config_to_int($config{"gitweb.$key"});1992}1993return$config{"gitweb.$key"};1994}19951996# get hash of given path at given ref1997sub git_get_hash_by_path {1998my$base=shift;1999my$path=shift||returnundef;2000my$type=shift;20012002$path=~ s,/+$,,;20032004open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2005or die_error(500,"Open git-ls-tree failed");2006my$line= <$fd>;2007close$fdorreturnundef;20082009if(!defined$line) {2010# there is no tree or hash given by $path at $base2011returnundef;2012}20132014#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2015$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2016if(defined$type&&$typene$2) {2017# type doesn't match2018returnundef;2019}2020return$3;2021}20222023# get path of entry with given hash at given tree-ish (ref)2024# used to get 'from' filename for combined diff (merge commit) for renames2025sub git_get_path_by_hash {2026my$base=shift||return;2027my$hash=shift||return;20282029local$/="\0";20302031open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2032orreturnundef;2033while(my$line= <$fd>) {2034chomp$line;20352036#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2037#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2038if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2039close$fd;2040return$1;2041}2042}2043close$fd;2044returnundef;2045}20462047## ......................................................................2048## git utility functions, directly accessing git repository20492050sub git_get_project_description {2051my$path=shift;20522053$git_dir="$projectroot/$path";2054open my$fd,"$git_dir/description"2055orreturn git_get_project_config('description');2056my$descr= <$fd>;2057close$fd;2058if(defined$descr) {2059chomp$descr;2060}2061return$descr;2062}20632064sub git_get_project_ctags {2065my$path=shift;2066my$ctags= {};20672068$git_dir="$projectroot/$path";2069unless(opendir D,"$git_dir/ctags") {2070return$ctags;2071}2072foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir(D)) {2073open CT,$_ornext;2074my$val= <CT>;2075chomp$val;2076close CT;2077my$ctag=$_;$ctag=~ s#.*/##;2078$ctags->{$ctag} =$val;2079}2080closedir D;2081$ctags;2082}20832084sub git_populate_project_tagcloud {2085my$ctags=shift;20862087# First, merge different-cased tags; tags vote on casing2088my%ctags_lc;2089foreach(keys%$ctags) {2090$ctags_lc{lc$_}->{count} +=$ctags->{$_};2091if(not$ctags_lc{lc$_}->{topcount}2092or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2093$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2094$ctags_lc{lc$_}->{topname} =$_;2095}2096}20972098my$cloud;2099if(eval{require HTML::TagCloud;1; }) {2100$cloud= HTML::TagCloud->new;2101foreach(sort keys%ctags_lc) {2102# Pad the title with spaces so that the cloud looks2103# less crammed.2104my$title=$ctags_lc{$_}->{topname};2105$title=~s/ / /g;2106$title=~s/^/ /g;2107$title=~s/$/ /g;2108$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2109}2110}else{2111$cloud= \%ctags_lc;2112}2113$cloud;2114}21152116sub git_show_project_tagcloud {2117my($cloud,$count) =@_;2118print STDERR ref($cloud)."..\n";2119if(ref$cloudeq'HTML::TagCloud') {2120return$cloud->html_and_css($count);2121}else{2122my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2123return'<p align="center">'.join(', ',map{2124"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2125}splice(@tags,0,$count)) .'</p>';2126}2127}21282129sub git_get_project_url_list {2130my$path=shift;21312132$git_dir="$projectroot/$path";2133open my$fd,"$git_dir/cloneurl"2134orreturnwantarray?2135@{ config_to_multi(git_get_project_config('url')) } :2136 config_to_multi(git_get_project_config('url'));2137my@git_project_url_list=map{chomp;$_} <$fd>;2138close$fd;21392140returnwantarray?@git_project_url_list: \@git_project_url_list;2141}21422143sub git_get_projects_list {2144my($filter) =@_;2145my@list;21462147$filter||='';2148$filter=~s/\.git$//;21492150my$check_forks= gitweb_check_feature('forks');21512152if(-d $projects_list) {2153# search in directory2154my$dir=$projects_list. ($filter?"/$filter":'');2155# remove the trailing "/"2156$dir=~s!/+$!!;2157my$pfxlen=length("$dir");2158my$pfxdepth= ($dir=~tr!/!!);21592160 File::Find::find({2161 follow_fast =>1,# follow symbolic links2162 follow_skip =>2,# ignore duplicates2163 dangling_symlinks =>0,# ignore dangling symlinks, silently2164 wanted =>sub{2165# skip project-list toplevel, if we get it.2166return if(m!^[/.]$!);2167# only directories can be git repositories2168return unless(-d $_);2169# don't traverse too deep (Find is super slow on os x)2170if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2171$File::Find::prune =1;2172return;2173}21742175my$subdir=substr($File::Find::name,$pfxlen+1);2176# we check related file in $projectroot2177my$path= ($filter?"$filter/":'') .$subdir;2178if(check_export_ok("$projectroot/$path")) {2179push@list, { path =>$path};2180$File::Find::prune =1;2181}2182},2183},"$dir");21842185}elsif(-f $projects_list) {2186# read from file(url-encoded):2187# 'git%2Fgit.git Linus+Torvalds'2188# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2189# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2190my%paths;2191open my($fd),$projects_listorreturn;2192 PROJECT:2193while(my$line= <$fd>) {2194chomp$line;2195my($path,$owner) =split' ',$line;2196$path= unescape($path);2197$owner= unescape($owner);2198if(!defined$path) {2199next;2200}2201if($filterne'') {2202# looking for forks;2203my$pfx=substr($path,0,length($filter));2204if($pfxne$filter) {2205next PROJECT;2206}2207my$sfx=substr($path,length($filter));2208if($sfx!~/^\/.*\.git$/) {2209next PROJECT;2210}2211}elsif($check_forks) {2212 PATH:2213foreachmy$filter(keys%paths) {2214# looking for forks;2215my$pfx=substr($path,0,length($filter));2216if($pfxne$filter) {2217next PATH;2218}2219my$sfx=substr($path,length($filter));2220if($sfx!~/^\/.*\.git$/) {2221next PATH;2222}2223# is a fork, don't include it in2224# the list2225next PROJECT;2226}2227}2228if(check_export_ok("$projectroot/$path")) {2229my$pr= {2230 path =>$path,2231 owner => to_utf8($owner),2232};2233push@list,$pr;2234(my$forks_path=$path) =~s/\.git$//;2235$paths{$forks_path}++;2236}2237}2238close$fd;2239}2240return@list;2241}22422243our$gitweb_project_owner=undef;2244sub git_get_project_list_from_file {22452246return if(defined$gitweb_project_owner);22472248$gitweb_project_owner= {};2249# read from file (url-encoded):2250# 'git%2Fgit.git Linus+Torvalds'2251# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2252# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2253if(-f $projects_list) {2254open(my$fd,$projects_list);2255while(my$line= <$fd>) {2256chomp$line;2257my($pr,$ow) =split' ',$line;2258$pr= unescape($pr);2259$ow= unescape($ow);2260$gitweb_project_owner->{$pr} = to_utf8($ow);2261}2262close$fd;2263}2264}22652266sub git_get_project_owner {2267my$project=shift;2268my$owner;22692270returnundefunless$project;2271$git_dir="$projectroot/$project";22722273if(!defined$gitweb_project_owner) {2274 git_get_project_list_from_file();2275}22762277if(exists$gitweb_project_owner->{$project}) {2278$owner=$gitweb_project_owner->{$project};2279}2280if(!defined$owner){2281$owner= git_get_project_config('owner');2282}2283if(!defined$owner) {2284$owner= get_file_owner("$git_dir");2285}22862287return$owner;2288}22892290sub git_get_last_activity {2291my($path) =@_;2292my$fd;22932294$git_dir="$projectroot/$path";2295open($fd,"-|", git_cmd(),'for-each-ref',2296'--format=%(committer)',2297'--sort=-committerdate',2298'--count=1',2299'refs/heads')orreturn;2300my$most_recent= <$fd>;2301close$fdorreturn;2302if(defined$most_recent&&2303$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2304my$timestamp=$1;2305my$age=time-$timestamp;2306return($age, age_string($age));2307}2308return(undef,undef);2309}23102311sub git_get_references {2312my$type=shift||"";2313my%refs;2314# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112315# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2316open my$fd,"-|", git_cmd(),"show-ref","--dereference",2317($type? ("--","refs/$type") : ())# use -- <pattern> if $type2318orreturn;23192320while(my$line= <$fd>) {2321chomp$line;2322if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2323if(defined$refs{$1}) {2324push@{$refs{$1}},$2;2325}else{2326$refs{$1} = [$2];2327}2328}2329}2330close$fdorreturn;2331return \%refs;2332}23332334sub git_get_rev_name_tags {2335my$hash=shift||returnundef;23362337open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2338orreturn;2339my$name_rev= <$fd>;2340close$fd;23412342if($name_rev=~ m|^$hash tags/(.*)$|) {2343return$1;2344}else{2345# catches also '$hash undefined' output2346returnundef;2347}2348}23492350## ----------------------------------------------------------------------2351## parse to hash functions23522353sub parse_date {2354my$epoch=shift;2355my$tz=shift||"-0000";23562357my%date;2358my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2359my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2360my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2361$date{'hour'} =$hour;2362$date{'minute'} =$min;2363$date{'mday'} =$mday;2364$date{'day'} =$days[$wday];2365$date{'month'} =$months[$mon];2366$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2367$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2368$date{'mday-time'} =sprintf"%d%s%02d:%02d",2369$mday,$months[$mon],$hour,$min;2370$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",23711900+$year,1+$mon,$mday,$hour,$min,$sec;23722373$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2374my$local=$epoch+ ((int$1+ ($2/60)) *3600);2375($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2376$date{'hour_local'} =$hour;2377$date{'minute_local'} =$min;2378$date{'tz_local'} =$tz;2379$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",23801900+$year,$mon+1,$mday,2381$hour,$min,$sec,$tz);2382return%date;2383}23842385sub parse_tag {2386my$tag_id=shift;2387my%tag;2388my@comment;23892390open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2391$tag{'id'} =$tag_id;2392while(my$line= <$fd>) {2393chomp$line;2394if($line=~m/^object ([0-9a-fA-F]{40})$/) {2395$tag{'object'} =$1;2396}elsif($line=~m/^type (.+)$/) {2397$tag{'type'} =$1;2398}elsif($line=~m/^tag (.+)$/) {2399$tag{'name'} =$1;2400}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2401$tag{'author'} =$1;2402$tag{'epoch'} =$2;2403$tag{'tz'} =$3;2404}elsif($line=~m/--BEGIN/) {2405push@comment,$line;2406last;2407}elsif($lineeq"") {2408last;2409}2410}2411push@comment, <$fd>;2412$tag{'comment'} = \@comment;2413close$fdorreturn;2414if(!defined$tag{'name'}) {2415return2416};2417return%tag2418}24192420sub parse_commit_text {2421my($commit_text,$withparents) =@_;2422my@commit_lines=split'\n',$commit_text;2423my%co;24242425pop@commit_lines;# Remove '\0'24262427if(!@commit_lines) {2428return;2429}24302431my$header=shift@commit_lines;2432if($header!~m/^[0-9a-fA-F]{40}/) {2433return;2434}2435($co{'id'},my@parents) =split' ',$header;2436while(my$line=shift@commit_lines) {2437last if$lineeq"\n";2438if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2439$co{'tree'} =$1;2440}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2441push@parents,$1;2442}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2443$co{'author'} =$1;2444$co{'author_epoch'} =$2;2445$co{'author_tz'} =$3;2446if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2447$co{'author_name'} =$1;2448$co{'author_email'} =$2;2449}else{2450$co{'author_name'} =$co{'author'};2451}2452}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2453$co{'committer'} =$1;2454$co{'committer_epoch'} =$2;2455$co{'committer_tz'} =$3;2456$co{'committer_name'} =$co{'committer'};2457if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2458$co{'committer_name'} =$1;2459$co{'committer_email'} =$2;2460}else{2461$co{'committer_name'} =$co{'committer'};2462}2463}2464}2465if(!defined$co{'tree'}) {2466return;2467};2468$co{'parents'} = \@parents;2469$co{'parent'} =$parents[0];24702471foreachmy$title(@commit_lines) {2472$title=~s/^ //;2473if($titlene"") {2474$co{'title'} = chop_str($title,80,5);2475# remove leading stuff of merges to make the interesting part visible2476if(length($title) >50) {2477$title=~s/^Automatic //;2478$title=~s/^merge (of|with) /Merge ... /i;2479if(length($title) >50) {2480$title=~s/(http|rsync):\/\///;2481}2482if(length($title) >50) {2483$title=~s/(master|www|rsync)\.//;2484}2485if(length($title) >50) {2486$title=~s/kernel.org:?//;2487}2488if(length($title) >50) {2489$title=~s/\/pub\/scm//;2490}2491}2492$co{'title_short'} = chop_str($title,50,5);2493last;2494}2495}2496if(!defined$co{'title'} ||$co{'title'}eq"") {2497$co{'title'} =$co{'title_short'} ='(no commit message)';2498}2499# remove added spaces2500foreachmy$line(@commit_lines) {2501$line=~s/^ //;2502}2503$co{'comment'} = \@commit_lines;25042505my$age=time-$co{'committer_epoch'};2506$co{'age'} =$age;2507$co{'age_string'} = age_string($age);2508my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2509if($age>60*60*24*7*2) {2510$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2511$co{'age_string_age'} =$co{'age_string'};2512}else{2513$co{'age_string_date'} =$co{'age_string'};2514$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2515}2516return%co;2517}25182519sub parse_commit {2520my($commit_id) =@_;2521my%co;25222523local$/="\0";25242525open my$fd,"-|", git_cmd(),"rev-list",2526"--parents",2527"--header",2528"--max-count=1",2529$commit_id,2530"--",2531or die_error(500,"Open git-rev-list failed");2532%co= parse_commit_text(<$fd>,1);2533close$fd;25342535return%co;2536}25372538sub parse_commits {2539my($commit_id,$maxcount,$skip,$filename,@args) =@_;2540my@cos;25412542$maxcount||=1;2543$skip||=0;25442545local$/="\0";25462547open my$fd,"-|", git_cmd(),"rev-list",2548"--header",2549@args,2550("--max-count=".$maxcount),2551("--skip=".$skip),2552@extra_options,2553$commit_id,2554"--",2555($filename? ($filename) : ())2556or die_error(500,"Open git-rev-list failed");2557while(my$line= <$fd>) {2558my%co= parse_commit_text($line);2559push@cos, \%co;2560}2561close$fd;25622563returnwantarray?@cos: \@cos;2564}25652566# parse line of git-diff-tree "raw" output2567sub parse_difftree_raw_line {2568my$line=shift;2569my%res;25702571# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2572# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2573if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2574$res{'from_mode'} =$1;2575$res{'to_mode'} =$2;2576$res{'from_id'} =$3;2577$res{'to_id'} =$4;2578$res{'status'} =$5;2579$res{'similarity'} =$6;2580if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2581($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2582}else{2583$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2584}2585}2586# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2587# combined diff (for merge commit)2588elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2589$res{'nparents'} =length($1);2590$res{'from_mode'} = [split(' ',$2) ];2591$res{'to_mode'} =pop@{$res{'from_mode'}};2592$res{'from_id'} = [split(' ',$3) ];2593$res{'to_id'} =pop@{$res{'from_id'}};2594$res{'status'} = [split('',$4) ];2595$res{'to_file'} = unquote($5);2596}2597# 'c512b523472485aef4fff9e57b229d9d243c967f'2598elsif($line=~m/^([0-9a-fA-F]{40})$/) {2599$res{'commit'} =$1;2600}26012602returnwantarray?%res: \%res;2603}26042605# wrapper: return parsed line of git-diff-tree "raw" output2606# (the argument might be raw line, or parsed info)2607sub parsed_difftree_line {2608my$line_or_ref=shift;26092610if(ref($line_or_ref)eq"HASH") {2611# pre-parsed (or generated by hand)2612return$line_or_ref;2613}else{2614return parse_difftree_raw_line($line_or_ref);2615}2616}26172618# parse line of git-ls-tree output2619sub parse_ls_tree_line {2620my$line=shift;2621my%opts=@_;2622my%res;26232624#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2625$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;26262627$res{'mode'} =$1;2628$res{'type'} =$2;2629$res{'hash'} =$3;2630if($opts{'-z'}) {2631$res{'name'} =$4;2632}else{2633$res{'name'} = unquote($4);2634}26352636returnwantarray?%res: \%res;2637}26382639# generates _two_ hashes, references to which are passed as 2 and 3 argument2640sub parse_from_to_diffinfo {2641my($diffinfo,$from,$to,@parents) =@_;26422643if($diffinfo->{'nparents'}) {2644# combined diff2645$from->{'file'} = [];2646$from->{'href'} = [];2647 fill_from_file_info($diffinfo,@parents)2648unlessexists$diffinfo->{'from_file'};2649for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2650$from->{'file'}[$i] =2651defined$diffinfo->{'from_file'}[$i] ?2652$diffinfo->{'from_file'}[$i] :2653$diffinfo->{'to_file'};2654if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2655$from->{'href'}[$i] = href(action=>"blob",2656 hash_base=>$parents[$i],2657 hash=>$diffinfo->{'from_id'}[$i],2658 file_name=>$from->{'file'}[$i]);2659}else{2660$from->{'href'}[$i] =undef;2661}2662}2663}else{2664# ordinary (not combined) diff2665$from->{'file'} =$diffinfo->{'from_file'};2666if($diffinfo->{'status'}ne"A") {# not new (added) file2667$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2668 hash=>$diffinfo->{'from_id'},2669 file_name=>$from->{'file'});2670}else{2671delete$from->{'href'};2672}2673}26742675$to->{'file'} =$diffinfo->{'to_file'};2676if(!is_deleted($diffinfo)) {# file exists in result2677$to->{'href'} = href(action=>"blob", hash_base=>$hash,2678 hash=>$diffinfo->{'to_id'},2679 file_name=>$to->{'file'});2680}else{2681delete$to->{'href'};2682}2683}26842685## ......................................................................2686## parse to array of hashes functions26872688sub git_get_heads_list {2689my$limit=shift;2690my@headslist;26912692open my$fd,'-|', git_cmd(),'for-each-ref',2693($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2694'--format=%(objectname) %(refname) %(subject)%00%(committer)',2695'refs/heads'2696orreturn;2697while(my$line= <$fd>) {2698my%ref_item;26992700chomp$line;2701my($refinfo,$committerinfo) =split(/\0/,$line);2702my($hash,$name,$title) =split(' ',$refinfo,3);2703my($committer,$epoch,$tz) =2704($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2705$ref_item{'fullname'} =$name;2706$name=~s!^refs/heads/!!;27072708$ref_item{'name'} =$name;2709$ref_item{'id'} =$hash;2710$ref_item{'title'} =$title||'(no commit message)';2711$ref_item{'epoch'} =$epoch;2712if($epoch) {2713$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2714}else{2715$ref_item{'age'} ="unknown";2716}27172718push@headslist, \%ref_item;2719}2720close$fd;27212722returnwantarray?@headslist: \@headslist;2723}27242725sub git_get_tags_list {2726my$limit=shift;2727my@tagslist;27282729open my$fd,'-|', git_cmd(),'for-each-ref',2730($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2731'--format=%(objectname) %(objecttype) %(refname) '.2732'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2733'refs/tags'2734orreturn;2735while(my$line= <$fd>) {2736my%ref_item;27372738chomp$line;2739my($refinfo,$creatorinfo) =split(/\0/,$line);2740my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2741my($creator,$epoch,$tz) =2742($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2743$ref_item{'fullname'} =$name;2744$name=~s!^refs/tags/!!;27452746$ref_item{'type'} =$type;2747$ref_item{'id'} =$id;2748$ref_item{'name'} =$name;2749if($typeeq"tag") {2750$ref_item{'subject'} =$title;2751$ref_item{'reftype'} =$reftype;2752$ref_item{'refid'} =$refid;2753}else{2754$ref_item{'reftype'} =$type;2755$ref_item{'refid'} =$id;2756}27572758if($typeeq"tag"||$typeeq"commit") {2759$ref_item{'epoch'} =$epoch;2760if($epoch) {2761$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2762}else{2763$ref_item{'age'} ="unknown";2764}2765}27662767push@tagslist, \%ref_item;2768}2769close$fd;27702771returnwantarray?@tagslist: \@tagslist;2772}27732774## ----------------------------------------------------------------------2775## filesystem-related functions27762777sub get_file_owner {2778my$path=shift;27792780my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2781my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2782if(!defined$gcos) {2783returnundef;2784}2785my$owner=$gcos;2786$owner=~s/[,;].*$//;2787return to_utf8($owner);2788}27892790# assume that file exists2791sub insert_file {2792my$filename=shift;27932794open my$fd,'<',$filename;2795print map{ to_utf8($_) } <$fd>;2796close$fd;2797}27982799## ......................................................................2800## mimetype related functions28012802sub mimetype_guess_file {2803my$filename=shift;2804my$mimemap=shift;2805-r $mimemaporreturnundef;28062807my%mimemap;2808open(MIME,$mimemap)orreturnundef;2809while(<MIME>) {2810next ifm/^#/;# skip comments2811my($mime,$exts) =split(/\t+/);2812if(defined$exts) {2813my@exts=split(/\s+/,$exts);2814foreachmy$ext(@exts) {2815$mimemap{$ext} =$mime;2816}2817}2818}2819close(MIME);28202821$filename=~/\.([^.]*)$/;2822return$mimemap{$1};2823}28242825sub mimetype_guess {2826my$filename=shift;2827my$mime;2828$filename=~/\./orreturnundef;28292830if($mimetypes_file) {2831my$file=$mimetypes_file;2832if($file!~m!^/!) {# if it is relative path2833# it is relative to project2834$file="$projectroot/$project/$file";2835}2836$mime= mimetype_guess_file($filename,$file);2837}2838$mime||= mimetype_guess_file($filename,'/etc/mime.types');2839return$mime;2840}28412842sub blob_mimetype {2843my$fd=shift;2844my$filename=shift;28452846if($filename) {2847my$mime= mimetype_guess($filename);2848$mimeandreturn$mime;2849}28502851# just in case2852return$default_blob_plain_mimetypeunless$fd;28532854if(-T $fd) {2855return'text/plain';2856}elsif(!$filename) {2857return'application/octet-stream';2858}elsif($filename=~m/\.png$/i) {2859return'image/png';2860}elsif($filename=~m/\.gif$/i) {2861return'image/gif';2862}elsif($filename=~m/\.jpe?g$/i) {2863return'image/jpeg';2864}else{2865return'application/octet-stream';2866}2867}28682869sub blob_contenttype {2870my($fd,$file_name,$type) =@_;28712872$type||= blob_mimetype($fd,$file_name);2873if($typeeq'text/plain'&&defined$default_text_plain_charset) {2874$type.="; charset=$default_text_plain_charset";2875}28762877return$type;2878}28792880## ======================================================================2881## functions printing HTML: header, footer, error page28822883sub git_header_html {2884my$status=shift||"200 OK";2885my$expires=shift;28862887my$title="$site_name";2888if(defined$project) {2889$title.=" - ". to_utf8($project);2890if(defined$action) {2891$title.="/$action";2892if(defined$file_name) {2893$title.=" - ". esc_path($file_name);2894if($actioneq"tree"&&$file_name!~ m|/$|) {2895$title.="/";2896}2897}2898}2899}2900my$content_type;2901# require explicit support from the UA if we are to send the page as2902# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2903# we have to do this because MSIE sometimes globs '*/*', pretending to2904# support xhtml+xml but choking when it gets what it asked for.2905if(defined$cgi->http('HTTP_ACCEPT') &&2906$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2907$cgi->Accept('application/xhtml+xml') !=0) {2908$content_type='application/xhtml+xml';2909}else{2910$content_type='text/html';2911}2912print$cgi->header(-type=>$content_type, -charset =>'utf-8',2913-status=>$status, -expires =>$expires);2914my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2915print<<EOF;2916<?xml version="1.0" encoding="utf-8"?>2917<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2918<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2919<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2920<!-- git core binaries version$git_version-->2921<head>2922<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2923<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2924<meta name="robots" content="index, nofollow"/>2925<title>$title</title>2926EOF2927# the stylesheet, favicon etc urls won't work correctly with path_info2928# unless we set the appropriate base URL2929if($ENV{'PATH_INFO'}) {2930print"<base href=\"".esc_url($base_url)."\"/>\n";2931}2932# print out each stylesheet that exist, providing backwards capability2933# for those people who defined $stylesheet in a config file2934if(defined$stylesheet) {2935print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2936}else{2937foreachmy$stylesheet(@stylesheets) {2938next unless$stylesheet;2939print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2940}2941}2942if(defined$project) {2943my%href_params= get_feed_info();2944if(!exists$href_params{'-title'}) {2945$href_params{'-title'} ='log';2946}29472948foreachmy$formatqw(RSS Atom){2949my$type=lc($format);2950my%link_attr= (2951'-rel'=>'alternate',2952'-title'=>"$project-$href_params{'-title'} -$formatfeed",2953'-type'=>"application/$type+xml"2954);29552956$href_params{'action'} =$type;2957$link_attr{'-href'} = href(%href_params);2958print"<link ".2959"rel=\"$link_attr{'-rel'}\"".2960"title=\"$link_attr{'-title'}\"".2961"href=\"$link_attr{'-href'}\"".2962"type=\"$link_attr{'-type'}\"".2963"/>\n";29642965$href_params{'extra_options'} ='--no-merges';2966$link_attr{'-href'} = href(%href_params);2967$link_attr{'-title'} .=' (no merges)';2968print"<link ".2969"rel=\"$link_attr{'-rel'}\"".2970"title=\"$link_attr{'-title'}\"".2971"href=\"$link_attr{'-href'}\"".2972"type=\"$link_attr{'-type'}\"".2973"/>\n";2974}29752976}else{2977printf('<link rel="alternate" title="%sprojects list" '.2978'href="%s" type="text/plain; charset=utf-8" />'."\n",2979$site_name, href(project=>undef, action=>"project_index"));2980printf('<link rel="alternate" title="%sprojects feeds" '.2981'href="%s" type="text/x-opml" />'."\n",2982$site_name, href(project=>undef, action=>"opml"));2983}2984if(defined$favicon) {2985printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2986}29872988print"</head>\n".2989"<body>\n";29902991if(-f $site_header) {2992 insert_file($site_header);2993}29942995print"<div class=\"page_header\">\n".2996$cgi->a({-href => esc_url($logo_url),2997-title =>$logo_label},2998qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2999print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3000if(defined$project) {3001print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3002if(defined$action) {3003print" /$action";3004}3005print"\n";3006}3007print"</div>\n";30083009my$have_search= gitweb_check_feature('search');3010if(defined$project&&$have_search) {3011if(!defined$searchtext) {3012$searchtext="";3013}3014my$search_hash;3015if(defined$hash_base) {3016$search_hash=$hash_base;3017}elsif(defined$hash) {3018$search_hash=$hash;3019}else{3020$search_hash="HEAD";3021}3022my$action=$my_uri;3023my$use_pathinfo= gitweb_check_feature('pathinfo');3024if($use_pathinfo) {3025$action.="/".esc_url($project);3026}3027print$cgi->startform(-method=>"get", -action =>$action) .3028"<div class=\"search\">\n".3029(!$use_pathinfo&&3030$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3031$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3032$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3033$cgi->popup_menu(-name =>'st', -default=>'commit',3034-values=> ['commit','grep','author','committer','pickaxe']) .3035$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3036" search:\n",3037$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3038"<span title=\"Extended regular expression\">".3039$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3040-checked =>$search_use_regexp) .3041"</span>".3042"</div>".3043$cgi->end_form() ."\n";3044}3045}30463047sub git_footer_html {3048my$feed_class='rss_logo';30493050print"<div class=\"page_footer\">\n";3051if(defined$project) {3052my$descr= git_get_project_description($project);3053if(defined$descr) {3054print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3055}30563057my%href_params= get_feed_info();3058if(!%href_params) {3059$feed_class.=' generic';3060}3061$href_params{'-title'} ||='log';30623063foreachmy$formatqw(RSS Atom){3064$href_params{'action'} =lc($format);3065print$cgi->a({-href => href(%href_params),3066-title =>"$href_params{'-title'}$formatfeed",3067-class=>$feed_class},$format)."\n";3068}30693070}else{3071print$cgi->a({-href => href(project=>undef, action=>"opml"),3072-class=>$feed_class},"OPML") ." ";3073print$cgi->a({-href => href(project=>undef, action=>"project_index"),3074-class=>$feed_class},"TXT") ."\n";3075}3076print"</div>\n";# class="page_footer"30773078if(-f $site_footer) {3079 insert_file($site_footer);3080}30813082print"</body>\n".3083"</html>";3084}30853086# die_error(<http_status_code>, <error_message>)3087# Example: die_error(404, 'Hash not found')3088# By convention, use the following status codes (as defined in RFC 2616):3089# 400: Invalid or missing CGI parameters, or3090# requested object exists but has wrong type.3091# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3092# this server or project.3093# 404: Requested object/revision/project doesn't exist.3094# 500: The server isn't configured properly, or3095# an internal error occurred (e.g. failed assertions caused by bugs), or3096# an unknown error occurred (e.g. the git binary died unexpectedly).3097sub die_error {3098my$status=shift||500;3099my$error=shift||"Internal server error";31003101my%http_responses= (400=>'400 Bad Request',3102403=>'403 Forbidden',3103404=>'404 Not Found',3104500=>'500 Internal Server Error');3105 git_header_html($http_responses{$status});3106print<<EOF;3107<div class="page_body">3108<br /><br />3109$status-$error3110<br />3111</div>3112EOF3113 git_footer_html();3114exit;3115}31163117## ----------------------------------------------------------------------3118## functions printing or outputting HTML: navigation31193120sub git_print_page_nav {3121my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3122$extra=''if!defined$extra;# pager or formats31233124my@navs=qw(summary shortlog log commit commitdiff tree);3125if($suppress) {3126@navs=grep{$_ne$suppress}@navs;3127}31283129my%arg=map{$_=> {action=>$_} }@navs;3130if(defined$head) {3131for(qw(commit commitdiff)) {3132$arg{$_}{'hash'} =$head;3133}3134if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3135for(qw(shortlog log)) {3136$arg{$_}{'hash'} =$head;3137}3138}3139}31403141$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3142$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;31433144my@actions= gitweb_get_feature('actions');3145my%repl= (3146'%'=>'%',3147'n'=>$project,# project name3148'f'=>$git_dir,# project path within filesystem3149'h'=>$treehead||'',# current hash ('h' parameter)3150'b'=>$treebase||'',# hash base ('hb' parameter)3151);3152while(@actions) {3153my($label,$link,$pos) =splice(@actions,0,3);3154# insert3155@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3156# munch munch3157$link=~s/%([%nfhb])/$repl{$1}/g;3158$arg{$label}{'_href'} =$link;3159}31603161print"<div class=\"page_nav\">\n".3162(join" | ",3163map{$_eq$current?3164$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3165}@navs);3166print"<br/>\n$extra<br/>\n".3167"</div>\n";3168}31693170sub format_paging_nav {3171my($action,$hash,$head,$page,$has_next_link) =@_;3172my$paging_nav;317331743175if($hashne$head||$page) {3176$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3177}else{3178$paging_nav.="HEAD";3179}31803181if($page>0) {3182$paging_nav.=" ⋅ ".3183$cgi->a({-href => href(-replay=>1, page=>$page-1),3184-accesskey =>"p", -title =>"Alt-p"},"prev");3185}else{3186$paging_nav.=" ⋅ prev";3187}31883189if($has_next_link) {3190$paging_nav.=" ⋅ ".3191$cgi->a({-href => href(-replay=>1, page=>$page+1),3192-accesskey =>"n", -title =>"Alt-n"},"next");3193}else{3194$paging_nav.=" ⋅ next";3195}31963197return$paging_nav;3198}31993200## ......................................................................3201## functions printing or outputting HTML: div32023203sub git_print_header_div {3204my($action,$title,$hash,$hash_base) =@_;3205my%args= ();32063207$args{'action'} =$action;3208$args{'hash'} =$hashif$hash;3209$args{'hash_base'} =$hash_baseif$hash_base;32103211print"<div class=\"header\">\n".3212$cgi->a({-href => href(%args), -class=>"title"},3213$title?$title:$action) .3214"\n</div>\n";3215}32163217sub git_print_authorship {3218my$co=shift;32193220my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3221print"<div class=\"author_date\">".3222 esc_html($co->{'author_name'}) .3223" [$ad{'rfc2822'}";3224if($ad{'hour_local'} <6) {3225printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3226$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3227}else{3228printf(" (%02d:%02d%s)",3229$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3230}3231print"]</div>\n";3232}32333234sub git_print_page_path {3235my$name=shift;3236my$type=shift;3237my$hb=shift;323832393240print"<div class=\"page_path\">";3241print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3242-title =>'tree root'}, to_utf8("[$project]"));3243print" / ";3244if(defined$name) {3245my@dirname=split'/',$name;3246my$basename=pop@dirname;3247my$fullname='';32483249foreachmy$dir(@dirname) {3250$fullname.= ($fullname?'/':'') .$dir;3251print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3252 hash_base=>$hb),3253-title =>$fullname}, esc_path($dir));3254print" / ";3255}3256if(defined$type&&$typeeq'blob') {3257print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3258 hash_base=>$hb),3259-title =>$name}, esc_path($basename));3260}elsif(defined$type&&$typeeq'tree') {3261print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3262 hash_base=>$hb),3263-title =>$name}, esc_path($basename));3264print" / ";3265}else{3266print esc_path($basename);3267}3268}3269print"<br/></div>\n";3270}32713272sub git_print_log {3273my$log=shift;3274my%opts=@_;32753276if($opts{'-remove_title'}) {3277# remove title, i.e. first line of log3278shift@$log;3279}3280# remove leading empty lines3281while(defined$log->[0] &&$log->[0]eq"") {3282shift@$log;3283}32843285# print log3286my$signoff=0;3287my$empty=0;3288foreachmy$line(@$log) {3289if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3290$signoff=1;3291$empty=0;3292if(!$opts{'-remove_signoff'}) {3293print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3294next;3295}else{3296# remove signoff lines3297next;3298}3299}else{3300$signoff=0;3301}33023303# print only one empty line3304# do not print empty line after signoff3305if($lineeq"") {3306next if($empty||$signoff);3307$empty=1;3308}else{3309$empty=0;3310}33113312print format_log_line_html($line) ."<br/>\n";3313}33143315if($opts{'-final_empty_line'}) {3316# end with single empty line3317print"<br/>\n"unless$empty;3318}3319}33203321# return link target (what link points to)3322sub git_get_link_target {3323my$hash=shift;3324my$link_target;33253326# read link3327open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3328orreturn;3329{3330local$/;3331$link_target= <$fd>;3332}3333close$fd3334orreturn;33353336return$link_target;3337}33383339# given link target, and the directory (basedir) the link is in,3340# return target of link relative to top directory (top tree);3341# return undef if it is not possible (including absolute links).3342sub normalize_link_target {3343my($link_target,$basedir,$hash_base) =@_;33443345# we can normalize symlink target only if $hash_base is provided3346return unless$hash_base;33473348# absolute symlinks (beginning with '/') cannot be normalized3349return if(substr($link_target,0,1)eq'/');33503351# normalize link target to path from top (root) tree (dir)3352my$path;3353if($basedir) {3354$path=$basedir.'/'.$link_target;3355}else{3356# we are in top (root) tree (dir)3357$path=$link_target;3358}33593360# remove //, /./, and /../3361my@path_parts;3362foreachmy$part(split('/',$path)) {3363# discard '.' and ''3364next if(!$part||$parteq'.');3365# handle '..'3366if($parteq'..') {3367if(@path_parts) {3368pop@path_parts;3369}else{3370# link leads outside repository (outside top dir)3371return;3372}3373}else{3374push@path_parts,$part;3375}3376}3377$path=join('/',@path_parts);33783379return$path;3380}33813382# print tree entry (row of git_tree), but without encompassing <tr> element3383sub git_print_tree_entry {3384my($t,$basedir,$hash_base,$have_blame) =@_;33853386my%base_key= ();3387$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33883389# The format of a table row is: mode list link. Where mode is3390# the mode of the entry, list is the name of the entry, an href,3391# and link is the action links of the entry.33923393print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3394if($t->{'type'}eq"blob") {3395print"<td class=\"list\">".3396$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3397 file_name=>"$basedir$t->{'name'}",%base_key),3398-class=>"list"}, esc_path($t->{'name'}));3399if(S_ISLNK(oct$t->{'mode'})) {3400my$link_target= git_get_link_target($t->{'hash'});3401if($link_target) {3402my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3403if(defined$norm_target) {3404print" -> ".3405$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3406 file_name=>$norm_target),3407-title =>$norm_target}, esc_path($link_target));3408}else{3409print" -> ". esc_path($link_target);3410}3411}3412}3413print"</td>\n";3414print"<td class=\"link\">";3415print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3416 file_name=>"$basedir$t->{'name'}",%base_key)},3417"blob");3418if($have_blame) {3419print" | ".3420$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3421 file_name=>"$basedir$t->{'name'}",%base_key)},3422"blame");3423}3424if(defined$hash_base) {3425print" | ".3426$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3427 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3428"history");3429}3430print" | ".3431$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3432 file_name=>"$basedir$t->{'name'}")},3433"raw");3434print"</td>\n";34353436}elsif($t->{'type'}eq"tree") {3437print"<td class=\"list\">";3438print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3439 file_name=>"$basedir$t->{'name'}",%base_key)},3440 esc_path($t->{'name'}));3441print"</td>\n";3442print"<td class=\"link\">";3443print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3444 file_name=>"$basedir$t->{'name'}",%base_key)},3445"tree");3446if(defined$hash_base) {3447print" | ".3448$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3449 file_name=>"$basedir$t->{'name'}")},3450"history");3451}3452print"</td>\n";3453}else{3454# unknown object: we can only present history for it3455# (this includes 'commit' object, i.e. submodule support)3456print"<td class=\"list\">".3457 esc_path($t->{'name'}) .3458"</td>\n";3459print"<td class=\"link\">";3460if(defined$hash_base) {3461print$cgi->a({-href => href(action=>"history",3462 hash_base=>$hash_base,3463 file_name=>"$basedir$t->{'name'}")},3464"history");3465}3466print"</td>\n";3467}3468}34693470## ......................................................................3471## functions printing large fragments of HTML34723473# get pre-image filenames for merge (combined) diff3474sub fill_from_file_info {3475my($diff,@parents) =@_;34763477$diff->{'from_file'} = [ ];3478$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3479for(my$i=0;$i<$diff->{'nparents'};$i++) {3480if($diff->{'status'}[$i]eq'R'||3481$diff->{'status'}[$i]eq'C') {3482$diff->{'from_file'}[$i] =3483 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3484}3485}34863487return$diff;3488}34893490# is current raw difftree line of file deletion3491sub is_deleted {3492my$diffinfo=shift;34933494return$diffinfo->{'to_id'}eq('0' x 40);3495}34963497# does patch correspond to [previous] difftree raw line3498# $diffinfo - hashref of parsed raw diff format3499# $patchinfo - hashref of parsed patch diff format3500# (the same keys as in $diffinfo)3501sub is_patch_split {3502my($diffinfo,$patchinfo) =@_;35033504returndefined$diffinfo&&defined$patchinfo3505&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3506}350735083509sub git_difftree_body {3510my($difftree,$hash,@parents) =@_;3511my($parent) =$parents[0];3512my$have_blame= gitweb_check_feature('blame');3513print"<div class=\"list_head\">\n";3514if($#{$difftree} >10) {3515print(($#{$difftree} +1) ." files changed:\n");3516}3517print"</div>\n";35183519print"<table class=\"".3520(@parents>1?"combined ":"") .3521"diff_tree\">\n";35223523# header only for combined diff in 'commitdiff' view3524my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3525if($has_header) {3526# table header3527print"<thead><tr>\n".3528"<th></th><th></th>\n";# filename, patchN link3529for(my$i=0;$i<@parents;$i++) {3530my$par=$parents[$i];3531print"<th>".3532$cgi->a({-href => href(action=>"commitdiff",3533 hash=>$hash, hash_parent=>$par),3534-title =>'commitdiff to parent number '.3535($i+1) .': '.substr($par,0,7)},3536$i+1) .3537" </th>\n";3538}3539print"</tr></thead>\n<tbody>\n";3540}35413542my$alternate=1;3543my$patchno=0;3544foreachmy$line(@{$difftree}) {3545my$diff= parsed_difftree_line($line);35463547if($alternate) {3548print"<tr class=\"dark\">\n";3549}else{3550print"<tr class=\"light\">\n";3551}3552$alternate^=1;35533554if(exists$diff->{'nparents'}) {# combined diff35553556 fill_from_file_info($diff,@parents)3557unlessexists$diff->{'from_file'};35583559if(!is_deleted($diff)) {3560# file exists in the result (child) commit3561print"<td>".3562$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3563 file_name=>$diff->{'to_file'},3564 hash_base=>$hash),3565-class=>"list"}, esc_path($diff->{'to_file'})) .3566"</td>\n";3567}else{3568print"<td>".3569 esc_path($diff->{'to_file'}) .3570"</td>\n";3571}35723573if($actioneq'commitdiff') {3574# link to patch3575$patchno++;3576print"<td class=\"link\">".3577$cgi->a({-href =>"#patch$patchno"},"patch") .3578" | ".3579"</td>\n";3580}35813582my$has_history=0;3583my$not_deleted=0;3584for(my$i=0;$i<$diff->{'nparents'};$i++) {3585my$hash_parent=$parents[$i];3586my$from_hash=$diff->{'from_id'}[$i];3587my$from_path=$diff->{'from_file'}[$i];3588my$status=$diff->{'status'}[$i];35893590$has_history||= ($statusne'A');3591$not_deleted||= ($statusne'D');35923593if($statuseq'A') {3594print"<td class=\"link\"align=\"right\"> | </td>\n";3595}elsif($statuseq'D') {3596print"<td class=\"link\">".3597$cgi->a({-href => href(action=>"blob",3598 hash_base=>$hash,3599 hash=>$from_hash,3600 file_name=>$from_path)},3601"blob". ($i+1)) .3602" | </td>\n";3603}else{3604if($diff->{'to_id'}eq$from_hash) {3605print"<td class=\"link nochange\">";3606}else{3607print"<td class=\"link\">";3608}3609print$cgi->a({-href => href(action=>"blobdiff",3610 hash=>$diff->{'to_id'},3611 hash_parent=>$from_hash,3612 hash_base=>$hash,3613 hash_parent_base=>$hash_parent,3614 file_name=>$diff->{'to_file'},3615 file_parent=>$from_path)},3616"diff". ($i+1)) .3617" | </td>\n";3618}3619}36203621print"<td class=\"link\">";3622if($not_deleted) {3623print$cgi->a({-href => href(action=>"blob",3624 hash=>$diff->{'to_id'},3625 file_name=>$diff->{'to_file'},3626 hash_base=>$hash)},3627"blob");3628print" | "if($has_history);3629}3630if($has_history) {3631print$cgi->a({-href => href(action=>"history",3632 file_name=>$diff->{'to_file'},3633 hash_base=>$hash)},3634"history");3635}3636print"</td>\n";36373638print"</tr>\n";3639next;# instead of 'else' clause, to avoid extra indent3640}3641# else ordinary diff36423643my($to_mode_oct,$to_mode_str,$to_file_type);3644my($from_mode_oct,$from_mode_str,$from_file_type);3645if($diff->{'to_mode'}ne('0' x 6)) {3646$to_mode_oct=oct$diff->{'to_mode'};3647if(S_ISREG($to_mode_oct)) {# only for regular file3648$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3649}3650$to_file_type= file_type($diff->{'to_mode'});3651}3652if($diff->{'from_mode'}ne('0' x 6)) {3653$from_mode_oct=oct$diff->{'from_mode'};3654if(S_ISREG($to_mode_oct)) {# only for regular file3655$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3656}3657$from_file_type= file_type($diff->{'from_mode'});3658}36593660if($diff->{'status'}eq"A") {# created3661my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3662$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3663$mode_chng.="]</span>";3664print"<td>";3665print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3666 hash_base=>$hash, file_name=>$diff->{'file'}),3667-class=>"list"}, esc_path($diff->{'file'}));3668print"</td>\n";3669print"<td>$mode_chng</td>\n";3670print"<td class=\"link\">";3671if($actioneq'commitdiff') {3672# link to patch3673$patchno++;3674print$cgi->a({-href =>"#patch$patchno"},"patch");3675print" | ";3676}3677print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3678 hash_base=>$hash, file_name=>$diff->{'file'})},3679"blob");3680print"</td>\n";36813682}elsif($diff->{'status'}eq"D") {# deleted3683my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3684print"<td>";3685print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3686 hash_base=>$parent, file_name=>$diff->{'file'}),3687-class=>"list"}, esc_path($diff->{'file'}));3688print"</td>\n";3689print"<td>$mode_chng</td>\n";3690print"<td class=\"link\">";3691if($actioneq'commitdiff') {3692# link to patch3693$patchno++;3694print$cgi->a({-href =>"#patch$patchno"},"patch");3695print" | ";3696}3697print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3698 hash_base=>$parent, file_name=>$diff->{'file'})},3699"blob") ." | ";3700if($have_blame) {3701print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3702 file_name=>$diff->{'file'})},3703"blame") ." | ";3704}3705print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3706 file_name=>$diff->{'file'})},3707"history");3708print"</td>\n";37093710}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3711my$mode_chnge="";3712if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3713$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3714if($from_file_typene$to_file_type) {3715$mode_chnge.=" from$from_file_typeto$to_file_type";3716}3717if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3718if($from_mode_str&&$to_mode_str) {3719$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3720}elsif($to_mode_str) {3721$mode_chnge.=" mode:$to_mode_str";3722}3723}3724$mode_chnge.="]</span>\n";3725}3726print"<td>";3727print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3728 hash_base=>$hash, file_name=>$diff->{'file'}),3729-class=>"list"}, esc_path($diff->{'file'}));3730print"</td>\n";3731print"<td>$mode_chnge</td>\n";3732print"<td class=\"link\">";3733if($actioneq'commitdiff') {3734# link to patch3735$patchno++;3736print$cgi->a({-href =>"#patch$patchno"},"patch") .3737" | ";3738}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3739# "commit" view and modified file (not onlu mode changed)3740print$cgi->a({-href => href(action=>"blobdiff",3741 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3742 hash_base=>$hash, hash_parent_base=>$parent,3743 file_name=>$diff->{'file'})},3744"diff") .3745" | ";3746}3747print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3748 hash_base=>$hash, file_name=>$diff->{'file'})},3749"blob") ." | ";3750if($have_blame) {3751print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3752 file_name=>$diff->{'file'})},3753"blame") ." | ";3754}3755print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3756 file_name=>$diff->{'file'})},3757"history");3758print"</td>\n";37593760}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3761my%status_name= ('R'=>'moved','C'=>'copied');3762my$nstatus=$status_name{$diff->{'status'}};3763my$mode_chng="";3764if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3765# mode also for directories, so we cannot use $to_mode_str3766$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3767}3768print"<td>".3769$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3770 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3771-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3772"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3773$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3774 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3775-class=>"list"}, esc_path($diff->{'from_file'})) .3776" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3777"<td class=\"link\">";3778if($actioneq'commitdiff') {3779# link to patch3780$patchno++;3781print$cgi->a({-href =>"#patch$patchno"},"patch") .3782" | ";3783}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3784# "commit" view and modified file (not only pure rename or copy)3785print$cgi->a({-href => href(action=>"blobdiff",3786 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3787 hash_base=>$hash, hash_parent_base=>$parent,3788 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3789"diff") .3790" | ";3791}3792print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3793 hash_base=>$parent, file_name=>$diff->{'to_file'})},3794"blob") ." | ";3795if($have_blame) {3796print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3797 file_name=>$diff->{'to_file'})},3798"blame") ." | ";3799}3800print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3801 file_name=>$diff->{'to_file'})},3802"history");3803print"</td>\n";38043805}# we should not encounter Unmerged (U) or Unknown (X) status3806print"</tr>\n";3807}3808print"</tbody>"if$has_header;3809print"</table>\n";3810}38113812sub git_patchset_body {3813my($fd,$difftree,$hash,@hash_parents) =@_;3814my($hash_parent) =$hash_parents[0];38153816my$is_combined= (@hash_parents>1);3817my$patch_idx=0;3818my$patch_number=0;3819my$patch_line;3820my$diffinfo;3821my$to_name;3822my(%from,%to);38233824print"<div class=\"patchset\">\n";38253826# skip to first patch3827while($patch_line= <$fd>) {3828chomp$patch_line;38293830last if($patch_line=~m/^diff /);3831}38323833 PATCH:3834while($patch_line) {38353836# parse "git diff" header line3837if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3838# $1 is from_name, which we do not use3839$to_name= unquote($2);3840$to_name=~s!^b/!!;3841}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3842# $1 is 'cc' or 'combined', which we do not use3843$to_name= unquote($2);3844}else{3845$to_name=undef;3846}38473848# check if current patch belong to current raw line3849# and parse raw git-diff line if needed3850if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3851# this is continuation of a split patch3852print"<div class=\"patch cont\">\n";3853}else{3854# advance raw git-diff output if needed3855$patch_idx++ifdefined$diffinfo;38563857# read and prepare patch information3858$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38593860# compact combined diff output can have some patches skipped3861# find which patch (using pathname of result) we are at now;3862if($is_combined) {3863while($to_namene$diffinfo->{'to_file'}) {3864print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3865 format_diff_cc_simplified($diffinfo,@hash_parents) .3866"</div>\n";# class="patch"38673868$patch_idx++;3869$patch_number++;38703871last if$patch_idx>$#$difftree;3872$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3873}3874}38753876# modifies %from, %to hashes3877 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);38783879# this is first patch for raw difftree line with $patch_idx index3880# we index @$difftree array from 0, but number patches from 13881print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3882}38833884# git diff header3885#assert($patch_line =~ m/^diff /) if DEBUG;3886#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3887$patch_number++;3888# print "git diff" header3889print format_git_diff_header_line($patch_line,$diffinfo,3890 \%from, \%to);38913892# print extended diff header3893print"<div class=\"diff extended_header\">\n";3894 EXTENDED_HEADER:3895while($patch_line= <$fd>) {3896chomp$patch_line;38973898last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);38993900print format_extended_diff_header_line($patch_line,$diffinfo,3901 \%from, \%to);3902}3903print"</div>\n";# class="diff extended_header"39043905# from-file/to-file diff header3906if(!$patch_line) {3907print"</div>\n";# class="patch"3908last PATCH;3909}3910next PATCH if($patch_line=~m/^diff /);3911#assert($patch_line =~ m/^---/) if DEBUG;39123913my$last_patch_line=$patch_line;3914$patch_line= <$fd>;3915chomp$patch_line;3916#assert($patch_line =~ m/^\+\+\+/) if DEBUG;39173918print format_diff_from_to_header($last_patch_line,$patch_line,3919$diffinfo, \%from, \%to,3920@hash_parents);39213922# the patch itself3923 LINE:3924while($patch_line= <$fd>) {3925chomp$patch_line;39263927next PATCH if($patch_line=~m/^diff /);39283929print format_diff_line($patch_line, \%from, \%to);3930}39313932}continue{3933print"</div>\n";# class="patch"3934}39353936# for compact combined (--cc) format, with chunk and patch simpliciaction3937# patchset might be empty, but there might be unprocessed raw lines3938for(++$patch_idxif$patch_number>0;3939$patch_idx<@$difftree;3940++$patch_idx) {3941# read and prepare patch information3942$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39433944# generate anchor for "patch" links in difftree / whatchanged part3945print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3946 format_diff_cc_simplified($diffinfo,@hash_parents) .3947"</div>\n";# class="patch"39483949$patch_number++;3950}39513952if($patch_number==0) {3953if(@hash_parents>1) {3954print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3955}else{3956print"<div class=\"diff nodifferences\">No differences found</div>\n";3957}3958}39593960print"</div>\n";# class="patchset"3961}39623963# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39643965# fills project list info (age, description, owner, forks) for each3966# project in the list, removing invalid projects from returned list3967# NOTE: modifies $projlist, but does not remove entries from it3968sub fill_project_list_info {3969my($projlist,$check_forks) =@_;3970my@projects;39713972my$show_ctags= gitweb_check_feature('ctags');3973 PROJECT:3974foreachmy$pr(@$projlist) {3975my(@activity) = git_get_last_activity($pr->{'path'});3976unless(@activity) {3977next PROJECT;3978}3979($pr->{'age'},$pr->{'age_string'}) =@activity;3980if(!defined$pr->{'descr'}) {3981my$descr= git_get_project_description($pr->{'path'}) ||"";3982$descr= to_utf8($descr);3983$pr->{'descr_long'} =$descr;3984$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3985}3986if(!defined$pr->{'owner'}) {3987$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3988}3989if($check_forks) {3990my$pname=$pr->{'path'};3991if(($pname=~s/\.git$//) &&3992($pname!~/\/$/) &&3993(-d "$projectroot/$pname")) {3994$pr->{'forks'} ="-d$projectroot/$pname";3995}else{3996$pr->{'forks'} =0;3997}3998}3999$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4000push@projects,$pr;4001}40024003return@projects;4004}40054006# print 'sort by' <th> element, generating 'sort by $name' replay link4007# if that order is not selected4008sub print_sort_th {4009my($name,$order,$header) =@_;4010$header||=ucfirst($name);40114012if($ordereq$name) {4013print"<th>$header</th>\n";4014}else{4015print"<th>".4016$cgi->a({-href => href(-replay=>1, order=>$name),4017-class=>"header"},$header) .4018"</th>\n";4019}4020}40214022sub git_project_list_body {4023# actually uses global variable $project4024my($projlist,$order,$from,$to,$extra,$no_header) =@_;40254026my$check_forks= gitweb_check_feature('forks');4027my@projects= fill_project_list_info($projlist,$check_forks);40284029$order||=$default_projects_order;4030$from=0unlessdefined$from;4031$to=$#projectsif(!defined$to||$#projects<$to);40324033my%order_info= (4034 project => { key =>'path', type =>'str'},4035 descr => { key =>'descr_long', type =>'str'},4036 owner => { key =>'owner', type =>'str'},4037 age => { key =>'age', type =>'num'}4038);4039my$oi=$order_info{$order};4040if($oi->{'type'}eq'str') {4041@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4042}else{4043@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4044}40454046my$show_ctags= gitweb_check_feature('ctags');4047if($show_ctags) {4048my%ctags;4049foreachmy$p(@projects) {4050foreachmy$ct(keys%{$p->{'ctags'}}) {4051$ctags{$ct} +=$p->{'ctags'}->{$ct};4052}4053}4054my$cloud= git_populate_project_tagcloud(\%ctags);4055print git_show_project_tagcloud($cloud,64);4056}40574058print"<table class=\"project_list\">\n";4059unless($no_header) {4060print"<tr>\n";4061if($check_forks) {4062print"<th></th>\n";4063}4064 print_sort_th('project',$order,'Project');4065 print_sort_th('descr',$order,'Description');4066 print_sort_th('owner',$order,'Owner');4067 print_sort_th('age',$order,'Last Change');4068print"<th></th>\n".# for links4069"</tr>\n";4070}4071my$alternate=1;4072my$tagfilter=$cgi->param('by_tag');4073for(my$i=$from;$i<=$to;$i++) {4074my$pr=$projects[$i];40754076next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4077next if$searchtextand not$pr->{'path'} =~/$searchtext/4078and not$pr->{'descr_long'} =~/$searchtext/;4079# Weed out forks or non-matching entries of search4080if($check_forks) {4081my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4082$forkbase="^$forkbase"if$forkbase;4083next ifnot$searchtextand not$tagfilterand$show_ctags4084and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4085}40864087if($alternate) {4088print"<tr class=\"dark\">\n";4089}else{4090print"<tr class=\"light\">\n";4091}4092$alternate^=1;4093if($check_forks) {4094print"<td>";4095if($pr->{'forks'}) {4096print"<!--$pr->{'forks'} -->\n";4097print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4098}4099print"</td>\n";4100}4101print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4102-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4103"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4104-class=>"list", -title =>$pr->{'descr_long'}},4105 esc_html($pr->{'descr'})) ."</td>\n".4106"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4107print"<td class=\"". age_class($pr->{'age'}) ."\">".4108(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4109"<td class=\"link\">".4110$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4111$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4112$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4113$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4114($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4115"</td>\n".4116"</tr>\n";4117}4118if(defined$extra) {4119print"<tr>\n";4120if($check_forks) {4121print"<td></td>\n";4122}4123print"<td colspan=\"5\">$extra</td>\n".4124"</tr>\n";4125}4126print"</table>\n";4127}41284129sub git_shortlog_body {4130# uses global variable $project4131my($commitlist,$from,$to,$refs,$extra) =@_;41324133$from=0unlessdefined$from;4134$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41354136print"<table class=\"shortlog\">\n";4137my$alternate=1;4138for(my$i=$from;$i<=$to;$i++) {4139my%co= %{$commitlist->[$i]};4140my$commit=$co{'id'};4141my$ref= format_ref_marker($refs,$commit);4142if($alternate) {4143print"<tr class=\"dark\">\n";4144}else{4145print"<tr class=\"light\">\n";4146}4147$alternate^=1;4148my$author= chop_and_escape_str($co{'author_name'},10);4149# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4150print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4151"<td><i>".$author."</i></td>\n".4152"<td>";4153print format_subject_html($co{'title'},$co{'title_short'},4154 href(action=>"commit", hash=>$commit),$ref);4155print"</td>\n".4156"<td class=\"link\">".4157$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4158$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4159$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4160my$snapshot_links= format_snapshot_links($commit);4161if(defined$snapshot_links) {4162print" | ".$snapshot_links;4163}4164print"</td>\n".4165"</tr>\n";4166}4167if(defined$extra) {4168print"<tr>\n".4169"<td colspan=\"4\">$extra</td>\n".4170"</tr>\n";4171}4172print"</table>\n";4173}41744175sub git_history_body {4176# Warning: assumes constant type (blob or tree) during history4177my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;41784179$from=0unlessdefined$from;4180$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});41814182print"<table class=\"history\">\n";4183my$alternate=1;4184for(my$i=$from;$i<=$to;$i++) {4185my%co= %{$commitlist->[$i]};4186if(!%co) {4187next;4188}4189my$commit=$co{'id'};41904191my$ref= format_ref_marker($refs,$commit);41924193if($alternate) {4194print"<tr class=\"dark\">\n";4195}else{4196print"<tr class=\"light\">\n";4197}4198$alternate^=1;4199# shortlog uses chop_str($co{'author_name'}, 10)4200my$author= chop_and_escape_str($co{'author_name'},15,3);4201print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4202"<td><i>".$author."</i></td>\n".4203"<td>";4204# originally git_history used chop_str($co{'title'}, 50)4205print format_subject_html($co{'title'},$co{'title_short'},4206 href(action=>"commit", hash=>$commit),$ref);4207print"</td>\n".4208"<td class=\"link\">".4209$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4210$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");42114212if($ftypeeq'blob') {4213my$blob_current= git_get_hash_by_path($hash_base,$file_name);4214my$blob_parent= git_get_hash_by_path($commit,$file_name);4215if(defined$blob_current&&defined$blob_parent&&4216$blob_currentne$blob_parent) {4217print" | ".4218$cgi->a({-href => href(action=>"blobdiff",4219 hash=>$blob_current, hash_parent=>$blob_parent,4220 hash_base=>$hash_base, hash_parent_base=>$commit,4221 file_name=>$file_name)},4222"diff to current");4223}4224}4225print"</td>\n".4226"</tr>\n";4227}4228if(defined$extra) {4229print"<tr>\n".4230"<td colspan=\"4\">$extra</td>\n".4231"</tr>\n";4232}4233print"</table>\n";4234}42354236sub git_tags_body {4237# uses global variable $project4238my($taglist,$from,$to,$extra) =@_;4239$from=0unlessdefined$from;4240$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);42414242print"<table class=\"tags\">\n";4243my$alternate=1;4244for(my$i=$from;$i<=$to;$i++) {4245my$entry=$taglist->[$i];4246my%tag=%$entry;4247my$comment=$tag{'subject'};4248my$comment_short;4249if(defined$comment) {4250$comment_short= chop_str($comment,30,5);4251}4252if($alternate) {4253print"<tr class=\"dark\">\n";4254}else{4255print"<tr class=\"light\">\n";4256}4257$alternate^=1;4258if(defined$tag{'age'}) {4259print"<td><i>$tag{'age'}</i></td>\n";4260}else{4261print"<td></td>\n";4262}4263print"<td>".4264$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4265-class=>"list name"}, esc_html($tag{'name'})) .4266"</td>\n".4267"<td>";4268if(defined$comment) {4269print format_subject_html($comment,$comment_short,4270 href(action=>"tag", hash=>$tag{'id'}));4271}4272print"</td>\n".4273"<td class=\"selflink\">";4274if($tag{'type'}eq"tag") {4275print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4276}else{4277print" ";4278}4279print"</td>\n".4280"<td class=\"link\">"." | ".4281$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4282if($tag{'reftype'}eq"commit") {4283print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4284" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4285}elsif($tag{'reftype'}eq"blob") {4286print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4287}4288print"</td>\n".4289"</tr>";4290}4291if(defined$extra) {4292print"<tr>\n".4293"<td colspan=\"5\">$extra</td>\n".4294"</tr>\n";4295}4296print"</table>\n";4297}42984299sub git_heads_body {4300# uses global variable $project4301my($headlist,$head,$from,$to,$extra) =@_;4302$from=0unlessdefined$from;4303$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);43044305print"<table class=\"heads\">\n";4306my$alternate=1;4307for(my$i=$from;$i<=$to;$i++) {4308my$entry=$headlist->[$i];4309my%ref=%$entry;4310my$curr=$ref{'id'}eq$head;4311if($alternate) {4312print"<tr class=\"dark\">\n";4313}else{4314print"<tr class=\"light\">\n";4315}4316$alternate^=1;4317print"<td><i>$ref{'age'}</i></td>\n".4318($curr?"<td class=\"current_head\">":"<td>") .4319$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4320-class=>"list name"},esc_html($ref{'name'})) .4321"</td>\n".4322"<td class=\"link\">".4323$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4324$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4325$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4326"</td>\n".4327"</tr>";4328}4329if(defined$extra) {4330print"<tr>\n".4331"<td colspan=\"3\">$extra</td>\n".4332"</tr>\n";4333}4334print"</table>\n";4335}43364337sub git_search_grep_body {4338my($commitlist,$from,$to,$extra) =@_;4339$from=0unlessdefined$from;4340$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43414342print"<table class=\"commit_search\">\n";4343my$alternate=1;4344for(my$i=$from;$i<=$to;$i++) {4345my%co= %{$commitlist->[$i]};4346if(!%co) {4347next;4348}4349my$commit=$co{'id'};4350if($alternate) {4351print"<tr class=\"dark\">\n";4352}else{4353print"<tr class=\"light\">\n";4354}4355$alternate^=1;4356my$author= chop_and_escape_str($co{'author_name'},15,5);4357print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4358"<td><i>".$author."</i></td>\n".4359"<td>".4360$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4361-class=>"list subject"},4362 chop_and_escape_str($co{'title'},50) ."<br/>");4363my$comment=$co{'comment'};4364foreachmy$line(@$comment) {4365if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4366my($lead,$match,$trail) = ($1,$2,$3);4367$match= chop_str($match,70,5,'center');4368my$contextlen=int((80-length($match))/2);4369$contextlen=30if($contextlen>30);4370$lead= chop_str($lead,$contextlen,10,'left');4371$trail= chop_str($trail,$contextlen,10,'right');43724373$lead= esc_html($lead);4374$match= esc_html($match);4375$trail= esc_html($trail);43764377print"$lead<span class=\"match\">$match</span>$trail<br />";4378}4379}4380print"</td>\n".4381"<td class=\"link\">".4382$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4383" | ".4384$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4385" | ".4386$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4387print"</td>\n".4388"</tr>\n";4389}4390if(defined$extra) {4391print"<tr>\n".4392"<td colspan=\"3\">$extra</td>\n".4393"</tr>\n";4394}4395print"</table>\n";4396}43974398## ======================================================================4399## ======================================================================4400## actions44014402sub git_project_list {4403my$order=$input_params{'order'};4404if(defined$order&&$order!~m/none|project|descr|owner|age/) {4405 die_error(400,"Unknown order parameter");4406}44074408my@list= git_get_projects_list();4409if(!@list) {4410 die_error(404,"No projects found");4411}44124413 git_header_html();4414if(-f $home_text) {4415print"<div class=\"index_include\">\n";4416 insert_file($home_text);4417print"</div>\n";4418}4419print$cgi->startform(-method=>"get") .4420"<p class=\"projsearch\">Search:\n".4421$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4422"</p>".4423$cgi->end_form() ."\n";4424 git_project_list_body(\@list,$order);4425 git_footer_html();4426}44274428sub git_forks {4429my$order=$input_params{'order'};4430if(defined$order&&$order!~m/none|project|descr|owner|age/) {4431 die_error(400,"Unknown order parameter");4432}44334434my@list= git_get_projects_list($project);4435if(!@list) {4436 die_error(404,"No forks found");4437}44384439 git_header_html();4440 git_print_page_nav('','');4441 git_print_header_div('summary',"$projectforks");4442 git_project_list_body(\@list,$order);4443 git_footer_html();4444}44454446sub git_project_index {4447my@projects= git_get_projects_list($project);44484449print$cgi->header(4450-type =>'text/plain',4451-charset =>'utf-8',4452-content_disposition =>'inline; filename="index.aux"');44534454foreachmy$pr(@projects) {4455if(!exists$pr->{'owner'}) {4456$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4457}44584459my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4460# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4461$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4462$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4463$path=~s/ /\+/g;4464$owner=~s/ /\+/g;44654466print"$path$owner\n";4467}4468}44694470sub git_summary {4471my$descr= git_get_project_description($project) ||"none";4472my%co= parse_commit("HEAD");4473my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4474my$head=$co{'id'};44754476my$owner= git_get_project_owner($project);44774478my$refs= git_get_references();4479# These get_*_list functions return one more to allow us to see if4480# there are more ...4481my@taglist= git_get_tags_list(16);4482my@headlist= git_get_heads_list(16);4483my@forklist;4484my$check_forks= gitweb_check_feature('forks');44854486if($check_forks) {4487@forklist= git_get_projects_list($project);4488}44894490 git_header_html();4491 git_print_page_nav('summary','',$head);44924493print"<div class=\"title\"> </div>\n";4494print"<table class=\"projects_list\">\n".4495"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4496"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4497if(defined$cd{'rfc2822'}) {4498print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4499}45004501# use per project git URL list in $projectroot/$project/cloneurl4502# or make project git URL from git base URL and project name4503my$url_tag="URL";4504my@url_list= git_get_project_url_list($project);4505@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4506foreachmy$git_url(@url_list) {4507next unless$git_url;4508print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4509$url_tag="";4510}45114512# Tag cloud4513my$show_ctags= gitweb_check_feature('ctags');4514if($show_ctags) {4515my$ctags= git_get_project_ctags($project);4516my$cloud= git_populate_project_tagcloud($ctags);4517print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4518print"</td>\n<td>"unless%$ctags;4519print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4520print"</td>\n<td>"if%$ctags;4521print git_show_project_tagcloud($cloud,48);4522print"</td></tr>";4523}45244525print"</table>\n";45264527# If XSS prevention is on, we don't include README.html.4528# TODO: Allow a readme in some safe format.4529if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4530print"<div class=\"title\">readme</div>\n".4531"<div class=\"readme\">\n";4532 insert_file("$projectroot/$project/README.html");4533print"\n</div>\n";# class="readme"4534}45354536# we need to request one more than 16 (0..15) to check if4537# those 16 are all4538my@commitlist=$head? parse_commits($head,17) : ();4539if(@commitlist) {4540 git_print_header_div('shortlog');4541 git_shortlog_body(\@commitlist,0,15,$refs,4542$#commitlist<=15?undef:4543$cgi->a({-href => href(action=>"shortlog")},"..."));4544}45454546if(@taglist) {4547 git_print_header_div('tags');4548 git_tags_body(\@taglist,0,15,4549$#taglist<=15?undef:4550$cgi->a({-href => href(action=>"tags")},"..."));4551}45524553if(@headlist) {4554 git_print_header_div('heads');4555 git_heads_body(\@headlist,$head,0,15,4556$#headlist<=15?undef:4557$cgi->a({-href => href(action=>"heads")},"..."));4558}45594560if(@forklist) {4561 git_print_header_div('forks');4562 git_project_list_body(\@forklist,'age',0,15,4563$#forklist<=15?undef:4564$cgi->a({-href => href(action=>"forks")},"..."),4565'no_header');4566}45674568 git_footer_html();4569}45704571sub git_tag {4572my$head= git_get_head_hash($project);4573 git_header_html();4574 git_print_page_nav('','',$head,undef,$head);4575my%tag= parse_tag($hash);45764577if(!%tag) {4578 die_error(404,"Unknown tag object");4579}45804581 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4582print"<div class=\"title_text\">\n".4583"<table class=\"object_header\">\n".4584"<tr>\n".4585"<td>object</td>\n".4586"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4587$tag{'object'}) ."</td>\n".4588"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4589$tag{'type'}) ."</td>\n".4590"</tr>\n";4591if(defined($tag{'author'})) {4592my%ad= parse_date($tag{'epoch'},$tag{'tz'});4593print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4594print"<tr><td></td><td>".$ad{'rfc2822'} .4595sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4596"</td></tr>\n";4597}4598print"</table>\n\n".4599"</div>\n";4600print"<div class=\"page_body\">";4601my$comment=$tag{'comment'};4602foreachmy$line(@$comment) {4603chomp$line;4604print esc_html($line, -nbsp=>1) ."<br/>\n";4605}4606print"</div>\n";4607 git_footer_html();4608}46094610sub git_blame {4611# permissions4612 gitweb_check_feature('blame')4613or die_error(403,"Blame view not allowed");46144615# error checking4616 die_error(400,"No file name given")unless$file_name;4617$hash_base||= git_get_head_hash($project);4618 die_error(404,"Couldn't find base commit")unless$hash_base;4619my%co= parse_commit($hash_base)4620or die_error(404,"Commit not found");4621my$ftype="blob";4622if(!defined$hash) {4623$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4624or die_error(404,"Error looking up file");4625}else{4626$ftype= git_get_type($hash);4627if($ftype!~"blob") {4628 die_error(400,"Object is not a blob");4629}4630}46314632# run git-blame --porcelain4633open my$fd,"-|", git_cmd(),"blame",'-p',4634$hash_base,'--',$file_name4635or die_error(500,"Open git-blame failed");46364637# page header4638 git_header_html();4639my$formats_nav=4640$cgi->a({-href => href(action=>"blob", -replay=>1)},4641"blob") .4642" | ".4643$cgi->a({-href => href(action=>"history", -replay=>1)},4644"history") .4645" | ".4646$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4647"HEAD");4648 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4649 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4650 git_print_page_path($file_name,$ftype,$hash_base);46514652# page body4653my@rev_color=qw(light2 dark2);4654my$num_colors=scalar(@rev_color);4655my$current_color=0;4656my%metainfo= ();46574658print<<HTML;4659<div class="page_body">4660<table class="blame">4661<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4662HTML4663 LINE:4664while(my$line= <$fd>) {4665chomp$line;4666# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4667# no <lines in group> for subsequent lines in group of lines4668my($full_rev,$orig_lineno,$lineno,$group_size) =4669($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4670if(!exists$metainfo{$full_rev}) {4671$metainfo{$full_rev} = {};4672}4673my$meta=$metainfo{$full_rev};4674my$data;4675while($data= <$fd>) {4676chomp$data;4677last if($data=~s/^\t//);# contents of line4678if($data=~/^(\S+) (.*)$/) {4679$meta->{$1} =$2;4680}4681}4682my$short_rev=substr($full_rev,0,8);4683my$author=$meta->{'author'};4684my%date=4685 parse_date($meta->{'author-time'},$meta->{'author-tz'});4686my$date=$date{'iso-tz'};4687if($group_size) {4688$current_color= ($current_color+1) %$num_colors;4689}4690print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4691if($group_size) {4692print"<td class=\"sha1\"";4693print" title=\"". esc_html($author) .",$date\"";4694print" rowspan=\"$group_size\""if($group_size>1);4695print">";4696print$cgi->a({-href => href(action=>"commit",4697 hash=>$full_rev,4698 file_name=>$file_name)},4699 esc_html($short_rev));4700print"</td>\n";4701}4702my$parent_commit;4703if(!exists$meta->{'parent'}) {4704open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4705or die_error(500,"Open git-rev-parse failed");4706$parent_commit= <$dd>;4707close$dd;4708chomp($parent_commit);4709$meta->{'parent'} =$parent_commit;4710}else{4711$parent_commit=$meta->{'parent'};4712}4713my$blamed= href(action =>'blame',4714 file_name =>$meta->{'filename'},4715 hash_base =>$parent_commit);4716print"<td class=\"linenr\">";4717print$cgi->a({ -href =>"$blamed#l$orig_lineno",4718-class=>"linenr"},4719 esc_html($lineno));4720print"</td>";4721print"<td class=\"pre\">". esc_html($data) ."</td>\n";4722print"</tr>\n";4723}4724print"</table>\n";4725print"</div>";4726close$fd4727or print"Reading blob failed\n";47284729# page footer4730 git_footer_html();4731}47324733sub git_tags {4734my$head= git_get_head_hash($project);4735 git_header_html();4736 git_print_page_nav('','',$head,undef,$head);4737 git_print_header_div('summary',$project);47384739my@tagslist= git_get_tags_list();4740if(@tagslist) {4741 git_tags_body(\@tagslist);4742}4743 git_footer_html();4744}47454746sub git_heads {4747my$head= git_get_head_hash($project);4748 git_header_html();4749 git_print_page_nav('','',$head,undef,$head);4750 git_print_header_div('summary',$project);47514752my@headslist= git_get_heads_list();4753if(@headslist) {4754 git_heads_body(\@headslist,$head);4755}4756 git_footer_html();4757}47584759sub git_blob_plain {4760my$type=shift;4761my$expires;47624763if(!defined$hash) {4764if(defined$file_name) {4765my$base=$hash_base|| git_get_head_hash($project);4766$hash= git_get_hash_by_path($base,$file_name,"blob")4767or die_error(404,"Cannot find file");4768}else{4769 die_error(400,"No file name defined");4770}4771}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4772# blobs defined by non-textual hash id's can be cached4773$expires="+1d";4774}47754776open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4777or die_error(500,"Open git-cat-file blob '$hash' failed");47784779# content-type (can include charset)4780$type= blob_contenttype($fd,$file_name,$type);47814782# "save as" filename, even when no $file_name is given4783my$save_as="$hash";4784if(defined$file_name) {4785$save_as=$file_name;4786}elsif($type=~m/^text\//) {4787$save_as.='.txt';4788}47894790# With XSS prevention on, blobs of all types except a few known safe4791# ones are served with "Content-Disposition: attachment" to make sure4792# they don't run in our security domain. For certain image types,4793# blob view writes an <img> tag referring to blob_plain view, and we4794# want to be sure not to break that by serving the image as an4795# attachment (though Firefox 3 doesn't seem to care).4796my$sandbox=$prevent_xss&&4797$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;47984799print$cgi->header(4800-type =>$type,4801-expires =>$expires,4802-content_disposition =>4803($sandbox?'attachment':'inline')4804.'; filename="'.$save_as.'"');4805undef$/;4806binmode STDOUT,':raw';4807print<$fd>;4808binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4809$/="\n";4810close$fd;4811}48124813sub git_blob {4814my$expires;48154816if(!defined$hash) {4817if(defined$file_name) {4818my$base=$hash_base|| git_get_head_hash($project);4819$hash= git_get_hash_by_path($base,$file_name,"blob")4820or die_error(404,"Cannot find file");4821}else{4822 die_error(400,"No file name defined");4823}4824}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4825# blobs defined by non-textual hash id's can be cached4826$expires="+1d";4827}48284829my$have_blame= gitweb_check_feature('blame');4830open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4831or die_error(500,"Couldn't cat$file_name,$hash");4832my$mimetype= blob_mimetype($fd,$file_name);4833if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4834close$fd;4835return git_blob_plain($mimetype);4836}4837# we can have blame only for text/* mimetype4838$have_blame&&= ($mimetype=~m!^text/!);48394840 git_header_html(undef,$expires);4841my$formats_nav='';4842if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4843if(defined$file_name) {4844if($have_blame) {4845$formats_nav.=4846$cgi->a({-href => href(action=>"blame", -replay=>1)},4847"blame") .4848" | ";4849}4850$formats_nav.=4851$cgi->a({-href => href(action=>"history", -replay=>1)},4852"history") .4853" | ".4854$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4855"raw") .4856" | ".4857$cgi->a({-href => href(action=>"blob",4858 hash_base=>"HEAD", file_name=>$file_name)},4859"HEAD");4860}else{4861$formats_nav.=4862$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4863"raw");4864}4865 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4866 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4867}else{4868print"<div class=\"page_nav\">\n".4869"<br/><br/></div>\n".4870"<div class=\"title\">$hash</div>\n";4871}4872 git_print_page_path($file_name,"blob",$hash_base);4873print"<div class=\"page_body\">\n";4874if($mimetype=~m!^image/!) {4875print qq!<img type="$mimetype"!;4876if($file_name) {4877print qq! alt="$file_name" title="$file_name"!;4878}4879print qq! src="! .4880 href(action=>"blob_plain", hash=>$hash,4881 hash_base=>$hash_base, file_name=>$file_name) .4882 qq!"/>\n!;4883}else{4884my$nr;4885while(my$line= <$fd>) {4886chomp$line;4887$nr++;4888$line= untabify($line);4889printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4890$nr,$nr,$nr, esc_html($line, -nbsp=>1);4891}4892}4893close$fd4894or print"Reading blob failed.\n";4895print"</div>";4896 git_footer_html();4897}48984899sub git_tree {4900if(!defined$hash_base) {4901$hash_base="HEAD";4902}4903if(!defined$hash) {4904if(defined$file_name) {4905$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4906}else{4907$hash=$hash_base;4908}4909}4910 die_error(404,"No such tree")unlessdefined($hash);4911$/="\0";4912open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4913or die_error(500,"Open git-ls-tree failed");4914my@entries=map{chomp;$_} <$fd>;4915close$fdor die_error(404,"Reading tree failed");4916$/="\n";49174918my$refs= git_get_references();4919my$ref= format_ref_marker($refs,$hash_base);4920 git_header_html();4921my$basedir='';4922my$have_blame= gitweb_check_feature('blame');4923if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4924my@views_nav= ();4925if(defined$file_name) {4926push@views_nav,4927$cgi->a({-href => href(action=>"history", -replay=>1)},4928"history"),4929$cgi->a({-href => href(action=>"tree",4930 hash_base=>"HEAD", file_name=>$file_name)},4931"HEAD"),4932}4933my$snapshot_links= format_snapshot_links($hash);4934if(defined$snapshot_links) {4935# FIXME: Should be available when we have no hash base as well.4936push@views_nav,$snapshot_links;4937}4938 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4939 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4940}else{4941undef$hash_base;4942print"<div class=\"page_nav\">\n";4943print"<br/><br/></div>\n";4944print"<div class=\"title\">$hash</div>\n";4945}4946if(defined$file_name) {4947$basedir=$file_name;4948if($basedirne''&&substr($basedir, -1)ne'/') {4949$basedir.='/';4950}4951 git_print_page_path($file_name,'tree',$hash_base);4952}4953print"<div class=\"page_body\">\n";4954print"<table class=\"tree\">\n";4955my$alternate=1;4956# '..' (top directory) link if possible4957if(defined$hash_base&&4958defined$file_name&&$file_name=~m![^/]+$!) {4959if($alternate) {4960print"<tr class=\"dark\">\n";4961}else{4962print"<tr class=\"light\">\n";4963}4964$alternate^=1;49654966my$up=$file_name;4967$up=~s!/?[^/]+$!!;4968undef$upunless$up;4969# based on git_print_tree_entry4970print'<td class="mode">'. mode_str('040000') ."</td>\n";4971print'<td class="list">';4972print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4973 file_name=>$up)},4974"..");4975print"</td>\n";4976print"<td class=\"link\"></td>\n";49774978print"</tr>\n";4979}4980foreachmy$line(@entries) {4981my%t= parse_ls_tree_line($line, -z =>1);49824983if($alternate) {4984print"<tr class=\"dark\">\n";4985}else{4986print"<tr class=\"light\">\n";4987}4988$alternate^=1;49894990 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);49914992print"</tr>\n";4993}4994print"</table>\n".4995"</div>";4996 git_footer_html();4997}49984999sub git_snapshot {5000my$format=$input_params{'snapshot_format'};5001if(!@snapshot_fmts) {5002 die_error(403,"Snapshots not allowed");5003}5004# default to first supported snapshot format5005$format||=$snapshot_fmts[0];5006if($format!~m/^[a-z0-9]+$/) {5007 die_error(400,"Invalid snapshot format parameter");5008}elsif(!exists($known_snapshot_formats{$format})) {5009 die_error(400,"Unknown snapshot format");5010}elsif(!grep($_eq$format,@snapshot_fmts)) {5011 die_error(403,"Unsupported snapshot format");5012}50135014if(!defined$hash) {5015$hash= git_get_head_hash($project);5016}50175018my$name=$project;5019$name=~ s,([^/])/*\.git$,$1,;5020$name= basename($name);5021my$filename= to_utf8($name);5022$name=~s/\047/\047\\\047\047/g;5023my$cmd;5024$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5025$cmd= quote_command(5026 git_cmd(),'archive',5027"--format=$known_snapshot_formats{$format}{'format'}",5028"--prefix=$name/",$hash);5029if(exists$known_snapshot_formats{$format}{'compressor'}) {5030$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5031}50325033print$cgi->header(5034-type =>$known_snapshot_formats{$format}{'type'},5035-content_disposition =>'inline; filename="'."$filename".'"',5036-status =>'200 OK');50375038open my$fd,"-|",$cmd5039or die_error(500,"Execute git-archive failed");5040binmode STDOUT,':raw';5041print<$fd>;5042binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5043close$fd;5044}50455046sub git_log {5047my$head= git_get_head_hash($project);5048if(!defined$hash) {5049$hash=$head;5050}5051if(!defined$page) {5052$page=0;5053}5054my$refs= git_get_references();50555056my@commitlist= parse_commits($hash,101, (100*$page));50575058my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);50595060my($patch_max) = gitweb_get_feature('patches');5061if($patch_max) {5062if($patch_max<0||@commitlist<=$patch_max) {5063$paging_nav.=" ⋅ ".5064$cgi->a({-href => href(action=>"patches", -replay=>1)},5065"patches");5066}5067}50685069 git_header_html();5070 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);50715072if(!@commitlist) {5073my%co= parse_commit($hash);50745075 git_print_header_div('summary',$project);5076print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5077}5078my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5079for(my$i=0;$i<=$to;$i++) {5080my%co= %{$commitlist[$i]};5081next if!%co;5082my$commit=$co{'id'};5083my$ref= format_ref_marker($refs,$commit);5084my%ad= parse_date($co{'author_epoch'});5085 git_print_header_div('commit',5086"<span class=\"age\">$co{'age_string'}</span>".5087 esc_html($co{'title'}) .$ref,5088$commit);5089print"<div class=\"title_text\">\n".5090"<div class=\"log_link\">\n".5091$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5092" | ".5093$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5094" | ".5095$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5096"<br/>\n".5097"</div>\n".5098"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".5099"</div>\n";51005101print"<div class=\"log_body\">\n";5102 git_print_log($co{'comment'}, -final_empty_line=>1);5103print"</div>\n";5104}5105if($#commitlist>=100) {5106print"<div class=\"page_nav\">\n";5107print$cgi->a({-href => href(-replay=>1, page=>$page+1),5108-accesskey =>"n", -title =>"Alt-n"},"next");5109print"</div>\n";5110}5111 git_footer_html();5112}51135114sub git_commit {5115$hash||=$hash_base||"HEAD";5116my%co= parse_commit($hash)5117or die_error(404,"Unknown commit object");5118my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5119my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});51205121my$parent=$co{'parent'};5122my$parents=$co{'parents'};# listref51235124# we need to prepare $formats_nav before any parameter munging5125my$formats_nav;5126if(!defined$parent) {5127# --root commitdiff5128$formats_nav.='(initial)';5129}elsif(@$parents==1) {5130# single parent commit5131$formats_nav.=5132'(parent: '.5133$cgi->a({-href => href(action=>"commit",5134 hash=>$parent)},5135 esc_html(substr($parent,0,7))) .5136')';5137}else{5138# merge commit5139$formats_nav.=5140'(merge: '.5141join(' ',map{5142$cgi->a({-href => href(action=>"commit",5143 hash=>$_)},5144 esc_html(substr($_,0,7)));5145}@$parents) .5146')';5147}5148if(gitweb_check_feature('patches')) {5149$formats_nav.=" | ".5150$cgi->a({-href => href(action=>"patch", -replay=>1)},5151"patch");5152}51535154if(!defined$parent) {5155$parent="--root";5156}5157my@difftree;5158open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5159@diff_opts,5160(@$parents<=1?$parent:'-c'),5161$hash,"--"5162or die_error(500,"Open git-diff-tree failed");5163@difftree=map{chomp;$_} <$fd>;5164close$fdor die_error(404,"Reading git-diff-tree failed");51655166# non-textual hash id's can be cached5167my$expires;5168if($hash=~m/^[0-9a-fA-F]{40}$/) {5169$expires="+1d";5170}5171my$refs= git_get_references();5172my$ref= format_ref_marker($refs,$co{'id'});51735174 git_header_html(undef,$expires);5175 git_print_page_nav('commit','',5176$hash,$co{'tree'},$hash,5177$formats_nav);51785179if(defined$co{'parent'}) {5180 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5181}else{5182 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5183}5184print"<div class=\"title_text\">\n".5185"<table class=\"object_header\">\n";5186print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5187"<tr>".5188"<td></td><td>$ad{'rfc2822'}";5189if($ad{'hour_local'} <6) {5190printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5191$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5192}else{5193printf(" (%02d:%02d%s)",5194$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5195}5196print"</td>".5197"</tr>\n";5198print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5199print"<tr><td></td><td>$cd{'rfc2822'}".5200sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5201"</td></tr>\n";5202print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5203print"<tr>".5204"<td>tree</td>".5205"<td class=\"sha1\">".5206$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5207class=>"list"},$co{'tree'}) .5208"</td>".5209"<td class=\"link\">".5210$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5211"tree");5212my$snapshot_links= format_snapshot_links($hash);5213if(defined$snapshot_links) {5214print" | ".$snapshot_links;5215}5216print"</td>".5217"</tr>\n";52185219foreachmy$par(@$parents) {5220print"<tr>".5221"<td>parent</td>".5222"<td class=\"sha1\">".5223$cgi->a({-href => href(action=>"commit", hash=>$par),5224class=>"list"},$par) .5225"</td>".5226"<td class=\"link\">".5227$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5228" | ".5229$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5230"</td>".5231"</tr>\n";5232}5233print"</table>".5234"</div>\n";52355236print"<div class=\"page_body\">\n";5237 git_print_log($co{'comment'});5238print"</div>\n";52395240 git_difftree_body(\@difftree,$hash,@$parents);52415242 git_footer_html();5243}52445245sub git_object {5246# object is defined by:5247# - hash or hash_base alone5248# - hash_base and file_name5249my$type;52505251# - hash or hash_base alone5252if($hash|| ($hash_base&& !defined$file_name)) {5253my$object_id=$hash||$hash_base;52545255open my$fd,"-|", quote_command(5256 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5257or die_error(404,"Object does not exist");5258$type= <$fd>;5259chomp$type;5260close$fd5261or die_error(404,"Object does not exist");52625263# - hash_base and file_name5264}elsif($hash_base&&defined$file_name) {5265$file_name=~ s,/+$,,;52665267system(git_cmd(),"cat-file",'-e',$hash_base) ==05268or die_error(404,"Base object does not exist");52695270# here errors should not hapen5271open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5272or die_error(500,"Open git-ls-tree failed");5273my$line= <$fd>;5274close$fd;52755276#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5277unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5278 die_error(404,"File or directory for given base does not exist");5279}5280$type=$2;5281$hash=$3;5282}else{5283 die_error(400,"Not enough information to find object");5284}52855286print$cgi->redirect(-uri => href(action=>$type, -full=>1,5287 hash=>$hash, hash_base=>$hash_base,5288 file_name=>$file_name),5289-status =>'302 Found');5290}52915292sub git_blobdiff {5293my$format=shift||'html';52945295my$fd;5296my@difftree;5297my%diffinfo;5298my$expires;52995300# preparing $fd and %diffinfo for git_patchset_body5301# new style URI5302if(defined$hash_base&&defined$hash_parent_base) {5303if(defined$file_name) {5304# read raw output5305open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5306$hash_parent_base,$hash_base,5307"--", (defined$file_parent?$file_parent: ()),$file_name5308or die_error(500,"Open git-diff-tree failed");5309@difftree=map{chomp;$_} <$fd>;5310close$fd5311or die_error(404,"Reading git-diff-tree failed");5312@difftree5313or die_error(404,"Blob diff not found");53145315}elsif(defined$hash&&5316$hash=~/[0-9a-fA-F]{40}/) {5317# try to find filename from $hash53185319# read filtered raw output5320open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5321$hash_parent_base,$hash_base,"--"5322or die_error(500,"Open git-diff-tree failed");5323@difftree=5324# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5325# $hash == to_id5326grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5327map{chomp;$_} <$fd>;5328close$fd5329or die_error(404,"Reading git-diff-tree failed");5330@difftree5331or die_error(404,"Blob diff not found");53325333}else{5334 die_error(400,"Missing one of the blob diff parameters");5335}53365337if(@difftree>1) {5338 die_error(400,"Ambiguous blob diff specification");5339}53405341%diffinfo= parse_difftree_raw_line($difftree[0]);5342$file_parent||=$diffinfo{'from_file'} ||$file_name;5343$file_name||=$diffinfo{'to_file'};53445345$hash_parent||=$diffinfo{'from_id'};5346$hash||=$diffinfo{'to_id'};53475348# non-textual hash id's can be cached5349if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5350$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5351$expires='+1d';5352}53535354# open patch output5355open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5356'-p', ($formateq'html'?"--full-index": ()),5357$hash_parent_base,$hash_base,5358"--", (defined$file_parent?$file_parent: ()),$file_name5359or die_error(500,"Open git-diff-tree failed");5360}53615362# old/legacy style URI -- not generated anymore since 1.4.3.5363if(!%diffinfo) {5364 die_error('404 Not Found',"Missing one of the blob diff parameters")5365}53665367# header5368if($formateq'html') {5369my$formats_nav=5370$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5371"raw");5372 git_header_html(undef,$expires);5373if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5374 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5375 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5376}else{5377print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5378print"<div class=\"title\">$hashvs$hash_parent</div>\n";5379}5380if(defined$file_name) {5381 git_print_page_path($file_name,"blob",$hash_base);5382}else{5383print"<div class=\"page_path\"></div>\n";5384}53855386}elsif($formateq'plain') {5387print$cgi->header(5388-type =>'text/plain',5389-charset =>'utf-8',5390-expires =>$expires,5391-content_disposition =>'inline; filename="'."$file_name".'.patch"');53925393print"X-Git-Url: ".$cgi->self_url() ."\n\n";53945395}else{5396 die_error(400,"Unknown blobdiff format");5397}53985399# patch5400if($formateq'html') {5401print"<div class=\"page_body\">\n";54025403 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5404close$fd;54055406print"</div>\n";# class="page_body"5407 git_footer_html();54085409}else{5410while(my$line= <$fd>) {5411$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5412$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;54135414print$line;54155416last if$line=~m!^\+\+\+!;5417}5418local$/=undef;5419print<$fd>;5420close$fd;5421}5422}54235424sub git_blobdiff_plain {5425 git_blobdiff('plain');5426}54275428sub git_commitdiff {5429my%params=@_;5430my$format=$params{-format} ||'html';54315432my($patch_max) = gitweb_get_feature('patches');5433if($formateq'patch') {5434 die_error(403,"Patch view not allowed")unless$patch_max;5435}54365437$hash||=$hash_base||"HEAD";5438my%co= parse_commit($hash)5439or die_error(404,"Unknown commit object");54405441# choose format for commitdiff for merge5442if(!defined$hash_parent&& @{$co{'parents'}} >1) {5443$hash_parent='--cc';5444}5445# we need to prepare $formats_nav before almost any parameter munging5446my$formats_nav;5447if($formateq'html') {5448$formats_nav=5449$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5450"raw");5451if($patch_max) {5452$formats_nav.=" | ".5453$cgi->a({-href => href(action=>"patch", -replay=>1)},5454"patch");5455}54565457if(defined$hash_parent&&5458$hash_parentne'-c'&&$hash_parentne'--cc') {5459# commitdiff with two commits given5460my$hash_parent_short=$hash_parent;5461if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5462$hash_parent_short=substr($hash_parent,0,7);5463}5464$formats_nav.=5465' (from';5466for(my$i=0;$i< @{$co{'parents'}};$i++) {5467if($co{'parents'}[$i]eq$hash_parent) {5468$formats_nav.=' parent '. ($i+1);5469last;5470}5471}5472$formats_nav.=': '.5473$cgi->a({-href => href(action=>"commitdiff",5474 hash=>$hash_parent)},5475 esc_html($hash_parent_short)) .5476')';5477}elsif(!$co{'parent'}) {5478# --root commitdiff5479$formats_nav.=' (initial)';5480}elsif(scalar@{$co{'parents'}} ==1) {5481# single parent commit5482$formats_nav.=5483' (parent: '.5484$cgi->a({-href => href(action=>"commitdiff",5485 hash=>$co{'parent'})},5486 esc_html(substr($co{'parent'},0,7))) .5487')';5488}else{5489# merge commit5490if($hash_parenteq'--cc') {5491$formats_nav.=' | '.5492$cgi->a({-href => href(action=>"commitdiff",5493 hash=>$hash, hash_parent=>'-c')},5494'combined');5495}else{# $hash_parent eq '-c'5496$formats_nav.=' | '.5497$cgi->a({-href => href(action=>"commitdiff",5498 hash=>$hash, hash_parent=>'--cc')},5499'compact');5500}5501$formats_nav.=5502' (merge: '.5503join(' ',map{5504$cgi->a({-href => href(action=>"commitdiff",5505 hash=>$_)},5506 esc_html(substr($_,0,7)));5507} @{$co{'parents'}} ) .5508')';5509}5510}55115512my$hash_parent_param=$hash_parent;5513if(!defined$hash_parent_param) {5514# --cc for multiple parents, --root for parentless5515$hash_parent_param=5516@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5517}55185519# read commitdiff5520my$fd;5521my@difftree;5522if($formateq'html') {5523open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5524"--no-commit-id","--patch-with-raw","--full-index",5525$hash_parent_param,$hash,"--"5526or die_error(500,"Open git-diff-tree failed");55275528while(my$line= <$fd>) {5529chomp$line;5530# empty line ends raw part of diff-tree output5531last unless$line;5532push@difftree,scalar parse_difftree_raw_line($line);5533}55345535}elsif($formateq'plain') {5536open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5537'-p',$hash_parent_param,$hash,"--"5538or die_error(500,"Open git-diff-tree failed");5539}elsif($formateq'patch') {5540# For commit ranges, we limit the output to the number of5541# patches specified in the 'patches' feature.5542# For single commits, we limit the output to a single patch,5543# diverging from the git-format-patch default.5544my@commit_spec= ();5545if($hash_parent) {5546if($patch_max>0) {5547push@commit_spec,"-$patch_max";5548}5549push@commit_spec,'-n',"$hash_parent..$hash";5550}else{5551if($params{-single}) {5552push@commit_spec,'-1';5553}else{5554if($patch_max>0) {5555push@commit_spec,"-$patch_max";5556}5557push@commit_spec,"-n";5558}5559push@commit_spec,'--root',$hash;5560}5561open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5562'--stdout',@commit_spec5563or die_error(500,"Open git-format-patch failed");5564}else{5565 die_error(400,"Unknown commitdiff format");5566}55675568# non-textual hash id's can be cached5569my$expires;5570if($hash=~m/^[0-9a-fA-F]{40}$/) {5571$expires="+1d";5572}55735574# write commit message5575if($formateq'html') {5576my$refs= git_get_references();5577my$ref= format_ref_marker($refs,$co{'id'});55785579 git_header_html(undef,$expires);5580 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5581 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5582 git_print_authorship(\%co);5583print"<div class=\"page_body\">\n";5584if(@{$co{'comment'}} >1) {5585print"<div class=\"log\">\n";5586 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5587print"</div>\n";# class="log"5588}55895590}elsif($formateq'plain') {5591my$refs= git_get_references("tags");5592my$tagname= git_get_rev_name_tags($hash);5593my$filename= basename($project) ."-$hash.patch";55945595print$cgi->header(5596-type =>'text/plain',5597-charset =>'utf-8',5598-expires =>$expires,5599-content_disposition =>'inline; filename="'."$filename".'"');5600my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5601print"From: ". to_utf8($co{'author'}) ."\n";5602print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5603print"Subject: ". to_utf8($co{'title'}) ."\n";56045605print"X-Git-Tag:$tagname\n"if$tagname;5606print"X-Git-Url: ".$cgi->self_url() ."\n\n";56075608foreachmy$line(@{$co{'comment'}}) {5609print to_utf8($line) ."\n";5610}5611print"---\n\n";5612}elsif($formateq'patch') {5613my$filename= basename($project) ."-$hash.patch";56145615print$cgi->header(5616-type =>'text/plain',5617-charset =>'utf-8',5618-expires =>$expires,5619-content_disposition =>'inline; filename="'."$filename".'"');5620}56215622# write patch5623if($formateq'html') {5624my$use_parents= !defined$hash_parent||5625$hash_parenteq'-c'||$hash_parenteq'--cc';5626 git_difftree_body(\@difftree,$hash,5627$use_parents? @{$co{'parents'}} :$hash_parent);5628print"<br/>\n";56295630 git_patchset_body($fd, \@difftree,$hash,5631$use_parents? @{$co{'parents'}} :$hash_parent);5632close$fd;5633print"</div>\n";# class="page_body"5634 git_footer_html();56355636}elsif($formateq'plain') {5637local$/=undef;5638print<$fd>;5639close$fd5640or print"Reading git-diff-tree failed\n";5641}elsif($formateq'patch') {5642local$/=undef;5643print<$fd>;5644close$fd5645or print"Reading git-format-patch failed\n";5646}5647}56485649sub git_commitdiff_plain {5650 git_commitdiff(-format =>'plain');5651}56525653# format-patch-style patches5654sub git_patch {5655 git_commitdiff(-format =>'patch', -single=>1);5656}56575658sub git_patches {5659 git_commitdiff(-format =>'patch');5660}56615662sub git_history {5663if(!defined$hash_base) {5664$hash_base= git_get_head_hash($project);5665}5666if(!defined$page) {5667$page=0;5668}5669my$ftype;5670my%co= parse_commit($hash_base)5671or die_error(404,"Unknown commit object");56725673my$refs= git_get_references();5674my$limit=sprintf("--max-count=%i", (100* ($page+1)));56755676my@commitlist= parse_commits($hash_base,101, (100*$page),5677$file_name,"--full-history")5678or die_error(404,"No such file or directory on given branch");56795680if(!defined$hash&&defined$file_name) {5681# some commits could have deleted file in question,5682# and not have it in tree, but one of them has to have it5683for(my$i=0;$i<=@commitlist;$i++) {5684$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5685last ifdefined$hash;5686}5687}5688if(defined$hash) {5689$ftype= git_get_type($hash);5690}5691if(!defined$ftype) {5692 die_error(500,"Unknown type of object");5693}56945695my$paging_nav='';5696if($page>0) {5697$paging_nav.=5698$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5699 file_name=>$file_name)},5700"first");5701$paging_nav.=" ⋅ ".5702$cgi->a({-href => href(-replay=>1, page=>$page-1),5703-accesskey =>"p", -title =>"Alt-p"},"prev");5704}else{5705$paging_nav.="first";5706$paging_nav.=" ⋅ prev";5707}5708my$next_link='';5709if($#commitlist>=100) {5710$next_link=5711$cgi->a({-href => href(-replay=>1, page=>$page+1),5712-accesskey =>"n", -title =>"Alt-n"},"next");5713$paging_nav.=" ⋅$next_link";5714}else{5715$paging_nav.=" ⋅ next";5716}57175718 git_header_html();5719 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5720 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5721 git_print_page_path($file_name,$ftype,$hash_base);57225723 git_history_body(\@commitlist,0,99,5724$refs,$hash_base,$ftype,$next_link);57255726 git_footer_html();5727}57285729sub git_search {5730 gitweb_check_feature('search')or die_error(403,"Search is disabled");5731if(!defined$searchtext) {5732 die_error(400,"Text field is empty");5733}5734if(!defined$hash) {5735$hash= git_get_head_hash($project);5736}5737my%co= parse_commit($hash);5738if(!%co) {5739 die_error(404,"Unknown commit object");5740}5741if(!defined$page) {5742$page=0;5743}57445745$searchtype||='commit';5746if($searchtypeeq'pickaxe') {5747# pickaxe may take all resources of your box and run for several minutes5748# with every query - so decide by yourself how public you make this feature5749 gitweb_check_feature('pickaxe')5750or die_error(403,"Pickaxe is disabled");5751}5752if($searchtypeeq'grep') {5753 gitweb_check_feature('grep')5754or die_error(403,"Grep is disabled");5755}57565757 git_header_html();57585759if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5760my$greptype;5761if($searchtypeeq'commit') {5762$greptype="--grep=";5763}elsif($searchtypeeq'author') {5764$greptype="--author=";5765}elsif($searchtypeeq'committer') {5766$greptype="--committer=";5767}5768$greptype.=$searchtext;5769my@commitlist= parse_commits($hash,101, (100*$page),undef,5770$greptype,'--regexp-ignore-case',5771$search_use_regexp?'--extended-regexp':'--fixed-strings');57725773my$paging_nav='';5774if($page>0) {5775$paging_nav.=5776$cgi->a({-href => href(action=>"search", hash=>$hash,5777 searchtext=>$searchtext,5778 searchtype=>$searchtype)},5779"first");5780$paging_nav.=" ⋅ ".5781$cgi->a({-href => href(-replay=>1, page=>$page-1),5782-accesskey =>"p", -title =>"Alt-p"},"prev");5783}else{5784$paging_nav.="first";5785$paging_nav.=" ⋅ prev";5786}5787my$next_link='';5788if($#commitlist>=100) {5789$next_link=5790$cgi->a({-href => href(-replay=>1, page=>$page+1),5791-accesskey =>"n", -title =>"Alt-n"},"next");5792$paging_nav.=" ⋅$next_link";5793}else{5794$paging_nav.=" ⋅ next";5795}57965797if($#commitlist>=100) {5798}57995800 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5801 git_print_header_div('commit', esc_html($co{'title'}),$hash);5802 git_search_grep_body(\@commitlist,0,99,$next_link);5803}58045805if($searchtypeeq'pickaxe') {5806 git_print_page_nav('','',$hash,$co{'tree'},$hash);5807 git_print_header_div('commit', esc_html($co{'title'}),$hash);58085809print"<table class=\"pickaxe search\">\n";5810my$alternate=1;5811$/="\n";5812open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5813'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5814($search_use_regexp?'--pickaxe-regex': ());5815undef%co;5816my@files;5817while(my$line= <$fd>) {5818chomp$line;5819next unless$line;58205821my%set= parse_difftree_raw_line($line);5822if(defined$set{'commit'}) {5823# finish previous commit5824if(%co) {5825print"</td>\n".5826"<td class=\"link\">".5827$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5828" | ".5829$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5830print"</td>\n".5831"</tr>\n";5832}58335834if($alternate) {5835print"<tr class=\"dark\">\n";5836}else{5837print"<tr class=\"light\">\n";5838}5839$alternate^=1;5840%co= parse_commit($set{'commit'});5841my$author= chop_and_escape_str($co{'author_name'},15,5);5842print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5843"<td><i>$author</i></td>\n".5844"<td>".5845$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5846-class=>"list subject"},5847 chop_and_escape_str($co{'title'},50) ."<br/>");5848}elsif(defined$set{'to_id'}) {5849next if($set{'to_id'} =~m/^0{40}$/);58505851print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5852 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5853-class=>"list"},5854"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5855"<br/>\n";5856}5857}5858close$fd;58595860# finish last commit (warning: repetition!)5861if(%co) {5862print"</td>\n".5863"<td class=\"link\">".5864$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5865" | ".5866$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5867print"</td>\n".5868"</tr>\n";5869}58705871print"</table>\n";5872}58735874if($searchtypeeq'grep') {5875 git_print_page_nav('','',$hash,$co{'tree'},$hash);5876 git_print_header_div('commit', esc_html($co{'title'}),$hash);58775878print"<table class=\"grep_search\">\n";5879my$alternate=1;5880my$matches=0;5881$/="\n";5882open my$fd,"-|", git_cmd(),'grep','-n',5883$search_use_regexp? ('-E','-i') :'-F',5884$searchtext,$co{'tree'};5885my$lastfile='';5886while(my$line= <$fd>) {5887chomp$line;5888my($file,$lno,$ltext,$binary);5889last if($matches++>1000);5890if($line=~/^Binary file (.+) matches$/) {5891$file=$1;5892$binary=1;5893}else{5894(undef,$file,$lno,$ltext) =split(/:/,$line,4);5895}5896if($filene$lastfile) {5897$lastfileand print"</td></tr>\n";5898if($alternate++) {5899print"<tr class=\"dark\">\n";5900}else{5901print"<tr class=\"light\">\n";5902}5903print"<td class=\"list\">".5904$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5905 file_name=>"$file"),5906-class=>"list"}, esc_path($file));5907print"</td><td>\n";5908$lastfile=$file;5909}5910if($binary) {5911print"<div class=\"binary\">Binary file</div>\n";5912}else{5913$ltext= untabify($ltext);5914if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5915$ltext= esc_html($1, -nbsp=>1);5916$ltext.='<span class="match">';5917$ltext.= esc_html($2, -nbsp=>1);5918$ltext.='</span>';5919$ltext.= esc_html($3, -nbsp=>1);5920}else{5921$ltext= esc_html($ltext, -nbsp=>1);5922}5923print"<div class=\"pre\">".5924$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5925 file_name=>"$file").'#l'.$lno,5926-class=>"linenr"},sprintf('%4i',$lno))5927.' '.$ltext."</div>\n";5928}5929}5930if($lastfile) {5931print"</td></tr>\n";5932if($matches>1000) {5933print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5934}5935}else{5936print"<div class=\"diff nodifferences\">No matches found</div>\n";5937}5938close$fd;59395940print"</table>\n";5941}5942 git_footer_html();5943}59445945sub git_search_help {5946 git_header_html();5947 git_print_page_nav('','',$hash,$hash,$hash);5948print<<EOT;5949<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5950regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5951the pattern entered is recognized as the POSIX extended5952<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5953insensitive).</p>5954<dl>5955<dt><b>commit</b></dt>5956<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5957EOT5958my$have_grep= gitweb_check_feature('grep');5959if($have_grep) {5960print<<EOT;5961<dt><b>grep</b></dt>5962<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5963 a different one) are searched for the given pattern. On large trees, this search can take5964a while and put some strain on the server, so please use it with some consideration. Note that5965due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5966case-sensitive.</dd>5967EOT5968}5969print<<EOT;5970<dt><b>author</b></dt>5971<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5972<dt><b>committer</b></dt>5973<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5974EOT5975my$have_pickaxe= gitweb_check_feature('pickaxe');5976if($have_pickaxe) {5977print<<EOT;5978<dt><b>pickaxe</b></dt>5979<dd>All commits that caused the string to appear or disappear from any file (changes that5980added, removed or "modified" the string) will be listed. This search can take a while and5981takes a lot of strain on the server, so please use it wisely. Note that since you may be5982interested even in changes just changing the case as well, this search is case sensitive.</dd>5983EOT5984}5985print"</dl>\n";5986 git_footer_html();5987}59885989sub git_shortlog {5990my$head= git_get_head_hash($project);5991if(!defined$hash) {5992$hash=$head;5993}5994if(!defined$page) {5995$page=0;5996}5997my$refs= git_get_references();59985999my$commit_hash=$hash;6000if(defined$hash_parent) {6001$commit_hash="$hash_parent..$hash";6002}6003my@commitlist= parse_commits($commit_hash,101, (100*$page));60046005my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6006my$next_link='';6007if($#commitlist>=100) {6008$next_link=6009$cgi->a({-href => href(-replay=>1, page=>$page+1),6010-accesskey =>"n", -title =>"Alt-n"},"next");6011}6012my$patch_max= gitweb_check_feature('patches');6013if($patch_max) {6014if($patch_max<0||@commitlist<=$patch_max) {6015$paging_nav.=" ⋅ ".6016$cgi->a({-href => href(action=>"patches", -replay=>1)},6017"patches");6018}6019}60206021 git_header_html();6022 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6023 git_print_header_div('summary',$project);60246025 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);60266027 git_footer_html();6028}60296030## ......................................................................6031## feeds (RSS, Atom; OPML)60326033sub git_feed {6034my$format=shift||'atom';6035my$have_blame= gitweb_check_feature('blame');60366037# Atom: http://www.atomenabled.org/developers/syndication/6038# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6039if($formatne'rss'&&$formatne'atom') {6040 die_error(400,"Unknown web feed format");6041}60426043# log/feed of current (HEAD) branch, log of given branch, history of file/directory6044my$head=$hash||'HEAD';6045my@commitlist= parse_commits($head,150,0,$file_name);60466047my%latest_commit;6048my%latest_date;6049my$content_type="application/$format+xml";6050if(defined$cgi->http('HTTP_ACCEPT') &&6051$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6052# browser (feed reader) prefers text/xml6053$content_type='text/xml';6054}6055if(defined($commitlist[0])) {6056%latest_commit= %{$commitlist[0]};6057my$latest_epoch=$latest_commit{'committer_epoch'};6058%latest_date= parse_date($latest_epoch);6059my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6060if(defined$if_modified) {6061my$since;6062if(eval{require HTTP::Date;1; }) {6063$since= HTTP::Date::str2time($if_modified);6064}elsif(eval{require Time::ParseDate;1; }) {6065$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6066}6067if(defined$since&&$latest_epoch<=$since) {6068print$cgi->header(6069-type =>$content_type,6070-charset =>'utf-8',6071-last_modified =>$latest_date{'rfc2822'},6072-status =>'304 Not Modified');6073return;6074}6075}6076print$cgi->header(6077-type =>$content_type,6078-charset =>'utf-8',6079-last_modified =>$latest_date{'rfc2822'});6080}else{6081print$cgi->header(6082-type =>$content_type,6083-charset =>'utf-8');6084}60856086# Optimization: skip generating the body if client asks only6087# for Last-Modified date.6088return if($cgi->request_method()eq'HEAD');60896090# header variables6091my$title="$site_name-$project/$action";6092my$feed_type='log';6093if(defined$hash) {6094$title.=" - '$hash'";6095$feed_type='branch log';6096if(defined$file_name) {6097$title.=" ::$file_name";6098$feed_type='history';6099}6100}elsif(defined$file_name) {6101$title.=" -$file_name";6102$feed_type='history';6103}6104$title.="$feed_type";6105my$descr= git_get_project_description($project);6106if(defined$descr) {6107$descr= esc_html($descr);6108}else{6109$descr="$project".6110($formateq'rss'?'RSS':'Atom') .6111" feed";6112}6113my$owner= git_get_project_owner($project);6114$owner= esc_html($owner);61156116#header6117my$alt_url;6118if(defined$file_name) {6119$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6120}elsif(defined$hash) {6121$alt_url= href(-full=>1, action=>"log", hash=>$hash);6122}else{6123$alt_url= href(-full=>1, action=>"summary");6124}6125print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6126if($formateq'rss') {6127print<<XML;6128<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6129<channel>6130XML6131print"<title>$title</title>\n".6132"<link>$alt_url</link>\n".6133"<description>$descr</description>\n".6134"<language>en</language>\n".6135# project owner is responsible for 'editorial' content6136"<managingEditor>$owner</managingEditor>\n";6137if(defined$logo||defined$favicon) {6138# prefer the logo to the favicon, since RSS6139# doesn't allow both6140my$img= esc_url($logo||$favicon);6141print"<image>\n".6142"<url>$img</url>\n".6143"<title>$title</title>\n".6144"<link>$alt_url</link>\n".6145"</image>\n";6146}6147if(%latest_date) {6148print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6149print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6150}6151print"<generator>gitweb v.$version/$git_version</generator>\n";6152}elsif($formateq'atom') {6153print<<XML;6154<feed xmlns="http://www.w3.org/2005/Atom">6155XML6156print"<title>$title</title>\n".6157"<subtitle>$descr</subtitle>\n".6158'<link rel="alternate" type="text/html" href="'.6159$alt_url.'" />'."\n".6160'<link rel="self" type="'.$content_type.'" href="'.6161$cgi->self_url() .'" />'."\n".6162"<id>". href(-full=>1) ."</id>\n".6163# use project owner for feed author6164"<author><name>$owner</name></author>\n";6165if(defined$favicon) {6166print"<icon>". esc_url($favicon) ."</icon>\n";6167}6168if(defined$logo_url) {6169# not twice as wide as tall: 72 x 27 pixels6170print"<logo>". esc_url($logo) ."</logo>\n";6171}6172if(!%latest_date) {6173# dummy date to keep the feed valid until commits trickle in:6174print"<updated>1970-01-01T00:00:00Z</updated>\n";6175}else{6176print"<updated>$latest_date{'iso-8601'}</updated>\n";6177}6178print"<generator version='$version/$git_version'>gitweb</generator>\n";6179}61806181# contents6182for(my$i=0;$i<=$#commitlist;$i++) {6183my%co= %{$commitlist[$i]};6184my$commit=$co{'id'};6185# we read 150, we always show 30 and the ones more recent than 48 hours6186if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6187last;6188}6189my%cd= parse_date($co{'author_epoch'});61906191# get list of changed files6192open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6193$co{'parent'} ||"--root",6194$co{'id'},"--", (defined$file_name?$file_name: ())6195ornext;6196my@difftree=map{chomp;$_} <$fd>;6197close$fd6198ornext;61996200# print element (entry, item)6201my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6202if($formateq'rss') {6203print"<item>\n".6204"<title>". esc_html($co{'title'}) ."</title>\n".6205"<author>". esc_html($co{'author'}) ."</author>\n".6206"<pubDate>$cd{'rfc2822'}</pubDate>\n".6207"<guid isPermaLink=\"true\">$co_url</guid>\n".6208"<link>$co_url</link>\n".6209"<description>". esc_html($co{'title'}) ."</description>\n".6210"<content:encoded>".6211"<![CDATA[\n";6212}elsif($formateq'atom') {6213print"<entry>\n".6214"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6215"<updated>$cd{'iso-8601'}</updated>\n".6216"<author>\n".6217" <name>". esc_html($co{'author_name'}) ."</name>\n";6218if($co{'author_email'}) {6219print" <email>". esc_html($co{'author_email'}) ."</email>\n";6220}6221print"</author>\n".6222# use committer for contributor6223"<contributor>\n".6224" <name>". esc_html($co{'committer_name'}) ."</name>\n";6225if($co{'committer_email'}) {6226print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6227}6228print"</contributor>\n".6229"<published>$cd{'iso-8601'}</published>\n".6230"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6231"<id>$co_url</id>\n".6232"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6233"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6234}6235my$comment=$co{'comment'};6236print"<pre>\n";6237foreachmy$line(@$comment) {6238$line= esc_html($line);6239print"$line\n";6240}6241print"</pre><ul>\n";6242foreachmy$difftree_line(@difftree) {6243my%difftree= parse_difftree_raw_line($difftree_line);6244next if!$difftree{'from_id'};62456246my$file=$difftree{'file'} ||$difftree{'to_file'};62476248print"<li>".6249"[".6250$cgi->a({-href => href(-full=>1, action=>"blobdiff",6251 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6252 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6253 file_name=>$file, file_parent=>$difftree{'from_file'}),6254-title =>"diff"},'D');6255if($have_blame) {6256print$cgi->a({-href => href(-full=>1, action=>"blame",6257 file_name=>$file, hash_base=>$commit),6258-title =>"blame"},'B');6259}6260# if this is not a feed of a file history6261if(!defined$file_name||$file_namene$file) {6262print$cgi->a({-href => href(-full=>1, action=>"history",6263 file_name=>$file, hash=>$commit),6264-title =>"history"},'H');6265}6266$file= esc_path($file);6267print"] ".6268"$file</li>\n";6269}6270if($formateq'rss') {6271print"</ul>]]>\n".6272"</content:encoded>\n".6273"</item>\n";6274}elsif($formateq'atom') {6275print"</ul>\n</div>\n".6276"</content>\n".6277"</entry>\n";6278}6279}62806281# end of feed6282if($formateq'rss') {6283print"</channel>\n</rss>\n";6284}elsif($formateq'atom') {6285print"</feed>\n";6286}6287}62886289sub git_rss {6290 git_feed('rss');6291}62926293sub git_atom {6294 git_feed('atom');6295}62966297sub git_opml {6298my@list= git_get_projects_list();62996300print$cgi->header(6301-type =>'text/xml',6302-charset =>'utf-8',6303-content_disposition =>'inline; filename="opml.xml"');63046305print<<XML;6306<?xml version="1.0" encoding="utf-8"?>6307<opml version="1.0">6308<head>6309 <title>$site_nameOPML Export</title>6310</head>6311<body>6312<outline text="git RSS feeds">6313XML63146315foreachmy$pr(@list) {6316my%proj=%$pr;6317my$head= git_get_head_hash($proj{'path'});6318if(!defined$head) {6319next;6320}6321$git_dir="$projectroot/$proj{'path'}";6322my%co= parse_commit($head);6323if(!%co) {6324next;6325}63266327my$path= esc_html(chop_str($proj{'path'},25,5));6328my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6329my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6330print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6331}6332print<<XML;6333</outline>6334</body>6335</opml>6336XML6337}