1#!/usr/bin/perl 2 3# gitweb - simple web interface to track changes in git repositories 4# 5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> 6# (C) 2005, Christian Gierke 7# 8# This program is licensed under the GPLv2 9 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21BEGIN{ 22 CGI->compile()if$ENV{'MOD_PERL'}; 23} 24 25our$cgi= new CGI; 26our$version="++GIT_VERSION++"; 27our$my_url=$cgi->url(); 28our$my_uri=$cgi->url(-absolute =>1); 29 30# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 31# needed and used only for URLs with nonempty PATH_INFO 32our$base_url=$my_url; 33 34# When the script is used as DirectoryIndex, the URL does not contain the name 35# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 36# have to do it ourselves. We make $path_info global because it's also used 37# later on. 38# 39# Another issue with the script being the DirectoryIndex is that the resulting 40# $my_url data is not the full script URL: this is good, because we want 41# generated links to keep implying the script name if it wasn't explicitly 42# indicated in the URL we're handling, but it means that $my_url cannot be used 43# as base URL. 44# Therefore, if we needed to strip PATH_INFO, then we know that we have 45# to build the base URL ourselves: 46our$path_info=$ENV{"PATH_INFO"}; 47if($path_info) { 48if($my_url=~ s,\Q$path_info\E$,, && 49$my_uri=~ s,\Q$path_info\E$,, && 50defined$ENV{'SCRIPT_NAME'}) { 51$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 52} 53} 54 55# core git executable to use 56# this can just be "git" if your webserver has a sensible PATH 57our$GIT="++GIT_BINDIR++/git"; 58 59# absolute fs-path which will be prepended to the project path 60#our $projectroot = "/pub/scm"; 61our$projectroot="++GITWEB_PROJECTROOT++"; 62 63# fs traversing limit for getting project list 64# the number is relative to the projectroot 65our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 66 67# target of the home link on top of all pages 68our$home_link=$my_uri||"/"; 69 70# string of the home link on top of all pages 71our$home_link_str="++GITWEB_HOME_LINK_STR++"; 72 73# name of your site or organization to appear in page titles 74# replace this with something more descriptive for clearer bookmarks 75our$site_name="++GITWEB_SITENAME++" 76|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 77 78# filename of html text to include at top of each page 79our$site_header="++GITWEB_SITE_HEADER++"; 80# html text to include at home page 81our$home_text="++GITWEB_HOMETEXT++"; 82# filename of html text to include at bottom of each page 83our$site_footer="++GITWEB_SITE_FOOTER++"; 84 85# URI of stylesheets 86our@stylesheets= ("++GITWEB_CSS++"); 87# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 88our$stylesheet=undef; 89# URI of GIT logo (72x27 size) 90our$logo="++GITWEB_LOGO++"; 91# URI of GIT favicon, assumed to be image/png type 92our$favicon="++GITWEB_FAVICON++"; 93 94# URI and label (title) of GIT logo link 95#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 96#our $logo_label = "git documentation"; 97our$logo_url="http://git.or.cz/"; 98our$logo_label="git homepage"; 99 100# source of projects list 101our$projects_list="++GITWEB_LIST++"; 102 103# the width (in characters) of the projects list "Description" column 104our$projects_list_description_width=25; 105 106# default order of projects list 107# valid values are none, project, descr, owner, and age 108our$default_projects_order="project"; 109 110# show repository only if this file exists 111# (only effective if this variable evaluates to true) 112our$export_ok="++GITWEB_EXPORT_OK++"; 113 114# show repository only if this subroutine returns true 115# when given the path to the project, for example: 116# sub { return -e "$_[0]/git-daemon-export-ok"; } 117our$export_auth_hook=undef; 118 119# only allow viewing of repositories also shown on the overview page 120our$strict_export="++GITWEB_STRICT_EXPORT++"; 121 122# list of git base URLs used for URL to where fetch project from, 123# i.e. full URL is "$git_base_url/$project" 124our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 125 126# default blob_plain mimetype and default charset for text/plain blob 127our$default_blob_plain_mimetype='text/plain'; 128our$default_text_plain_charset=undef; 129 130# file to use for guessing MIME types before trying /etc/mime.types 131# (relative to the current git repository) 132our$mimetypes_file=undef; 133 134# assume this charset if line contains non-UTF-8 characters; 135# it should be valid encoding (see Encoding::Supported(3pm) for list), 136# for which encoding all byte sequences are valid, for example 137# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 138# could be even 'utf-8' for the old behavior) 139our$fallback_encoding='latin1'; 140 141# rename detection options for git-diff and git-diff-tree 142# - default is '-M', with the cost proportional to 143# (number of removed files) * (number of new files). 144# - more costly is '-C' (which implies '-M'), with the cost proportional to 145# (number of changed files + number of removed files) * (number of new files) 146# - even more costly is '-C', '--find-copies-harder' with cost 147# (number of files in the original tree) * (number of new files) 148# - one might want to include '-B' option, e.g. '-B', '-M' 149our@diff_opts= ('-M');# taken from git_commit 150 151# Disables features that would allow repository owners to inject script into 152# the gitweb domain. 153our$prevent_xss=0; 154 155# information about snapshot formats that gitweb is capable of serving 156our%known_snapshot_formats= ( 157# name => { 158# 'display' => display name, 159# 'type' => mime type, 160# 'suffix' => filename suffix, 161# 'format' => --format for git-archive, 162# 'compressor' => [compressor command and arguments] 163# (array reference, optional)} 164# 165'tgz'=> { 166'display'=>'tar.gz', 167'type'=>'application/x-gzip', 168'suffix'=>'.tar.gz', 169'format'=>'tar', 170'compressor'=> ['gzip']}, 171 172'tbz2'=> { 173'display'=>'tar.bz2', 174'type'=>'application/x-bzip2', 175'suffix'=>'.tar.bz2', 176'format'=>'tar', 177'compressor'=> ['bzip2']}, 178 179'zip'=> { 180'display'=>'zip', 181'type'=>'application/x-zip', 182'suffix'=>'.zip', 183'format'=>'zip'}, 184); 185 186# Aliases so we understand old gitweb.snapshot values in repository 187# configuration. 188our%known_snapshot_format_aliases= ( 189'gzip'=>'tgz', 190'bzip2'=>'tbz2', 191 192# backward compatibility: legacy gitweb config support 193'x-gzip'=>undef,'gz'=>undef, 194'x-bzip2'=>undef,'bz2'=>undef, 195'x-zip'=>undef,''=>undef, 196); 197 198# You define site-wide feature defaults here; override them with 199# $GITWEB_CONFIG as necessary. 200our%feature= ( 201# feature => { 202# 'sub' => feature-sub (subroutine), 203# 'override' => allow-override (boolean), 204# 'default' => [ default options...] (array reference)} 205# 206# if feature is overridable (it means that allow-override has true value), 207# then feature-sub will be called with default options as parameters; 208# return value of feature-sub indicates if to enable specified feature 209# 210# if there is no 'sub' key (no feature-sub), then feature cannot be 211# overriden 212# 213# use gitweb_get_feature(<feature>) to retrieve the <feature> value 214# (an array) or gitweb_check_feature(<feature>) to check if <feature> 215# is enabled 216 217# Enable the 'blame' blob view, showing the last commit that modified 218# each line in the file. This can be very CPU-intensive. 219 220# To enable system wide have in $GITWEB_CONFIG 221# $feature{'blame'}{'default'} = [1]; 222# To have project specific config enable override in $GITWEB_CONFIG 223# $feature{'blame'}{'override'} = 1; 224# and in project config gitweb.blame = 0|1; 225'blame'=> { 226'sub'=>sub{ feature_bool('blame',@_) }, 227'override'=>0, 228'default'=> [0]}, 229 230# Enable the 'snapshot' link, providing a compressed archive of any 231# tree. This can potentially generate high traffic if you have large 232# project. 233 234# Value is a list of formats defined in %known_snapshot_formats that 235# you wish to offer. 236# To disable system wide have in $GITWEB_CONFIG 237# $feature{'snapshot'}{'default'} = []; 238# To have project specific config enable override in $GITWEB_CONFIG 239# $feature{'snapshot'}{'override'} = 1; 240# and in project config, a comma-separated list of formats or "none" 241# to disable. Example: gitweb.snapshot = tbz2,zip; 242'snapshot'=> { 243'sub'=> \&feature_snapshot, 244'override'=>0, 245'default'=> ['tgz']}, 246 247# Enable text search, which will list the commits which match author, 248# committer or commit text to a given string. Enabled by default. 249# Project specific override is not supported. 250'search'=> { 251'override'=>0, 252'default'=> [1]}, 253 254# Enable grep search, which will list the files in currently selected 255# tree containing the given string. Enabled by default. This can be 256# potentially CPU-intensive, of course. 257 258# To enable system wide have in $GITWEB_CONFIG 259# $feature{'grep'}{'default'} = [1]; 260# To have project specific config enable override in $GITWEB_CONFIG 261# $feature{'grep'}{'override'} = 1; 262# and in project config gitweb.grep = 0|1; 263'grep'=> { 264'sub'=>sub{ feature_bool('grep',@_) }, 265'override'=>0, 266'default'=> [1]}, 267 268# Enable the pickaxe search, which will list the commits that modified 269# a given string in a file. This can be practical and quite faster 270# alternative to 'blame', but still potentially CPU-intensive. 271 272# To enable system wide have in $GITWEB_CONFIG 273# $feature{'pickaxe'}{'default'} = [1]; 274# To have project specific config enable override in $GITWEB_CONFIG 275# $feature{'pickaxe'}{'override'} = 1; 276# and in project config gitweb.pickaxe = 0|1; 277'pickaxe'=> { 278'sub'=>sub{ feature_bool('pickaxe',@_) }, 279'override'=>0, 280'default'=> [1]}, 281 282# Make gitweb use an alternative format of the URLs which can be 283# more readable and natural-looking: project name is embedded 284# directly in the path and the query string contains other 285# auxiliary information. All gitweb installations recognize 286# URL in either format; this configures in which formats gitweb 287# generates links. 288 289# To enable system wide have in $GITWEB_CONFIG 290# $feature{'pathinfo'}{'default'} = [1]; 291# Project specific override is not supported. 292 293# Note that you will need to change the default location of CSS, 294# favicon, logo and possibly other files to an absolute URL. Also, 295# if gitweb.cgi serves as your indexfile, you will need to force 296# $my_uri to contain the script name in your $GITWEB_CONFIG. 297'pathinfo'=> { 298'override'=>0, 299'default'=> [0]}, 300 301# Make gitweb consider projects in project root subdirectories 302# to be forks of existing projects. Given project $projname.git, 303# projects matching $projname/*.git will not be shown in the main 304# projects list, instead a '+' mark will be added to $projname 305# there and a 'forks' view will be enabled for the project, listing 306# all the forks. If project list is taken from a file, forks have 307# to be listed after the main project. 308 309# To enable system wide have in $GITWEB_CONFIG 310# $feature{'forks'}{'default'} = [1]; 311# Project specific override is not supported. 312'forks'=> { 313'override'=>0, 314'default'=> [0]}, 315 316# Insert custom links to the action bar of all project pages. 317# This enables you mainly to link to third-party scripts integrating 318# into gitweb; e.g. git-browser for graphical history representation 319# or custom web-based repository administration interface. 320 321# The 'default' value consists of a list of triplets in the form 322# (label, link, position) where position is the label after which 323# to insert the link and link is a format string where %n expands 324# to the project name, %f to the project path within the filesystem, 325# %h to the current hash (h gitweb parameter) and %b to the current 326# hash base (hb gitweb parameter); %% expands to %. 327 328# To enable system wide have in $GITWEB_CONFIG e.g. 329# $feature{'actions'}{'default'} = [('graphiclog', 330# '/git-browser/by-commit.html?r=%n', 'summary')]; 331# Project specific override is not supported. 332'actions'=> { 333'override'=>0, 334'default'=> []}, 335 336# Allow gitweb scan project content tags described in ctags/ 337# of project repository, and display the popular Web 2.0-ish 338# "tag cloud" near the project list. Note that this is something 339# COMPLETELY different from the normal Git tags. 340 341# gitweb by itself can show existing tags, but it does not handle 342# tagging itself; you need an external application for that. 343# For an example script, check Girocco's cgi/tagproj.cgi. 344# You may want to install the HTML::TagCloud Perl module to get 345# a pretty tag cloud instead of just a list of tags. 346 347# To enable system wide have in $GITWEB_CONFIG 348# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 349# Project specific override is not supported. 350'ctags'=> { 351'override'=>0, 352'default'=> [0]}, 353 354# The maximum number of patches in a patchset generated in patch 355# view. Set this to 0 or undef to disable patch view, or to a 356# negative number to remove any limit. 357 358# To disable system wide have in $GITWEB_CONFIG 359# $feature{'patches'}{'default'} = [0]; 360# To have project specific config enable override in $GITWEB_CONFIG 361# $feature{'patches'}{'override'} = 1; 362# and in project config gitweb.patches = 0|n; 363# where n is the maximum number of patches allowed in a patchset. 364'patches'=> { 365'sub'=> \&feature_patches, 366'override'=>0, 367'default'=> [16]}, 368); 369 370sub gitweb_get_feature { 371my($name) =@_; 372return unlessexists$feature{$name}; 373my($sub,$override,@defaults) = ( 374$feature{$name}{'sub'}, 375$feature{$name}{'override'}, 376@{$feature{$name}{'default'}}); 377if(!$override) {return@defaults; } 378if(!defined$sub) { 379warn"feature$nameis not overrideable"; 380return@defaults; 381} 382return$sub->(@defaults); 383} 384 385# A wrapper to check if a given feature is enabled. 386# With this, you can say 387# 388# my $bool_feat = gitweb_check_feature('bool_feat'); 389# gitweb_check_feature('bool_feat') or somecode; 390# 391# instead of 392# 393# my ($bool_feat) = gitweb_get_feature('bool_feat'); 394# (gitweb_get_feature('bool_feat'))[0] or somecode; 395# 396sub gitweb_check_feature { 397return(gitweb_get_feature(@_))[0]; 398} 399 400 401sub feature_bool { 402my$key=shift; 403my($val) = git_get_project_config($key,'--bool'); 404 405if(!defined$val) { 406return($_[0]); 407}elsif($valeq'true') { 408return(1); 409}elsif($valeq'false') { 410return(0); 411} 412} 413 414sub feature_snapshot { 415my(@fmts) =@_; 416 417my($val) = git_get_project_config('snapshot'); 418 419if($val) { 420@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 421} 422 423return@fmts; 424} 425 426sub feature_patches { 427my@val= (git_get_project_config('patches','--int')); 428 429if(@val) { 430return@val; 431} 432 433return($_[0]); 434} 435 436# checking HEAD file with -e is fragile if the repository was 437# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 438# and then pruned. 439sub check_head_link { 440my($dir) =@_; 441my$headfile="$dir/HEAD"; 442return((-e $headfile) || 443(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 444} 445 446sub check_export_ok { 447my($dir) =@_; 448return(check_head_link($dir) && 449(!$export_ok|| -e "$dir/$export_ok") && 450(!$export_auth_hook||$export_auth_hook->($dir))); 451} 452 453# process alternate names for backward compatibility 454# filter out unsupported (unknown) snapshot formats 455sub filter_snapshot_fmts { 456my@fmts=@_; 457 458@fmts=map{ 459exists$known_snapshot_format_aliases{$_} ? 460$known_snapshot_format_aliases{$_} :$_}@fmts; 461@fmts=grep{ 462exists$known_snapshot_formats{$_} }@fmts; 463} 464 465our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 466if(-e $GITWEB_CONFIG) { 467do$GITWEB_CONFIG; 468}else{ 469our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 470do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 471} 472 473# version of the core git binary 474our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 475 476$projects_list||=$projectroot; 477 478# ====================================================================== 479# input validation and dispatch 480 481# input parameters can be collected from a variety of sources (presently, CGI 482# and PATH_INFO), so we define an %input_params hash that collects them all 483# together during validation: this allows subsequent uses (e.g. href()) to be 484# agnostic of the parameter origin 485 486our%input_params= (); 487 488# input parameters are stored with the long parameter name as key. This will 489# also be used in the href subroutine to convert parameters to their CGI 490# equivalent, and since the href() usage is the most frequent one, we store 491# the name -> CGI key mapping here, instead of the reverse. 492# 493# XXX: Warning: If you touch this, check the search form for updating, 494# too. 495 496our@cgi_param_mapping= ( 497 project =>"p", 498 action =>"a", 499 file_name =>"f", 500 file_parent =>"fp", 501 hash =>"h", 502 hash_parent =>"hp", 503 hash_base =>"hb", 504 hash_parent_base =>"hpb", 505 page =>"pg", 506 order =>"o", 507 searchtext =>"s", 508 searchtype =>"st", 509 snapshot_format =>"sf", 510 extra_options =>"opt", 511 search_use_regexp =>"sr", 512); 513our%cgi_param_mapping=@cgi_param_mapping; 514 515# we will also need to know the possible actions, for validation 516our%actions= ( 517"blame"=> \&git_blame, 518"blobdiff"=> \&git_blobdiff, 519"blobdiff_plain"=> \&git_blobdiff_plain, 520"blob"=> \&git_blob, 521"blob_plain"=> \&git_blob_plain, 522"commitdiff"=> \&git_commitdiff, 523"commitdiff_plain"=> \&git_commitdiff_plain, 524"commit"=> \&git_commit, 525"forks"=> \&git_forks, 526"heads"=> \&git_heads, 527"history"=> \&git_history, 528"log"=> \&git_log, 529"patch"=> \&git_patch, 530"patches"=> \&git_patches, 531"rss"=> \&git_rss, 532"atom"=> \&git_atom, 533"search"=> \&git_search, 534"search_help"=> \&git_search_help, 535"shortlog"=> \&git_shortlog, 536"summary"=> \&git_summary, 537"tag"=> \&git_tag, 538"tags"=> \&git_tags, 539"tree"=> \&git_tree, 540"snapshot"=> \&git_snapshot, 541"object"=> \&git_object, 542# those below don't need $project 543"opml"=> \&git_opml, 544"project_list"=> \&git_project_list, 545"project_index"=> \&git_project_index, 546); 547 548# finally, we have the hash of allowed extra_options for the commands that 549# allow them 550our%allowed_options= ( 551"--no-merges"=> [qw(rss atom log shortlog history)], 552); 553 554# fill %input_params with the CGI parameters. All values except for 'opt' 555# should be single values, but opt can be an array. We should probably 556# build an array of parameters that can be multi-valued, but since for the time 557# being it's only this one, we just single it out 558while(my($name,$symbol) =each%cgi_param_mapping) { 559if($symboleq'opt') { 560$input_params{$name} = [$cgi->param($symbol) ]; 561}else{ 562$input_params{$name} =$cgi->param($symbol); 563} 564} 565 566# now read PATH_INFO and update the parameter list for missing parameters 567sub evaluate_path_info { 568return ifdefined$input_params{'project'}; 569return if!$path_info; 570$path_info=~ s,^/+,,; 571return if!$path_info; 572 573# find which part of PATH_INFO is project 574my$project=$path_info; 575$project=~ s,/+$,,; 576while($project&& !check_head_link("$projectroot/$project")) { 577$project=~ s,/*[^/]*$,,; 578} 579return unless$project; 580$input_params{'project'} =$project; 581 582# do not change any parameters if an action is given using the query string 583return if$input_params{'action'}; 584$path_info=~ s,^\Q$project\E/*,,; 585 586# next, check if we have an action 587my$action=$path_info; 588$action=~ s,/.*$,,; 589if(exists$actions{$action}) { 590$path_info=~ s,^$action/*,,; 591$input_params{'action'} =$action; 592} 593 594# list of actions that want hash_base instead of hash, but can have no 595# pathname (f) parameter 596my@wants_base= ( 597'tree', 598'history', 599); 600 601# we want to catch 602# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 603my($parentrefname,$parentpathname,$refname,$pathname) = 604($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 605 606# first, analyze the 'current' part 607if(defined$pathname) { 608# we got "branch:filename" or "branch:dir/" 609# we could use git_get_type(branch:pathname), but: 610# - it needs $git_dir 611# - it does a git() call 612# - the convention of terminating directories with a slash 613# makes it superfluous 614# - embedding the action in the PATH_INFO would make it even 615# more superfluous 616$pathname=~ s,^/+,,; 617if(!$pathname||substr($pathname, -1)eq"/") { 618$input_params{'action'} ||="tree"; 619$pathname=~ s,/$,,; 620}else{ 621# the default action depends on whether we had parent info 622# or not 623if($parentrefname) { 624$input_params{'action'} ||="blobdiff_plain"; 625}else{ 626$input_params{'action'} ||="blob_plain"; 627} 628} 629$input_params{'hash_base'} ||=$refname; 630$input_params{'file_name'} ||=$pathname; 631}elsif(defined$refname) { 632# we got "branch". In this case we have to choose if we have to 633# set hash or hash_base. 634# 635# Most of the actions without a pathname only want hash to be 636# set, except for the ones specified in @wants_base that want 637# hash_base instead. It should also be noted that hand-crafted 638# links having 'history' as an action and no pathname or hash 639# set will fail, but that happens regardless of PATH_INFO. 640$input_params{'action'} ||="shortlog"; 641if(grep{$_eq$input_params{'action'} }@wants_base) { 642$input_params{'hash_base'} ||=$refname; 643}else{ 644$input_params{'hash'} ||=$refname; 645} 646} 647 648# next, handle the 'parent' part, if present 649if(defined$parentrefname) { 650# a missing pathspec defaults to the 'current' filename, allowing e.g. 651# someproject/blobdiff/oldrev..newrev:/filename 652if($parentpathname) { 653$parentpathname=~ s,^/+,,; 654$parentpathname=~ s,/$,,; 655$input_params{'file_parent'} ||=$parentpathname; 656}else{ 657$input_params{'file_parent'} ||=$input_params{'file_name'}; 658} 659# we assume that hash_parent_base is wanted if a path was specified, 660# or if the action wants hash_base instead of hash 661if(defined$input_params{'file_parent'} || 662grep{$_eq$input_params{'action'} }@wants_base) { 663$input_params{'hash_parent_base'} ||=$parentrefname; 664}else{ 665$input_params{'hash_parent'} ||=$parentrefname; 666} 667} 668 669# for the snapshot action, we allow URLs in the form 670# $project/snapshot/$hash.ext 671# where .ext determines the snapshot and gets removed from the 672# passed $refname to provide the $hash. 673# 674# To be able to tell that $refname includes the format extension, we 675# require the following two conditions to be satisfied: 676# - the hash input parameter MUST have been set from the $refname part 677# of the URL (i.e. they must be equal) 678# - the snapshot format MUST NOT have been defined already (e.g. from 679# CGI parameter sf) 680# It's also useless to try any matching unless $refname has a dot, 681# so we check for that too 682if(defined$input_params{'action'} && 683$input_params{'action'}eq'snapshot'&& 684defined$refname&&index($refname,'.') != -1&& 685$refnameeq$input_params{'hash'} && 686!defined$input_params{'snapshot_format'}) { 687# We loop over the known snapshot formats, checking for 688# extensions. Allowed extensions are both the defined suffix 689# (which includes the initial dot already) and the snapshot 690# format key itself, with a prepended dot 691while(my($fmt,$opt) =each%known_snapshot_formats) { 692my$hash=$refname; 693unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 694next; 695} 696my$sfx=$1; 697# a valid suffix was found, so set the snapshot format 698# and reset the hash parameter 699$input_params{'snapshot_format'} =$fmt; 700$input_params{'hash'} =$hash; 701# we also set the format suffix to the one requested 702# in the URL: this way a request for e.g. .tgz returns 703# a .tgz instead of a .tar.gz 704$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 705last; 706} 707} 708} 709evaluate_path_info(); 710 711our$action=$input_params{'action'}; 712if(defined$action) { 713if(!validate_action($action)) { 714 die_error(400,"Invalid action parameter"); 715} 716} 717 718# parameters which are pathnames 719our$project=$input_params{'project'}; 720if(defined$project) { 721if(!validate_project($project)) { 722undef$project; 723 die_error(404,"No such project"); 724} 725} 726 727our$file_name=$input_params{'file_name'}; 728if(defined$file_name) { 729if(!validate_pathname($file_name)) { 730 die_error(400,"Invalid file parameter"); 731} 732} 733 734our$file_parent=$input_params{'file_parent'}; 735if(defined$file_parent) { 736if(!validate_pathname($file_parent)) { 737 die_error(400,"Invalid file parent parameter"); 738} 739} 740 741# parameters which are refnames 742our$hash=$input_params{'hash'}; 743if(defined$hash) { 744if(!validate_refname($hash)) { 745 die_error(400,"Invalid hash parameter"); 746} 747} 748 749our$hash_parent=$input_params{'hash_parent'}; 750if(defined$hash_parent) { 751if(!validate_refname($hash_parent)) { 752 die_error(400,"Invalid hash parent parameter"); 753} 754} 755 756our$hash_base=$input_params{'hash_base'}; 757if(defined$hash_base) { 758if(!validate_refname($hash_base)) { 759 die_error(400,"Invalid hash base parameter"); 760} 761} 762 763our@extra_options= @{$input_params{'extra_options'}}; 764# @extra_options is always defined, since it can only be (currently) set from 765# CGI, and $cgi->param() returns the empty array in array context if the param 766# is not set 767foreachmy$opt(@extra_options) { 768if(not exists$allowed_options{$opt}) { 769 die_error(400,"Invalid option parameter"); 770} 771if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 772 die_error(400,"Invalid option parameter for this action"); 773} 774} 775 776our$hash_parent_base=$input_params{'hash_parent_base'}; 777if(defined$hash_parent_base) { 778if(!validate_refname($hash_parent_base)) { 779 die_error(400,"Invalid hash parent base parameter"); 780} 781} 782 783# other parameters 784our$page=$input_params{'page'}; 785if(defined$page) { 786if($page=~m/[^0-9]/) { 787 die_error(400,"Invalid page parameter"); 788} 789} 790 791our$searchtype=$input_params{'searchtype'}; 792if(defined$searchtype) { 793if($searchtype=~m/[^a-z]/) { 794 die_error(400,"Invalid searchtype parameter"); 795} 796} 797 798our$search_use_regexp=$input_params{'search_use_regexp'}; 799 800our$searchtext=$input_params{'searchtext'}; 801our$search_regexp; 802if(defined$searchtext) { 803if(length($searchtext) <2) { 804 die_error(403,"At least two characters are required for search parameter"); 805} 806$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 807} 808 809# path to the current git repository 810our$git_dir; 811$git_dir="$projectroot/$project"if$project; 812 813# list of supported snapshot formats 814our@snapshot_fmts= gitweb_get_feature('snapshot'); 815@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 816 817# dispatch 818if(!defined$action) { 819if(defined$hash) { 820$action= git_get_type($hash); 821}elsif(defined$hash_base&&defined$file_name) { 822$action= git_get_type("$hash_base:$file_name"); 823}elsif(defined$project) { 824$action='summary'; 825}else{ 826$action='project_list'; 827} 828} 829if(!defined($actions{$action})) { 830 die_error(400,"Unknown action"); 831} 832if($action!~m/^(?:opml|project_list|project_index)$/&& 833!$project) { 834 die_error(400,"Project needed"); 835} 836$actions{$action}->(); 837exit; 838 839## ====================================================================== 840## action links 841 842sub href { 843my%params=@_; 844# default is to use -absolute url() i.e. $my_uri 845my$href=$params{-full} ?$my_url:$my_uri; 846 847$params{'project'} =$projectunlessexists$params{'project'}; 848 849if($params{-replay}) { 850while(my($name,$symbol) =each%cgi_param_mapping) { 851if(!exists$params{$name}) { 852$params{$name} =$input_params{$name}; 853} 854} 855} 856 857my$use_pathinfo= gitweb_check_feature('pathinfo'); 858if($use_pathinfoand defined$params{'project'}) { 859# try to put as many parameters as possible in PATH_INFO: 860# - project name 861# - action 862# - hash_parent or hash_parent_base:/file_parent 863# - hash or hash_base:/filename 864# - the snapshot_format as an appropriate suffix 865 866# When the script is the root DirectoryIndex for the domain, 867# $href here would be something like http://gitweb.example.com/ 868# Thus, we strip any trailing / from $href, to spare us double 869# slashes in the final URL 870$href=~ s,/$,,; 871 872# Then add the project name, if present 873$href.="/".esc_url($params{'project'}); 874delete$params{'project'}; 875 876# since we destructively absorb parameters, we keep this 877# boolean that remembers if we're handling a snapshot 878my$is_snapshot=$params{'action'}eq'snapshot'; 879 880# Summary just uses the project path URL, any other action is 881# added to the URL 882if(defined$params{'action'}) { 883$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 884delete$params{'action'}; 885} 886 887# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 888# stripping nonexistent or useless pieces 889$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 890||$params{'hash_parent'} ||$params{'hash'}); 891if(defined$params{'hash_base'}) { 892if(defined$params{'hash_parent_base'}) { 893$href.= esc_url($params{'hash_parent_base'}); 894# skip the file_parent if it's the same as the file_name 895delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 896if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 897$href.=":/".esc_url($params{'file_parent'}); 898delete$params{'file_parent'}; 899} 900$href.=".."; 901delete$params{'hash_parent'}; 902delete$params{'hash_parent_base'}; 903}elsif(defined$params{'hash_parent'}) { 904$href.= esc_url($params{'hash_parent'}).".."; 905delete$params{'hash_parent'}; 906} 907 908$href.= esc_url($params{'hash_base'}); 909if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 910$href.=":/".esc_url($params{'file_name'}); 911delete$params{'file_name'}; 912} 913delete$params{'hash'}; 914delete$params{'hash_base'}; 915}elsif(defined$params{'hash'}) { 916$href.= esc_url($params{'hash'}); 917delete$params{'hash'}; 918} 919 920# If the action was a snapshot, we can absorb the 921# snapshot_format parameter too 922if($is_snapshot) { 923my$fmt=$params{'snapshot_format'}; 924# snapshot_format should always be defined when href() 925# is called, but just in case some code forgets, we 926# fall back to the default 927$fmt||=$snapshot_fmts[0]; 928$href.=$known_snapshot_formats{$fmt}{'suffix'}; 929delete$params{'snapshot_format'}; 930} 931} 932 933# now encode the parameters explicitly 934my@result= (); 935for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 936my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 937if(defined$params{$name}) { 938if(ref($params{$name})eq"ARRAY") { 939foreachmy$par(@{$params{$name}}) { 940push@result,$symbol."=". esc_param($par); 941} 942}else{ 943push@result,$symbol."=". esc_param($params{$name}); 944} 945} 946} 947$href.="?".join(';',@result)ifscalar@result; 948 949return$href; 950} 951 952 953## ====================================================================== 954## validation, quoting/unquoting and escaping 955 956sub validate_action { 957my$input=shift||returnundef; 958returnundefunlessexists$actions{$input}; 959return$input; 960} 961 962sub validate_project { 963my$input=shift||returnundef; 964if(!validate_pathname($input) || 965!(-d "$projectroot/$input") || 966!check_export_ok("$projectroot/$input") || 967($strict_export&& !project_in_list($input))) { 968returnundef; 969}else{ 970return$input; 971} 972} 973 974sub validate_pathname { 975my$input=shift||returnundef; 976 977# no '.' or '..' as elements of path, i.e. no '.' nor '..' 978# at the beginning, at the end, and between slashes. 979# also this catches doubled slashes 980if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 981returnundef; 982} 983# no null characters 984if($input=~m!\0!) { 985returnundef; 986} 987return$input; 988} 989 990sub validate_refname { 991my$input=shift||returnundef; 992 993# textual hashes are O.K. 994if($input=~m/^[0-9a-fA-F]{40}$/) { 995return$input; 996} 997# it must be correct pathname 998$input= validate_pathname($input) 999orreturnundef;1000# restrictions on ref name according to git-check-ref-format1001if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1002returnundef;1003}1004return$input;1005}10061007# decode sequences of octets in utf8 into Perl's internal form,1008# which is utf-8 with utf8 flag set if needed. gitweb writes out1009# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1010sub to_utf8 {1011my$str=shift;1012if(utf8::valid($str)) {1013 utf8::decode($str);1014return$str;1015}else{1016return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1017}1018}10191020# quote unsafe chars, but keep the slash, even when it's not1021# correct, but quoted slashes look too horrible in bookmarks1022sub esc_param {1023my$str=shift;1024$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1025$str=~s/\+/%2B/g;1026$str=~s/ /\+/g;1027return$str;1028}10291030# quote unsafe chars in whole URL, so some charactrs cannot be quoted1031sub esc_url {1032my$str=shift;1033$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1034$str=~s/\+/%2B/g;1035$str=~s/ /\+/g;1036return$str;1037}10381039# replace invalid utf8 character with SUBSTITUTION sequence1040sub esc_html {1041my$str=shift;1042my%opts=@_;10431044$str= to_utf8($str);1045$str=$cgi->escapeHTML($str);1046if($opts{'-nbsp'}) {1047$str=~s/ / /g;1048}1049$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1050return$str;1051}10521053# quote control characters and escape filename to HTML1054sub esc_path {1055my$str=shift;1056my%opts=@_;10571058$str= to_utf8($str);1059$str=$cgi->escapeHTML($str);1060if($opts{'-nbsp'}) {1061$str=~s/ / /g;1062}1063$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1064return$str;1065}10661067# Make control characters "printable", using character escape codes (CEC)1068sub quot_cec {1069my$cntrl=shift;1070my%opts=@_;1071my%es= (# character escape codes, aka escape sequences1072"\t"=>'\t',# tab (HT)1073"\n"=>'\n',# line feed (LF)1074"\r"=>'\r',# carrige return (CR)1075"\f"=>'\f',# form feed (FF)1076"\b"=>'\b',# backspace (BS)1077"\a"=>'\a',# alarm (bell) (BEL)1078"\e"=>'\e',# escape (ESC)1079"\013"=>'\v',# vertical tab (VT)1080"\000"=>'\0',# nul character (NUL)1081);1082my$chr= ( (exists$es{$cntrl})1083?$es{$cntrl}1084:sprintf('\%2x',ord($cntrl)) );1085if($opts{-nohtml}) {1086return$chr;1087}else{1088return"<span class=\"cntrl\">$chr</span>";1089}1090}10911092# Alternatively use unicode control pictures codepoints,1093# Unicode "printable representation" (PR)1094sub quot_upr {1095my$cntrl=shift;1096my%opts=@_;10971098my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1099if($opts{-nohtml}) {1100return$chr;1101}else{1102return"<span class=\"cntrl\">$chr</span>";1103}1104}11051106# git may return quoted and escaped filenames1107sub unquote {1108my$str=shift;11091110sub unq {1111my$seq=shift;1112my%es= (# character escape codes, aka escape sequences1113't'=>"\t",# tab (HT, TAB)1114'n'=>"\n",# newline (NL)1115'r'=>"\r",# return (CR)1116'f'=>"\f",# form feed (FF)1117'b'=>"\b",# backspace (BS)1118'a'=>"\a",# alarm (bell) (BEL)1119'e'=>"\e",# escape (ESC)1120'v'=>"\013",# vertical tab (VT)1121);11221123if($seq=~m/^[0-7]{1,3}$/) {1124# octal char sequence1125returnchr(oct($seq));1126}elsif(exists$es{$seq}) {1127# C escape sequence, aka character escape code1128return$es{$seq};1129}1130# quoted ordinary character1131return$seq;1132}11331134if($str=~m/^"(.*)"$/) {1135# needs unquoting1136$str=$1;1137$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1138}1139return$str;1140}11411142# escape tabs (convert tabs to spaces)1143sub untabify {1144my$line=shift;11451146while((my$pos=index($line,"\t")) != -1) {1147if(my$count= (8- ($pos%8))) {1148my$spaces=' ' x $count;1149$line=~s/\t/$spaces/;1150}1151}11521153return$line;1154}11551156sub project_in_list {1157my$project=shift;1158my@list= git_get_projects_list();1159return@list&&scalar(grep{$_->{'path'}eq$project}@list);1160}11611162## ----------------------------------------------------------------------1163## HTML aware string manipulation11641165# Try to chop given string on a word boundary between position1166# $len and $len+$add_len. If there is no word boundary there,1167# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1168# (marking chopped part) would be longer than given string.1169sub chop_str {1170my$str=shift;1171my$len=shift;1172my$add_len=shift||10;1173my$where=shift||'right';# 'left' | 'center' | 'right'11741175# Make sure perl knows it is utf8 encoded so we don't1176# cut in the middle of a utf8 multibyte char.1177$str= to_utf8($str);11781179# allow only $len chars, but don't cut a word if it would fit in $add_len1180# if it doesn't fit, cut it if it's still longer than the dots we would add1181# remove chopped character entities entirely11821183# when chopping in the middle, distribute $len into left and right part1184# return early if chopping wouldn't make string shorter1185if($whereeq'center') {1186return$strif($len+5>=length($str));# filler is length 51187$len=int($len/2);1188}else{1189return$strif($len+4>=length($str));# filler is length 41190}11911192# regexps: ending and beginning with word part up to $add_len1193my$endre=qr/.{$len}\w{0,$add_len}/;1194my$begre=qr/\w{0,$add_len}.{$len}/;11951196if($whereeq'left') {1197$str=~m/^(.*?)($begre)$/;1198my($lead,$body) = ($1,$2);1199if(length($lead) >4) {1200$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1201$lead=" ...";1202}1203return"$lead$body";12041205}elsif($whereeq'center') {1206$str=~m/^($endre)(.*)$/;1207my($left,$str) = ($1,$2);1208$str=~m/^(.*?)($begre)$/;1209my($mid,$right) = ($1,$2);1210if(length($mid) >5) {1211$left=~s/&[^;]*$//;1212$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1213$mid=" ... ";1214}1215return"$left$mid$right";12161217}else{1218$str=~m/^($endre)(.*)$/;1219my$body=$1;1220my$tail=$2;1221if(length($tail) >4) {1222$body=~s/&[^;]*$//;1223$tail="... ";1224}1225return"$body$tail";1226}1227}12281229# takes the same arguments as chop_str, but also wraps a <span> around the1230# result with a title attribute if it does get chopped. Additionally, the1231# string is HTML-escaped.1232sub chop_and_escape_str {1233my($str) =@_;12341235my$chopped= chop_str(@_);1236if($choppedeq$str) {1237return esc_html($chopped);1238}else{1239$str=~s/[[:cntrl:]]/?/g;1240return$cgi->span({-title=>$str}, esc_html($chopped));1241}1242}12431244## ----------------------------------------------------------------------1245## functions returning short strings12461247# CSS class for given age value (in seconds)1248sub age_class {1249my$age=shift;12501251if(!defined$age) {1252return"noage";1253}elsif($age<60*60*2) {1254return"age0";1255}elsif($age<60*60*24*2) {1256return"age1";1257}else{1258return"age2";1259}1260}12611262# convert age in seconds to "nn units ago" string1263sub age_string {1264my$age=shift;1265my$age_str;12661267if($age>60*60*24*365*2) {1268$age_str= (int$age/60/60/24/365);1269$age_str.=" years ago";1270}elsif($age>60*60*24*(365/12)*2) {1271$age_str=int$age/60/60/24/(365/12);1272$age_str.=" months ago";1273}elsif($age>60*60*24*7*2) {1274$age_str=int$age/60/60/24/7;1275$age_str.=" weeks ago";1276}elsif($age>60*60*24*2) {1277$age_str=int$age/60/60/24;1278$age_str.=" days ago";1279}elsif($age>60*60*2) {1280$age_str=int$age/60/60;1281$age_str.=" hours ago";1282}elsif($age>60*2) {1283$age_str=int$age/60;1284$age_str.=" min ago";1285}elsif($age>2) {1286$age_str=int$age;1287$age_str.=" sec ago";1288}else{1289$age_str.=" right now";1290}1291return$age_str;1292}12931294useconstant{1295 S_IFINVALID =>0030000,1296 S_IFGITLINK =>0160000,1297};12981299# submodule/subproject, a commit object reference1300sub S_ISGITLINK {1301my$mode=shift;13021303return(($mode& S_IFMT) == S_IFGITLINK)1304}13051306# convert file mode in octal to symbolic file mode string1307sub mode_str {1308my$mode=oct shift;13091310if(S_ISGITLINK($mode)) {1311return'm---------';1312}elsif(S_ISDIR($mode& S_IFMT)) {1313return'drwxr-xr-x';1314}elsif(S_ISLNK($mode)) {1315return'lrwxrwxrwx';1316}elsif(S_ISREG($mode)) {1317# git cares only about the executable bit1318if($mode& S_IXUSR) {1319return'-rwxr-xr-x';1320}else{1321return'-rw-r--r--';1322};1323}else{1324return'----------';1325}1326}13271328# convert file mode in octal to file type string1329sub file_type {1330my$mode=shift;13311332if($mode!~m/^[0-7]+$/) {1333return$mode;1334}else{1335$mode=oct$mode;1336}13371338if(S_ISGITLINK($mode)) {1339return"submodule";1340}elsif(S_ISDIR($mode& S_IFMT)) {1341return"directory";1342}elsif(S_ISLNK($mode)) {1343return"symlink";1344}elsif(S_ISREG($mode)) {1345return"file";1346}else{1347return"unknown";1348}1349}13501351# convert file mode in octal to file type description string1352sub file_type_long {1353my$mode=shift;13541355if($mode!~m/^[0-7]+$/) {1356return$mode;1357}else{1358$mode=oct$mode;1359}13601361if(S_ISGITLINK($mode)) {1362return"submodule";1363}elsif(S_ISDIR($mode& S_IFMT)) {1364return"directory";1365}elsif(S_ISLNK($mode)) {1366return"symlink";1367}elsif(S_ISREG($mode)) {1368if($mode& S_IXUSR) {1369return"executable";1370}else{1371return"file";1372};1373}else{1374return"unknown";1375}1376}137713781379## ----------------------------------------------------------------------1380## functions returning short HTML fragments, or transforming HTML fragments1381## which don't belong to other sections13821383# format line of commit message.1384sub format_log_line_html {1385my$line=shift;13861387$line= esc_html($line, -nbsp=>1);1388$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1389$cgi->a({-href => href(action=>"object", hash=>$1),1390-class=>"text"},$1);1391}eg;13921393return$line;1394}13951396# format marker of refs pointing to given object13971398# the destination action is chosen based on object type and current context:1399# - for annotated tags, we choose the tag view unless it's the current view1400# already, in which case we go to shortlog view1401# - for other refs, we keep the current view if we're in history, shortlog or1402# log view, and select shortlog otherwise1403sub format_ref_marker {1404my($refs,$id) =@_;1405my$markers='';14061407if(defined$refs->{$id}) {1408foreachmy$ref(@{$refs->{$id}}) {1409# this code exploits the fact that non-lightweight tags are the1410# only indirect objects, and that they are the only objects for which1411# we want to use tag instead of shortlog as action1412my($type,$name) =qw();1413my$indirect= ($ref=~s/\^\{\}$//);1414# e.g. tags/v2.6.11 or heads/next1415if($ref=~m!^(.*?)s?/(.*)$!) {1416$type=$1;1417$name=$2;1418}else{1419$type="ref";1420$name=$ref;1421}14221423my$class=$type;1424$class.=" indirect"if$indirect;14251426my$dest_action="shortlog";14271428if($indirect) {1429$dest_action="tag"unless$actioneq"tag";1430}elsif($action=~/^(history|(short)?log)$/) {1431$dest_action=$action;1432}14331434my$dest="";1435$dest.="refs/"unless$ref=~ m!^refs/!;1436$dest.=$ref;14371438my$link=$cgi->a({1439-href => href(1440 action=>$dest_action,1441 hash=>$dest1442)},$name);14431444$markers.=" <span class=\"$class\"title=\"$ref\">".1445$link."</span>";1446}1447}14481449if($markers) {1450return' <span class="refs">'.$markers.'</span>';1451}else{1452return"";1453}1454}14551456# format, perhaps shortened and with markers, title line1457sub format_subject_html {1458my($long,$short,$href,$extra) =@_;1459$extra=''unlessdefined($extra);14601461if(length($short) <length($long)) {1462$long=~s/[[:cntrl:]]/?/g;1463return$cgi->a({-href =>$href, -class=>"list subject",1464-title => to_utf8($long)},1465 esc_html($short) .$extra);1466}else{1467return$cgi->a({-href =>$href, -class=>"list subject"},1468 esc_html($long) .$extra);1469}1470}14711472# format git diff header line, i.e. "diff --(git|combined|cc) ..."1473sub format_git_diff_header_line {1474my$line=shift;1475my$diffinfo=shift;1476my($from,$to) =@_;14771478if($diffinfo->{'nparents'}) {1479# combined diff1480$line=~s!^(diff (.*?) )"?.*$!$1!;1481if($to->{'href'}) {1482$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1483 esc_path($to->{'file'}));1484}else{# file was deleted (no href)1485$line.= esc_path($to->{'file'});1486}1487}else{1488# "ordinary" diff1489$line=~s!^(diff (.*?) )"?a/.*$!$1!;1490if($from->{'href'}) {1491$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1492'a/'. esc_path($from->{'file'}));1493}else{# file was added (no href)1494$line.='a/'. esc_path($from->{'file'});1495}1496$line.=' ';1497if($to->{'href'}) {1498$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1499'b/'. esc_path($to->{'file'}));1500}else{# file was deleted1501$line.='b/'. esc_path($to->{'file'});1502}1503}15041505return"<div class=\"diff header\">$line</div>\n";1506}15071508# format extended diff header line, before patch itself1509sub format_extended_diff_header_line {1510my$line=shift;1511my$diffinfo=shift;1512my($from,$to) =@_;15131514# match <path>1515if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1516$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1517 esc_path($from->{'file'}));1518}1519if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1520$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1521 esc_path($to->{'file'}));1522}1523# match single <mode>1524if($line=~m/\s(\d{6})$/) {1525$line.='<span class="info"> ('.1526 file_type_long($1) .1527')</span>';1528}1529# match <hash>1530if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1531# can match only for combined diff1532$line='index ';1533for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1534if($from->{'href'}[$i]) {1535$line.=$cgi->a({-href=>$from->{'href'}[$i],1536-class=>"hash"},1537substr($diffinfo->{'from_id'}[$i],0,7));1538}else{1539$line.='0' x 7;1540}1541# separator1542$line.=','if($i<$diffinfo->{'nparents'} -1);1543}1544$line.='..';1545if($to->{'href'}) {1546$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1547substr($diffinfo->{'to_id'},0,7));1548}else{1549$line.='0' x 7;1550}15511552}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1553# can match only for ordinary diff1554my($from_link,$to_link);1555if($from->{'href'}) {1556$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1557substr($diffinfo->{'from_id'},0,7));1558}else{1559$from_link='0' x 7;1560}1561if($to->{'href'}) {1562$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1563substr($diffinfo->{'to_id'},0,7));1564}else{1565$to_link='0' x 7;1566}1567my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1568$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1569}15701571return$line."<br/>\n";1572}15731574# format from-file/to-file diff header1575sub format_diff_from_to_header {1576my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1577my$line;1578my$result='';15791580$line=$from_line;1581#assert($line =~ m/^---/) if DEBUG;1582# no extra formatting for "^--- /dev/null"1583if(!$diffinfo->{'nparents'}) {1584# ordinary (single parent) diff1585if($line=~m!^--- "?a/!) {1586if($from->{'href'}) {1587$line='--- a/'.1588$cgi->a({-href=>$from->{'href'}, -class=>"path"},1589 esc_path($from->{'file'}));1590}else{1591$line='--- a/'.1592 esc_path($from->{'file'});1593}1594}1595$result.= qq!<div class="diff from_file">$line</div>\n!;15961597}else{1598# combined diff (merge commit)1599for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1600if($from->{'href'}[$i]) {1601$line='--- '.1602$cgi->a({-href=>href(action=>"blobdiff",1603 hash_parent=>$diffinfo->{'from_id'}[$i],1604 hash_parent_base=>$parents[$i],1605 file_parent=>$from->{'file'}[$i],1606 hash=>$diffinfo->{'to_id'},1607 hash_base=>$hash,1608 file_name=>$to->{'file'}),1609-class=>"path",1610-title=>"diff". ($i+1)},1611$i+1) .1612'/'.1613$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1614 esc_path($from->{'file'}[$i]));1615}else{1616$line='--- /dev/null';1617}1618$result.= qq!<div class="diff from_file">$line</div>\n!;1619}1620}16211622$line=$to_line;1623#assert($line =~ m/^\+\+\+/) if DEBUG;1624# no extra formatting for "^+++ /dev/null"1625if($line=~m!^\+\+\+ "?b/!) {1626if($to->{'href'}) {1627$line='+++ b/'.1628$cgi->a({-href=>$to->{'href'}, -class=>"path"},1629 esc_path($to->{'file'}));1630}else{1631$line='+++ b/'.1632 esc_path($to->{'file'});1633}1634}1635$result.= qq!<div class="diff to_file">$line</div>\n!;16361637return$result;1638}16391640# create note for patch simplified by combined diff1641sub format_diff_cc_simplified {1642my($diffinfo,@parents) =@_;1643my$result='';16441645$result.="<div class=\"diff header\">".1646"diff --cc ";1647if(!is_deleted($diffinfo)) {1648$result.=$cgi->a({-href => href(action=>"blob",1649 hash_base=>$hash,1650 hash=>$diffinfo->{'to_id'},1651 file_name=>$diffinfo->{'to_file'}),1652-class=>"path"},1653 esc_path($diffinfo->{'to_file'}));1654}else{1655$result.= esc_path($diffinfo->{'to_file'});1656}1657$result.="</div>\n".# class="diff header"1658"<div class=\"diff nodifferences\">".1659"Simple merge".1660"</div>\n";# class="diff nodifferences"16611662return$result;1663}16641665# format patch (diff) line (not to be used for diff headers)1666sub format_diff_line {1667my$line=shift;1668my($from,$to) =@_;1669my$diff_class="";16701671chomp$line;16721673if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1674# combined diff1675my$prefix=substr($line,0,scalar@{$from->{'href'}});1676if($line=~m/^\@{3}/) {1677$diff_class=" chunk_header";1678}elsif($line=~m/^\\/) {1679$diff_class=" incomplete";1680}elsif($prefix=~tr/+/+/) {1681$diff_class=" add";1682}elsif($prefix=~tr/-/-/) {1683$diff_class=" rem";1684}1685}else{1686# assume ordinary diff1687my$char=substr($line,0,1);1688if($chareq'+') {1689$diff_class=" add";1690}elsif($chareq'-') {1691$diff_class=" rem";1692}elsif($chareq'@') {1693$diff_class=" chunk_header";1694}elsif($chareq"\\") {1695$diff_class=" incomplete";1696}1697}1698$line= untabify($line);1699if($from&&$to&&$line=~m/^\@{2} /) {1700my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1701$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;17021703$from_lines=0unlessdefined$from_lines;1704$to_lines=0unlessdefined$to_lines;17051706if($from->{'href'}) {1707$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1708-class=>"list"},$from_text);1709}1710if($to->{'href'}) {1711$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1712-class=>"list"},$to_text);1713}1714$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1715"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1716return"<div class=\"diff$diff_class\">$line</div>\n";1717}elsif($from&&$to&&$line=~m/^\@{3}/) {1718my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1719my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);17201721@from_text=split(' ',$ranges);1722for(my$i=0;$i<@from_text; ++$i) {1723($from_start[$i],$from_nlines[$i]) =1724(split(',',substr($from_text[$i],1)),0);1725}17261727$to_text=pop@from_text;1728$to_start=pop@from_start;1729$to_nlines=pop@from_nlines;17301731$line="<span class=\"chunk_info\">$prefix";1732for(my$i=0;$i<@from_text; ++$i) {1733if($from->{'href'}[$i]) {1734$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1735-class=>"list"},$from_text[$i]);1736}else{1737$line.=$from_text[$i];1738}1739$line.=" ";1740}1741if($to->{'href'}) {1742$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1743-class=>"list"},$to_text);1744}else{1745$line.=$to_text;1746}1747$line.="$prefix</span>".1748"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1749return"<div class=\"diff$diff_class\">$line</div>\n";1750}1751return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1752}17531754# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1755# linked. Pass the hash of the tree/commit to snapshot.1756sub format_snapshot_links {1757my($hash) =@_;1758my$num_fmts=@snapshot_fmts;1759if($num_fmts>1) {1760# A parenthesized list of links bearing format names.1761# e.g. "snapshot (_tar.gz_ _zip_)"1762return"snapshot (".join(' ',map1763$cgi->a({1764-href => href(1765 action=>"snapshot",1766 hash=>$hash,1767 snapshot_format=>$_1768)1769},$known_snapshot_formats{$_}{'display'})1770,@snapshot_fmts) .")";1771}elsif($num_fmts==1) {1772# A single "snapshot" link whose tooltip bears the format name.1773# i.e. "_snapshot_"1774my($fmt) =@snapshot_fmts;1775return1776$cgi->a({1777-href => href(1778 action=>"snapshot",1779 hash=>$hash,1780 snapshot_format=>$fmt1781),1782-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1783},"snapshot");1784}else{# $num_fmts == 01785returnundef;1786}1787}17881789## ......................................................................1790## functions returning values to be passed, perhaps after some1791## transformation, to other functions; e.g. returning arguments to href()17921793# returns hash to be passed to href to generate gitweb URL1794# in -title key it returns description of link1795sub get_feed_info {1796my$format=shift||'Atom';1797my%res= (action =>lc($format));17981799# feed links are possible only for project views1800return unless(defined$project);1801# some views should link to OPML, or to generic project feed,1802# or don't have specific feed yet (so they should use generic)1803return if($action=~/^(?:tags|heads|forks|tag|search)$/x);18041805my$branch;1806# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1807# from tag links; this also makes possible to detect branch links1808if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1809(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1810$branch=$1;1811}1812# find log type for feed description (title)1813my$type='log';1814if(defined$file_name) {1815$type="history of$file_name";1816$type.="/"if($actioneq'tree');1817$type.=" on '$branch'"if(defined$branch);1818}else{1819$type="log of$branch"if(defined$branch);1820}18211822$res{-title} =$type;1823$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1824$res{'file_name'} =$file_name;18251826return%res;1827}18281829## ----------------------------------------------------------------------1830## git utility subroutines, invoking git commands18311832# returns path to the core git executable and the --git-dir parameter as list1833sub git_cmd {1834return$GIT,'--git-dir='.$git_dir;1835}18361837# quote the given arguments for passing them to the shell1838# quote_command("command", "arg 1", "arg with ' and ! characters")1839# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1840# Try to avoid using this function wherever possible.1841sub quote_command {1842returnjoin(' ',1843map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);1844}18451846# get HEAD ref of given project as hash1847sub git_get_head_hash {1848my$project=shift;1849my$o_git_dir=$git_dir;1850my$retval=undef;1851$git_dir="$projectroot/$project";1852if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1853my$head= <$fd>;1854close$fd;1855if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1856$retval=$1;1857}1858}1859if(defined$o_git_dir) {1860$git_dir=$o_git_dir;1861}1862return$retval;1863}18641865# get type of given object1866sub git_get_type {1867my$hash=shift;18681869open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1870my$type= <$fd>;1871close$fdorreturn;1872chomp$type;1873return$type;1874}18751876# repository configuration1877our$config_file='';1878our%config;18791880# store multiple values for single key as anonymous array reference1881# single values stored directly in the hash, not as [ <value> ]1882sub hash_set_multi {1883my($hash,$key,$value) =@_;18841885if(!exists$hash->{$key}) {1886$hash->{$key} =$value;1887}elsif(!ref$hash->{$key}) {1888$hash->{$key} = [$hash->{$key},$value];1889}else{1890push@{$hash->{$key}},$value;1891}1892}18931894# return hash of git project configuration1895# optionally limited to some section, e.g. 'gitweb'1896sub git_parse_project_config {1897my$section_regexp=shift;1898my%config;18991900local$/="\0";19011902open my$fh,"-|", git_cmd(),"config",'-z','-l',1903orreturn;19041905while(my$keyval= <$fh>) {1906chomp$keyval;1907my($key,$value) =split(/\n/,$keyval,2);19081909 hash_set_multi(\%config,$key,$value)1910if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1911}1912close$fh;19131914return%config;1915}19161917# convert config value to boolean: 'true' or 'false'1918# no value, number > 0, 'true' and 'yes' values are true1919# rest of values are treated as false (never as error)1920sub config_to_bool {1921my$val=shift;19221923return1if!defined$val;# section.key19241925# strip leading and trailing whitespace1926$val=~s/^\s+//;1927$val=~s/\s+$//;19281929return(($val=~/^\d+$/&&$val) ||# section.key = 11930($val=~/^(?:true|yes)$/i));# section.key = true1931}19321933# convert config value to simple decimal number1934# an optional value suffix of 'k', 'm', or 'g' will cause the value1935# to be multiplied by 1024, 1048576, or 10737418241936sub config_to_int {1937my$val=shift;19381939# strip leading and trailing whitespace1940$val=~s/^\s+//;1941$val=~s/\s+$//;19421943if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1944$unit=lc($unit);1945# unknown unit is treated as 11946return$num* ($uniteq'g'?1073741824:1947$uniteq'm'?1048576:1948$uniteq'k'?1024:1);1949}1950return$val;1951}19521953# convert config value to array reference, if needed1954sub config_to_multi {1955my$val=shift;19561957returnref($val) ?$val: (defined($val) ? [$val] : []);1958}19591960sub git_get_project_config {1961my($key,$type) =@_;19621963# key sanity check1964return unless($key);1965$key=~s/^gitweb\.//;1966return if($key=~m/\W/);19671968# type sanity check1969if(defined$type) {1970$type=~s/^--//;1971$type=undef1972unless($typeeq'bool'||$typeeq'int');1973}19741975# get config1976if(!defined$config_file||1977$config_filene"$git_dir/config") {1978%config= git_parse_project_config('gitweb');1979$config_file="$git_dir/config";1980}19811982# check if config variable (key) exists1983return unlessexists$config{"gitweb.$key"};19841985# ensure given type1986if(!defined$type) {1987return$config{"gitweb.$key"};1988}elsif($typeeq'bool') {1989# backward compatibility: 'git config --bool' returns true/false1990return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1991}elsif($typeeq'int') {1992return config_to_int($config{"gitweb.$key"});1993}1994return$config{"gitweb.$key"};1995}19961997# get hash of given path at given ref1998sub git_get_hash_by_path {1999my$base=shift;2000my$path=shift||returnundef;2001my$type=shift;20022003$path=~ s,/+$,,;20042005open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2006or die_error(500,"Open git-ls-tree failed");2007my$line= <$fd>;2008close$fdorreturnundef;20092010if(!defined$line) {2011# there is no tree or hash given by $path at $base2012returnundef;2013}20142015#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2016$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2017if(defined$type&&$typene$2) {2018# type doesn't match2019returnundef;2020}2021return$3;2022}20232024# get path of entry with given hash at given tree-ish (ref)2025# used to get 'from' filename for combined diff (merge commit) for renames2026sub git_get_path_by_hash {2027my$base=shift||return;2028my$hash=shift||return;20292030local$/="\0";20312032open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2033orreturnundef;2034while(my$line= <$fd>) {2035chomp$line;20362037#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2038#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2039if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2040close$fd;2041return$1;2042}2043}2044close$fd;2045returnundef;2046}20472048## ......................................................................2049## git utility functions, directly accessing git repository20502051sub git_get_project_description {2052my$path=shift;20532054$git_dir="$projectroot/$path";2055open my$fd,'<',"$git_dir/description"2056orreturn git_get_project_config('description');2057my$descr= <$fd>;2058close$fd;2059if(defined$descr) {2060chomp$descr;2061}2062return$descr;2063}20642065sub git_get_project_ctags {2066my$path=shift;2067my$ctags= {};20682069$git_dir="$projectroot/$path";2070opendir my$dh,"$git_dir/ctags"2071orreturn$ctags;2072foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2073open my$ct,'<',$_ornext;2074my$val= <$ct>;2075chomp$val;2076close$ct;2077my$ctag=$_;$ctag=~ s#.*/##;2078$ctags->{$ctag} =$val;2079}2080closedir$dh;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(my$mh,'<',$mimemap)orreturnundef;2809while(<$mh>) {2810next ifm/^#/;# skip comments2811my($mimetype,$exts) =split(/\t+/);2812if(defined$exts) {2813my@exts=split(/\s+/,$exts);2814foreachmy$ext(@exts) {2815$mimemap{$ext} =$mimetype;2816}2817}2818}2819close($mh);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$/=undef;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) =@_;33443345# absolute symlinks (beginning with '/') cannot be normalized3346return if(substr($link_target,0,1)eq'/');33473348# normalize link target to path from top (root) tree (dir)3349my$path;3350if($basedir) {3351$path=$basedir.'/'.$link_target;3352}else{3353# we are in top (root) tree (dir)3354$path=$link_target;3355}33563357# remove //, /./, and /../3358my@path_parts;3359foreachmy$part(split('/',$path)) {3360# discard '.' and ''3361next if(!$part||$parteq'.');3362# handle '..'3363if($parteq'..') {3364if(@path_parts) {3365pop@path_parts;3366}else{3367# link leads outside repository (outside top dir)3368return;3369}3370}else{3371push@path_parts,$part;3372}3373}3374$path=join('/',@path_parts);33753376return$path;3377}33783379# print tree entry (row of git_tree), but without encompassing <tr> element3380sub git_print_tree_entry {3381my($t,$basedir,$hash_base,$have_blame) =@_;33823383my%base_key= ();3384$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33853386# The format of a table row is: mode list link. Where mode is3387# the mode of the entry, list is the name of the entry, an href,3388# and link is the action links of the entry.33893390print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3391if($t->{'type'}eq"blob") {3392print"<td class=\"list\">".3393$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3394 file_name=>"$basedir$t->{'name'}",%base_key),3395-class=>"list"}, esc_path($t->{'name'}));3396if(S_ISLNK(oct$t->{'mode'})) {3397my$link_target= git_get_link_target($t->{'hash'});3398if($link_target) {3399my$norm_target= normalize_link_target($link_target,$basedir);3400if(defined$norm_target) {3401print" -> ".3402$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3403 file_name=>$norm_target),3404-title =>$norm_target}, esc_path($link_target));3405}else{3406print" -> ". esc_path($link_target);3407}3408}3409}3410print"</td>\n";3411print"<td class=\"link\">";3412print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3413 file_name=>"$basedir$t->{'name'}",%base_key)},3414"blob");3415if($have_blame) {3416print" | ".3417$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3418 file_name=>"$basedir$t->{'name'}",%base_key)},3419"blame");3420}3421if(defined$hash_base) {3422print" | ".3423$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3424 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3425"history");3426}3427print" | ".3428$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3429 file_name=>"$basedir$t->{'name'}")},3430"raw");3431print"</td>\n";34323433}elsif($t->{'type'}eq"tree") {3434print"<td class=\"list\">";3435print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3436 file_name=>"$basedir$t->{'name'}",%base_key)},3437 esc_path($t->{'name'}));3438print"</td>\n";3439print"<td class=\"link\">";3440print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3441 file_name=>"$basedir$t->{'name'}",%base_key)},3442"tree");3443if(defined$hash_base) {3444print" | ".3445$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3446 file_name=>"$basedir$t->{'name'}")},3447"history");3448}3449print"</td>\n";3450}else{3451# unknown object: we can only present history for it3452# (this includes 'commit' object, i.e. submodule support)3453print"<td class=\"list\">".3454 esc_path($t->{'name'}) .3455"</td>\n";3456print"<td class=\"link\">";3457if(defined$hash_base) {3458print$cgi->a({-href => href(action=>"history",3459 hash_base=>$hash_base,3460 file_name=>"$basedir$t->{'name'}")},3461"history");3462}3463print"</td>\n";3464}3465}34663467## ......................................................................3468## functions printing large fragments of HTML34693470# get pre-image filenames for merge (combined) diff3471sub fill_from_file_info {3472my($diff,@parents) =@_;34733474$diff->{'from_file'} = [ ];3475$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3476for(my$i=0;$i<$diff->{'nparents'};$i++) {3477if($diff->{'status'}[$i]eq'R'||3478$diff->{'status'}[$i]eq'C') {3479$diff->{'from_file'}[$i] =3480 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3481}3482}34833484return$diff;3485}34863487# is current raw difftree line of file deletion3488sub is_deleted {3489my$diffinfo=shift;34903491return$diffinfo->{'to_id'}eq('0' x 40);3492}34933494# does patch correspond to [previous] difftree raw line3495# $diffinfo - hashref of parsed raw diff format3496# $patchinfo - hashref of parsed patch diff format3497# (the same keys as in $diffinfo)3498sub is_patch_split {3499my($diffinfo,$patchinfo) =@_;35003501returndefined$diffinfo&&defined$patchinfo3502&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3503}350435053506sub git_difftree_body {3507my($difftree,$hash,@parents) =@_;3508my($parent) =$parents[0];3509my$have_blame= gitweb_check_feature('blame');3510print"<div class=\"list_head\">\n";3511if($#{$difftree} >10) {3512print(($#{$difftree} +1) ." files changed:\n");3513}3514print"</div>\n";35153516print"<table class=\"".3517(@parents>1?"combined ":"") .3518"diff_tree\">\n";35193520# header only for combined diff in 'commitdiff' view3521my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3522if($has_header) {3523# table header3524print"<thead><tr>\n".3525"<th></th><th></th>\n";# filename, patchN link3526for(my$i=0;$i<@parents;$i++) {3527my$par=$parents[$i];3528print"<th>".3529$cgi->a({-href => href(action=>"commitdiff",3530 hash=>$hash, hash_parent=>$par),3531-title =>'commitdiff to parent number '.3532($i+1) .': '.substr($par,0,7)},3533$i+1) .3534" </th>\n";3535}3536print"</tr></thead>\n<tbody>\n";3537}35383539my$alternate=1;3540my$patchno=0;3541foreachmy$line(@{$difftree}) {3542my$diff= parsed_difftree_line($line);35433544if($alternate) {3545print"<tr class=\"dark\">\n";3546}else{3547print"<tr class=\"light\">\n";3548}3549$alternate^=1;35503551if(exists$diff->{'nparents'}) {# combined diff35523553 fill_from_file_info($diff,@parents)3554unlessexists$diff->{'from_file'};35553556if(!is_deleted($diff)) {3557# file exists in the result (child) commit3558print"<td>".3559$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3560 file_name=>$diff->{'to_file'},3561 hash_base=>$hash),3562-class=>"list"}, esc_path($diff->{'to_file'})) .3563"</td>\n";3564}else{3565print"<td>".3566 esc_path($diff->{'to_file'}) .3567"</td>\n";3568}35693570if($actioneq'commitdiff') {3571# link to patch3572$patchno++;3573print"<td class=\"link\">".3574$cgi->a({-href =>"#patch$patchno"},"patch") .3575" | ".3576"</td>\n";3577}35783579my$has_history=0;3580my$not_deleted=0;3581for(my$i=0;$i<$diff->{'nparents'};$i++) {3582my$hash_parent=$parents[$i];3583my$from_hash=$diff->{'from_id'}[$i];3584my$from_path=$diff->{'from_file'}[$i];3585my$status=$diff->{'status'}[$i];35863587$has_history||= ($statusne'A');3588$not_deleted||= ($statusne'D');35893590if($statuseq'A') {3591print"<td class=\"link\"align=\"right\"> | </td>\n";3592}elsif($statuseq'D') {3593print"<td class=\"link\">".3594$cgi->a({-href => href(action=>"blob",3595 hash_base=>$hash,3596 hash=>$from_hash,3597 file_name=>$from_path)},3598"blob". ($i+1)) .3599" | </td>\n";3600}else{3601if($diff->{'to_id'}eq$from_hash) {3602print"<td class=\"link nochange\">";3603}else{3604print"<td class=\"link\">";3605}3606print$cgi->a({-href => href(action=>"blobdiff",3607 hash=>$diff->{'to_id'},3608 hash_parent=>$from_hash,3609 hash_base=>$hash,3610 hash_parent_base=>$hash_parent,3611 file_name=>$diff->{'to_file'},3612 file_parent=>$from_path)},3613"diff". ($i+1)) .3614" | </td>\n";3615}3616}36173618print"<td class=\"link\">";3619if($not_deleted) {3620print$cgi->a({-href => href(action=>"blob",3621 hash=>$diff->{'to_id'},3622 file_name=>$diff->{'to_file'},3623 hash_base=>$hash)},3624"blob");3625print" | "if($has_history);3626}3627if($has_history) {3628print$cgi->a({-href => href(action=>"history",3629 file_name=>$diff->{'to_file'},3630 hash_base=>$hash)},3631"history");3632}3633print"</td>\n";36343635print"</tr>\n";3636next;# instead of 'else' clause, to avoid extra indent3637}3638# else ordinary diff36393640my($to_mode_oct,$to_mode_str,$to_file_type);3641my($from_mode_oct,$from_mode_str,$from_file_type);3642if($diff->{'to_mode'}ne('0' x 6)) {3643$to_mode_oct=oct$diff->{'to_mode'};3644if(S_ISREG($to_mode_oct)) {# only for regular file3645$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3646}3647$to_file_type= file_type($diff->{'to_mode'});3648}3649if($diff->{'from_mode'}ne('0' x 6)) {3650$from_mode_oct=oct$diff->{'from_mode'};3651if(S_ISREG($to_mode_oct)) {# only for regular file3652$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3653}3654$from_file_type= file_type($diff->{'from_mode'});3655}36563657if($diff->{'status'}eq"A") {# created3658my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3659$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3660$mode_chng.="]</span>";3661print"<td>";3662print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3663 hash_base=>$hash, file_name=>$diff->{'file'}),3664-class=>"list"}, esc_path($diff->{'file'}));3665print"</td>\n";3666print"<td>$mode_chng</td>\n";3667print"<td class=\"link\">";3668if($actioneq'commitdiff') {3669# link to patch3670$patchno++;3671print$cgi->a({-href =>"#patch$patchno"},"patch");3672print" | ";3673}3674print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3675 hash_base=>$hash, file_name=>$diff->{'file'})},3676"blob");3677print"</td>\n";36783679}elsif($diff->{'status'}eq"D") {# deleted3680my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3681print"<td>";3682print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3683 hash_base=>$parent, file_name=>$diff->{'file'}),3684-class=>"list"}, esc_path($diff->{'file'}));3685print"</td>\n";3686print"<td>$mode_chng</td>\n";3687print"<td class=\"link\">";3688if($actioneq'commitdiff') {3689# link to patch3690$patchno++;3691print$cgi->a({-href =>"#patch$patchno"},"patch");3692print" | ";3693}3694print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3695 hash_base=>$parent, file_name=>$diff->{'file'})},3696"blob") ." | ";3697if($have_blame) {3698print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3699 file_name=>$diff->{'file'})},3700"blame") ." | ";3701}3702print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3703 file_name=>$diff->{'file'})},3704"history");3705print"</td>\n";37063707}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3708my$mode_chnge="";3709if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3710$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3711if($from_file_typene$to_file_type) {3712$mode_chnge.=" from$from_file_typeto$to_file_type";3713}3714if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3715if($from_mode_str&&$to_mode_str) {3716$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3717}elsif($to_mode_str) {3718$mode_chnge.=" mode:$to_mode_str";3719}3720}3721$mode_chnge.="]</span>\n";3722}3723print"<td>";3724print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3725 hash_base=>$hash, file_name=>$diff->{'file'}),3726-class=>"list"}, esc_path($diff->{'file'}));3727print"</td>\n";3728print"<td>$mode_chnge</td>\n";3729print"<td class=\"link\">";3730if($actioneq'commitdiff') {3731# link to patch3732$patchno++;3733print$cgi->a({-href =>"#patch$patchno"},"patch") .3734" | ";3735}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3736# "commit" view and modified file (not onlu mode changed)3737print$cgi->a({-href => href(action=>"blobdiff",3738 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3739 hash_base=>$hash, hash_parent_base=>$parent,3740 file_name=>$diff->{'file'})},3741"diff") .3742" | ";3743}3744print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3745 hash_base=>$hash, file_name=>$diff->{'file'})},3746"blob") ." | ";3747if($have_blame) {3748print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3749 file_name=>$diff->{'file'})},3750"blame") ." | ";3751}3752print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3753 file_name=>$diff->{'file'})},3754"history");3755print"</td>\n";37563757}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3758my%status_name= ('R'=>'moved','C'=>'copied');3759my$nstatus=$status_name{$diff->{'status'}};3760my$mode_chng="";3761if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3762# mode also for directories, so we cannot use $to_mode_str3763$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3764}3765print"<td>".3766$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3767 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3768-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3769"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3770$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3771 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3772-class=>"list"}, esc_path($diff->{'from_file'})) .3773" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3774"<td class=\"link\">";3775if($actioneq'commitdiff') {3776# link to patch3777$patchno++;3778print$cgi->a({-href =>"#patch$patchno"},"patch") .3779" | ";3780}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3781# "commit" view and modified file (not only pure rename or copy)3782print$cgi->a({-href => href(action=>"blobdiff",3783 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3784 hash_base=>$hash, hash_parent_base=>$parent,3785 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3786"diff") .3787" | ";3788}3789print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3790 hash_base=>$parent, file_name=>$diff->{'to_file'})},3791"blob") ." | ";3792if($have_blame) {3793print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3794 file_name=>$diff->{'to_file'})},3795"blame") ." | ";3796}3797print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3798 file_name=>$diff->{'to_file'})},3799"history");3800print"</td>\n";38013802}# we should not encounter Unmerged (U) or Unknown (X) status3803print"</tr>\n";3804}3805print"</tbody>"if$has_header;3806print"</table>\n";3807}38083809sub git_patchset_body {3810my($fd,$difftree,$hash,@hash_parents) =@_;3811my($hash_parent) =$hash_parents[0];38123813my$is_combined= (@hash_parents>1);3814my$patch_idx=0;3815my$patch_number=0;3816my$patch_line;3817my$diffinfo;3818my$to_name;3819my(%from,%to);38203821print"<div class=\"patchset\">\n";38223823# skip to first patch3824while($patch_line= <$fd>) {3825chomp$patch_line;38263827last if($patch_line=~m/^diff /);3828}38293830 PATCH:3831while($patch_line) {38323833# parse "git diff" header line3834if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3835# $1 is from_name, which we do not use3836$to_name= unquote($2);3837$to_name=~s!^b/!!;3838}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3839# $1 is 'cc' or 'combined', which we do not use3840$to_name= unquote($2);3841}else{3842$to_name=undef;3843}38443845# check if current patch belong to current raw line3846# and parse raw git-diff line if needed3847if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3848# this is continuation of a split patch3849print"<div class=\"patch cont\">\n";3850}else{3851# advance raw git-diff output if needed3852$patch_idx++ifdefined$diffinfo;38533854# read and prepare patch information3855$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38563857# compact combined diff output can have some patches skipped3858# find which patch (using pathname of result) we are at now;3859if($is_combined) {3860while($to_namene$diffinfo->{'to_file'}) {3861print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3862 format_diff_cc_simplified($diffinfo,@hash_parents) .3863"</div>\n";# class="patch"38643865$patch_idx++;3866$patch_number++;38673868last if$patch_idx>$#$difftree;3869$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3870}3871}38723873# modifies %from, %to hashes3874 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);38753876# this is first patch for raw difftree line with $patch_idx index3877# we index @$difftree array from 0, but number patches from 13878print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3879}38803881# git diff header3882#assert($patch_line =~ m/^diff /) if DEBUG;3883#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3884$patch_number++;3885# print "git diff" header3886print format_git_diff_header_line($patch_line,$diffinfo,3887 \%from, \%to);38883889# print extended diff header3890print"<div class=\"diff extended_header\">\n";3891 EXTENDED_HEADER:3892while($patch_line= <$fd>) {3893chomp$patch_line;38943895last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);38963897print format_extended_diff_header_line($patch_line,$diffinfo,3898 \%from, \%to);3899}3900print"</div>\n";# class="diff extended_header"39013902# from-file/to-file diff header3903if(!$patch_line) {3904print"</div>\n";# class="patch"3905last PATCH;3906}3907next PATCH if($patch_line=~m/^diff /);3908#assert($patch_line =~ m/^---/) if DEBUG;39093910my$last_patch_line=$patch_line;3911$patch_line= <$fd>;3912chomp$patch_line;3913#assert($patch_line =~ m/^\+\+\+/) if DEBUG;39143915print format_diff_from_to_header($last_patch_line,$patch_line,3916$diffinfo, \%from, \%to,3917@hash_parents);39183919# the patch itself3920 LINE:3921while($patch_line= <$fd>) {3922chomp$patch_line;39233924next PATCH if($patch_line=~m/^diff /);39253926print format_diff_line($patch_line, \%from, \%to);3927}39283929}continue{3930print"</div>\n";# class="patch"3931}39323933# for compact combined (--cc) format, with chunk and patch simpliciaction3934# patchset might be empty, but there might be unprocessed raw lines3935for(++$patch_idxif$patch_number>0;3936$patch_idx<@$difftree;3937++$patch_idx) {3938# read and prepare patch information3939$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39403941# generate anchor for "patch" links in difftree / whatchanged part3942print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3943 format_diff_cc_simplified($diffinfo,@hash_parents) .3944"</div>\n";# class="patch"39453946$patch_number++;3947}39483949if($patch_number==0) {3950if(@hash_parents>1) {3951print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3952}else{3953print"<div class=\"diff nodifferences\">No differences found</div>\n";3954}3955}39563957print"</div>\n";# class="patchset"3958}39593960# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39613962# fills project list info (age, description, owner, forks) for each3963# project in the list, removing invalid projects from returned list3964# NOTE: modifies $projlist, but does not remove entries from it3965sub fill_project_list_info {3966my($projlist,$check_forks) =@_;3967my@projects;39683969my$show_ctags= gitweb_check_feature('ctags');3970 PROJECT:3971foreachmy$pr(@$projlist) {3972my(@activity) = git_get_last_activity($pr->{'path'});3973unless(@activity) {3974next PROJECT;3975}3976($pr->{'age'},$pr->{'age_string'}) =@activity;3977if(!defined$pr->{'descr'}) {3978my$descr= git_get_project_description($pr->{'path'}) ||"";3979$descr= to_utf8($descr);3980$pr->{'descr_long'} =$descr;3981$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3982}3983if(!defined$pr->{'owner'}) {3984$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3985}3986if($check_forks) {3987my$pname=$pr->{'path'};3988if(($pname=~s/\.git$//) &&3989($pname!~/\/$/) &&3990(-d "$projectroot/$pname")) {3991$pr->{'forks'} ="-d$projectroot/$pname";3992}else{3993$pr->{'forks'} =0;3994}3995}3996$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3997push@projects,$pr;3998}39994000return@projects;4001}40024003# print 'sort by' <th> element, generating 'sort by $name' replay link4004# if that order is not selected4005sub print_sort_th {4006my($name,$order,$header) =@_;4007$header||=ucfirst($name);40084009if($ordereq$name) {4010print"<th>$header</th>\n";4011}else{4012print"<th>".4013$cgi->a({-href => href(-replay=>1, order=>$name),4014-class=>"header"},$header) .4015"</th>\n";4016}4017}40184019sub git_project_list_body {4020# actually uses global variable $project4021my($projlist,$order,$from,$to,$extra,$no_header) =@_;40224023my$check_forks= gitweb_check_feature('forks');4024my@projects= fill_project_list_info($projlist,$check_forks);40254026$order||=$default_projects_order;4027$from=0unlessdefined$from;4028$to=$#projectsif(!defined$to||$#projects<$to);40294030my%order_info= (4031 project => { key =>'path', type =>'str'},4032 descr => { key =>'descr_long', type =>'str'},4033 owner => { key =>'owner', type =>'str'},4034 age => { key =>'age', type =>'num'}4035);4036my$oi=$order_info{$order};4037if($oi->{'type'}eq'str') {4038@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4039}else{4040@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4041}40424043my$show_ctags= gitweb_check_feature('ctags');4044if($show_ctags) {4045my%ctags;4046foreachmy$p(@projects) {4047foreachmy$ct(keys%{$p->{'ctags'}}) {4048$ctags{$ct} +=$p->{'ctags'}->{$ct};4049}4050}4051my$cloud= git_populate_project_tagcloud(\%ctags);4052print git_show_project_tagcloud($cloud,64);4053}40544055print"<table class=\"project_list\">\n";4056unless($no_header) {4057print"<tr>\n";4058if($check_forks) {4059print"<th></th>\n";4060}4061 print_sort_th('project',$order,'Project');4062 print_sort_th('descr',$order,'Description');4063 print_sort_th('owner',$order,'Owner');4064 print_sort_th('age',$order,'Last Change');4065print"<th></th>\n".# for links4066"</tr>\n";4067}4068my$alternate=1;4069my$tagfilter=$cgi->param('by_tag');4070for(my$i=$from;$i<=$to;$i++) {4071my$pr=$projects[$i];40724073next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4074next if$searchtextand not$pr->{'path'} =~/$searchtext/4075and not$pr->{'descr_long'} =~/$searchtext/;4076# Weed out forks or non-matching entries of search4077if($check_forks) {4078my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4079$forkbase="^$forkbase"if$forkbase;4080next ifnot$searchtextand not$tagfilterand$show_ctags4081and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4082}40834084if($alternate) {4085print"<tr class=\"dark\">\n";4086}else{4087print"<tr class=\"light\">\n";4088}4089$alternate^=1;4090if($check_forks) {4091print"<td>";4092if($pr->{'forks'}) {4093print"<!--$pr->{'forks'} -->\n";4094print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4095}4096print"</td>\n";4097}4098print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4099-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4100"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4101-class=>"list", -title =>$pr->{'descr_long'}},4102 esc_html($pr->{'descr'})) ."</td>\n".4103"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4104print"<td class=\"". age_class($pr->{'age'}) ."\">".4105(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4106"<td class=\"link\">".4107$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4108$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4109$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4110$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4111($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4112"</td>\n".4113"</tr>\n";4114}4115if(defined$extra) {4116print"<tr>\n";4117if($check_forks) {4118print"<td></td>\n";4119}4120print"<td colspan=\"5\">$extra</td>\n".4121"</tr>\n";4122}4123print"</table>\n";4124}41254126sub git_shortlog_body {4127# uses global variable $project4128my($commitlist,$from,$to,$refs,$extra) =@_;41294130$from=0unlessdefined$from;4131$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41324133print"<table class=\"shortlog\">\n";4134my$alternate=1;4135for(my$i=$from;$i<=$to;$i++) {4136my%co= %{$commitlist->[$i]};4137my$commit=$co{'id'};4138my$ref= format_ref_marker($refs,$commit);4139if($alternate) {4140print"<tr class=\"dark\">\n";4141}else{4142print"<tr class=\"light\">\n";4143}4144$alternate^=1;4145my$author= chop_and_escape_str($co{'author_name'},10);4146# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4147print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4148"<td><i>".$author."</i></td>\n".4149"<td>";4150print format_subject_html($co{'title'},$co{'title_short'},4151 href(action=>"commit", hash=>$commit),$ref);4152print"</td>\n".4153"<td class=\"link\">".4154$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4155$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4156$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4157my$snapshot_links= format_snapshot_links($commit);4158if(defined$snapshot_links) {4159print" | ".$snapshot_links;4160}4161print"</td>\n".4162"</tr>\n";4163}4164if(defined$extra) {4165print"<tr>\n".4166"<td colspan=\"4\">$extra</td>\n".4167"</tr>\n";4168}4169print"</table>\n";4170}41714172sub git_history_body {4173# Warning: assumes constant type (blob or tree) during history4174my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;41754176$from=0unlessdefined$from;4177$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});41784179print"<table class=\"history\">\n";4180my$alternate=1;4181for(my$i=$from;$i<=$to;$i++) {4182my%co= %{$commitlist->[$i]};4183if(!%co) {4184next;4185}4186my$commit=$co{'id'};41874188my$ref= format_ref_marker($refs,$commit);41894190if($alternate) {4191print"<tr class=\"dark\">\n";4192}else{4193print"<tr class=\"light\">\n";4194}4195$alternate^=1;4196# shortlog uses chop_str($co{'author_name'}, 10)4197my$author= chop_and_escape_str($co{'author_name'},15,3);4198print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4199"<td><i>".$author."</i></td>\n".4200"<td>";4201# originally git_history used chop_str($co{'title'}, 50)4202print format_subject_html($co{'title'},$co{'title_short'},4203 href(action=>"commit", hash=>$commit),$ref);4204print"</td>\n".4205"<td class=\"link\">".4206$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4207$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");42084209if($ftypeeq'blob') {4210my$blob_current= git_get_hash_by_path($hash_base,$file_name);4211my$blob_parent= git_get_hash_by_path($commit,$file_name);4212if(defined$blob_current&&defined$blob_parent&&4213$blob_currentne$blob_parent) {4214print" | ".4215$cgi->a({-href => href(action=>"blobdiff",4216 hash=>$blob_current, hash_parent=>$blob_parent,4217 hash_base=>$hash_base, hash_parent_base=>$commit,4218 file_name=>$file_name)},4219"diff to current");4220}4221}4222print"</td>\n".4223"</tr>\n";4224}4225if(defined$extra) {4226print"<tr>\n".4227"<td colspan=\"4\">$extra</td>\n".4228"</tr>\n";4229}4230print"</table>\n";4231}42324233sub git_tags_body {4234# uses global variable $project4235my($taglist,$from,$to,$extra) =@_;4236$from=0unlessdefined$from;4237$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);42384239print"<table class=\"tags\">\n";4240my$alternate=1;4241for(my$i=$from;$i<=$to;$i++) {4242my$entry=$taglist->[$i];4243my%tag=%$entry;4244my$comment=$tag{'subject'};4245my$comment_short;4246if(defined$comment) {4247$comment_short= chop_str($comment,30,5);4248}4249if($alternate) {4250print"<tr class=\"dark\">\n";4251}else{4252print"<tr class=\"light\">\n";4253}4254$alternate^=1;4255if(defined$tag{'age'}) {4256print"<td><i>$tag{'age'}</i></td>\n";4257}else{4258print"<td></td>\n";4259}4260print"<td>".4261$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4262-class=>"list name"}, esc_html($tag{'name'})) .4263"</td>\n".4264"<td>";4265if(defined$comment) {4266print format_subject_html($comment,$comment_short,4267 href(action=>"tag", hash=>$tag{'id'}));4268}4269print"</td>\n".4270"<td class=\"selflink\">";4271if($tag{'type'}eq"tag") {4272print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4273}else{4274print" ";4275}4276print"</td>\n".4277"<td class=\"link\">"." | ".4278$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4279if($tag{'reftype'}eq"commit") {4280print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4281" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4282}elsif($tag{'reftype'}eq"blob") {4283print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4284}4285print"</td>\n".4286"</tr>";4287}4288if(defined$extra) {4289print"<tr>\n".4290"<td colspan=\"5\">$extra</td>\n".4291"</tr>\n";4292}4293print"</table>\n";4294}42954296sub git_heads_body {4297# uses global variable $project4298my($headlist,$head,$from,$to,$extra) =@_;4299$from=0unlessdefined$from;4300$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);43014302print"<table class=\"heads\">\n";4303my$alternate=1;4304for(my$i=$from;$i<=$to;$i++) {4305my$entry=$headlist->[$i];4306my%ref=%$entry;4307my$curr=$ref{'id'}eq$head;4308if($alternate) {4309print"<tr class=\"dark\">\n";4310}else{4311print"<tr class=\"light\">\n";4312}4313$alternate^=1;4314print"<td><i>$ref{'age'}</i></td>\n".4315($curr?"<td class=\"current_head\">":"<td>") .4316$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4317-class=>"list name"},esc_html($ref{'name'})) .4318"</td>\n".4319"<td class=\"link\">".4320$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4321$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4322$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4323"</td>\n".4324"</tr>";4325}4326if(defined$extra) {4327print"<tr>\n".4328"<td colspan=\"3\">$extra</td>\n".4329"</tr>\n";4330}4331print"</table>\n";4332}43334334sub git_search_grep_body {4335my($commitlist,$from,$to,$extra) =@_;4336$from=0unlessdefined$from;4337$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43384339print"<table class=\"commit_search\">\n";4340my$alternate=1;4341for(my$i=$from;$i<=$to;$i++) {4342my%co= %{$commitlist->[$i]};4343if(!%co) {4344next;4345}4346my$commit=$co{'id'};4347if($alternate) {4348print"<tr class=\"dark\">\n";4349}else{4350print"<tr class=\"light\">\n";4351}4352$alternate^=1;4353my$author= chop_and_escape_str($co{'author_name'},15,5);4354print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4355"<td><i>".$author."</i></td>\n".4356"<td>".4357$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4358-class=>"list subject"},4359 chop_and_escape_str($co{'title'},50) ."<br/>");4360my$comment=$co{'comment'};4361foreachmy$line(@$comment) {4362if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4363my($lead,$match,$trail) = ($1,$2,$3);4364$match= chop_str($match,70,5,'center');4365my$contextlen=int((80-length($match))/2);4366$contextlen=30if($contextlen>30);4367$lead= chop_str($lead,$contextlen,10,'left');4368$trail= chop_str($trail,$contextlen,10,'right');43694370$lead= esc_html($lead);4371$match= esc_html($match);4372$trail= esc_html($trail);43734374print"$lead<span class=\"match\">$match</span>$trail<br />";4375}4376}4377print"</td>\n".4378"<td class=\"link\">".4379$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4380" | ".4381$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4382" | ".4383$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4384print"</td>\n".4385"</tr>\n";4386}4387if(defined$extra) {4388print"<tr>\n".4389"<td colspan=\"3\">$extra</td>\n".4390"</tr>\n";4391}4392print"</table>\n";4393}43944395## ======================================================================4396## ======================================================================4397## actions43984399sub git_project_list {4400my$order=$input_params{'order'};4401if(defined$order&&$order!~m/none|project|descr|owner|age/) {4402 die_error(400,"Unknown order parameter");4403}44044405my@list= git_get_projects_list();4406if(!@list) {4407 die_error(404,"No projects found");4408}44094410 git_header_html();4411if(-f $home_text) {4412print"<div class=\"index_include\">\n";4413 insert_file($home_text);4414print"</div>\n";4415}4416print$cgi->startform(-method=>"get") .4417"<p class=\"projsearch\">Search:\n".4418$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4419"</p>".4420$cgi->end_form() ."\n";4421 git_project_list_body(\@list,$order);4422 git_footer_html();4423}44244425sub git_forks {4426my$order=$input_params{'order'};4427if(defined$order&&$order!~m/none|project|descr|owner|age/) {4428 die_error(400,"Unknown order parameter");4429}44304431my@list= git_get_projects_list($project);4432if(!@list) {4433 die_error(404,"No forks found");4434}44354436 git_header_html();4437 git_print_page_nav('','');4438 git_print_header_div('summary',"$projectforks");4439 git_project_list_body(\@list,$order);4440 git_footer_html();4441}44424443sub git_project_index {4444my@projects= git_get_projects_list($project);44454446print$cgi->header(4447-type =>'text/plain',4448-charset =>'utf-8',4449-content_disposition =>'inline; filename="index.aux"');44504451foreachmy$pr(@projects) {4452if(!exists$pr->{'owner'}) {4453$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4454}44554456my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4457# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4458$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4459$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4460$path=~s/ /\+/g;4461$owner=~s/ /\+/g;44624463print"$path$owner\n";4464}4465}44664467sub git_summary {4468my$descr= git_get_project_description($project) ||"none";4469my%co= parse_commit("HEAD");4470my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4471my$head=$co{'id'};44724473my$owner= git_get_project_owner($project);44744475my$refs= git_get_references();4476# These get_*_list functions return one more to allow us to see if4477# there are more ...4478my@taglist= git_get_tags_list(16);4479my@headlist= git_get_heads_list(16);4480my@forklist;4481my$check_forks= gitweb_check_feature('forks');44824483if($check_forks) {4484@forklist= git_get_projects_list($project);4485}44864487 git_header_html();4488 git_print_page_nav('summary','',$head);44894490print"<div class=\"title\"> </div>\n";4491print"<table class=\"projects_list\">\n".4492"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4493"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4494if(defined$cd{'rfc2822'}) {4495print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4496}44974498# use per project git URL list in $projectroot/$project/cloneurl4499# or make project git URL from git base URL and project name4500my$url_tag="URL";4501my@url_list= git_get_project_url_list($project);4502@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4503foreachmy$git_url(@url_list) {4504next unless$git_url;4505print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4506$url_tag="";4507}45084509# Tag cloud4510my$show_ctags= gitweb_check_feature('ctags');4511if($show_ctags) {4512my$ctags= git_get_project_ctags($project);4513my$cloud= git_populate_project_tagcloud($ctags);4514print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4515print"</td>\n<td>"unless%$ctags;4516print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4517print"</td>\n<td>"if%$ctags;4518print git_show_project_tagcloud($cloud,48);4519print"</td></tr>";4520}45214522print"</table>\n";45234524# If XSS prevention is on, we don't include README.html.4525# TODO: Allow a readme in some safe format.4526if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4527print"<div class=\"title\">readme</div>\n".4528"<div class=\"readme\">\n";4529 insert_file("$projectroot/$project/README.html");4530print"\n</div>\n";# class="readme"4531}45324533# we need to request one more than 16 (0..15) to check if4534# those 16 are all4535my@commitlist=$head? parse_commits($head,17) : ();4536if(@commitlist) {4537 git_print_header_div('shortlog');4538 git_shortlog_body(\@commitlist,0,15,$refs,4539$#commitlist<=15?undef:4540$cgi->a({-href => href(action=>"shortlog")},"..."));4541}45424543if(@taglist) {4544 git_print_header_div('tags');4545 git_tags_body(\@taglist,0,15,4546$#taglist<=15?undef:4547$cgi->a({-href => href(action=>"tags")},"..."));4548}45494550if(@headlist) {4551 git_print_header_div('heads');4552 git_heads_body(\@headlist,$head,0,15,4553$#headlist<=15?undef:4554$cgi->a({-href => href(action=>"heads")},"..."));4555}45564557if(@forklist) {4558 git_print_header_div('forks');4559 git_project_list_body(\@forklist,'age',0,15,4560$#forklist<=15?undef:4561$cgi->a({-href => href(action=>"forks")},"..."),4562'no_header');4563}45644565 git_footer_html();4566}45674568sub git_tag {4569my$head= git_get_head_hash($project);4570 git_header_html();4571 git_print_page_nav('','',$head,undef,$head);4572my%tag= parse_tag($hash);45734574if(!%tag) {4575 die_error(404,"Unknown tag object");4576}45774578 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4579print"<div class=\"title_text\">\n".4580"<table class=\"object_header\">\n".4581"<tr>\n".4582"<td>object</td>\n".4583"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4584$tag{'object'}) ."</td>\n".4585"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4586$tag{'type'}) ."</td>\n".4587"</tr>\n";4588if(defined($tag{'author'})) {4589my%ad= parse_date($tag{'epoch'},$tag{'tz'});4590print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4591print"<tr><td></td><td>".$ad{'rfc2822'} .4592sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4593"</td></tr>\n";4594}4595print"</table>\n\n".4596"</div>\n";4597print"<div class=\"page_body\">";4598my$comment=$tag{'comment'};4599foreachmy$line(@$comment) {4600chomp$line;4601print esc_html($line, -nbsp=>1) ."<br/>\n";4602}4603print"</div>\n";4604 git_footer_html();4605}46064607sub git_blame {4608# permissions4609 gitweb_check_feature('blame')4610or die_error(403,"Blame view not allowed");46114612# error checking4613 die_error(400,"No file name given")unless$file_name;4614$hash_base||= git_get_head_hash($project);4615 die_error(404,"Couldn't find base commit")unless$hash_base;4616my%co= parse_commit($hash_base)4617or die_error(404,"Commit not found");4618my$ftype="blob";4619if(!defined$hash) {4620$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4621or die_error(404,"Error looking up file");4622}else{4623$ftype= git_get_type($hash);4624if($ftype!~"blob") {4625 die_error(400,"Object is not a blob");4626}4627}46284629# run git-blame --porcelain4630open my$fd,"-|", git_cmd(),"blame",'-p',4631$hash_base,'--',$file_name4632or die_error(500,"Open git-blame failed");46334634# page header4635 git_header_html();4636my$formats_nav=4637$cgi->a({-href => href(action=>"blob", -replay=>1)},4638"blob") .4639" | ".4640$cgi->a({-href => href(action=>"history", -replay=>1)},4641"history") .4642" | ".4643$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4644"HEAD");4645 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4646 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4647 git_print_page_path($file_name,$ftype,$hash_base);46484649# page body4650my@rev_color=qw(light2 dark2);4651my$num_colors=scalar(@rev_color);4652my$current_color=0;4653my%metainfo= ();46544655print<<HTML;4656<div class="page_body">4657<table class="blame">4658<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4659HTML4660 LINE:4661while(my$line= <$fd>) {4662chomp$line;4663# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4664# no <lines in group> for subsequent lines in group of lines4665my($full_rev,$orig_lineno,$lineno,$group_size) =4666($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4667if(!exists$metainfo{$full_rev}) {4668$metainfo{$full_rev} = {};4669}4670my$meta=$metainfo{$full_rev};4671my$data;4672while($data= <$fd>) {4673chomp$data;4674last if($data=~s/^\t//);# contents of line4675if($data=~/^(\S+) (.*)$/) {4676$meta->{$1} =$2;4677}4678}4679my$short_rev=substr($full_rev,0,8);4680my$author=$meta->{'author'};4681my%date=4682 parse_date($meta->{'author-time'},$meta->{'author-tz'});4683my$date=$date{'iso-tz'};4684if($group_size) {4685$current_color= ($current_color+1) %$num_colors;4686}4687print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4688if($group_size) {4689print"<td class=\"sha1\"";4690print" title=\"". esc_html($author) .",$date\"";4691print" rowspan=\"$group_size\""if($group_size>1);4692print">";4693print$cgi->a({-href => href(action=>"commit",4694 hash=>$full_rev,4695 file_name=>$file_name)},4696 esc_html($short_rev));4697print"</td>\n";4698}4699my$parent_commit;4700if(!exists$meta->{'parent'}) {4701open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4702or die_error(500,"Open git-rev-parse failed");4703$parent_commit= <$dd>;4704close$dd;4705chomp($parent_commit);4706$meta->{'parent'} =$parent_commit;4707}else{4708$parent_commit=$meta->{'parent'};4709}4710my$blamed= href(action =>'blame',4711 file_name =>$meta->{'filename'},4712 hash_base =>$parent_commit);4713print"<td class=\"linenr\">";4714print$cgi->a({ -href =>"$blamed#l$orig_lineno",4715-class=>"linenr"},4716 esc_html($lineno));4717print"</td>";4718print"<td class=\"pre\">". esc_html($data) ."</td>\n";4719print"</tr>\n";4720}4721print"</table>\n";4722print"</div>";4723close$fd4724or print"Reading blob failed\n";47254726# page footer4727 git_footer_html();4728}47294730sub git_tags {4731my$head= git_get_head_hash($project);4732 git_header_html();4733 git_print_page_nav('','',$head,undef,$head);4734 git_print_header_div('summary',$project);47354736my@tagslist= git_get_tags_list();4737if(@tagslist) {4738 git_tags_body(\@tagslist);4739}4740 git_footer_html();4741}47424743sub git_heads {4744my$head= git_get_head_hash($project);4745 git_header_html();4746 git_print_page_nav('','',$head,undef,$head);4747 git_print_header_div('summary',$project);47484749my@headslist= git_get_heads_list();4750if(@headslist) {4751 git_heads_body(\@headslist,$head);4752}4753 git_footer_html();4754}47554756sub git_blob_plain {4757my$type=shift;4758my$expires;47594760if(!defined$hash) {4761if(defined$file_name) {4762my$base=$hash_base|| git_get_head_hash($project);4763$hash= git_get_hash_by_path($base,$file_name,"blob")4764or die_error(404,"Cannot find file");4765}else{4766 die_error(400,"No file name defined");4767}4768}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4769# blobs defined by non-textual hash id's can be cached4770$expires="+1d";4771}47724773open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4774or die_error(500,"Open git-cat-file blob '$hash' failed");47754776# content-type (can include charset)4777$type= blob_contenttype($fd,$file_name,$type);47784779# "save as" filename, even when no $file_name is given4780my$save_as="$hash";4781if(defined$file_name) {4782$save_as=$file_name;4783}elsif($type=~m/^text\//) {4784$save_as.='.txt';4785}47864787# With XSS prevention on, blobs of all types except a few known safe4788# ones are served with "Content-Disposition: attachment" to make sure4789# they don't run in our security domain. For certain image types,4790# blob view writes an <img> tag referring to blob_plain view, and we4791# want to be sure not to break that by serving the image as an4792# attachment (though Firefox 3 doesn't seem to care).4793my$sandbox=$prevent_xss&&4794$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;47954796print$cgi->header(4797-type =>$type,4798-expires =>$expires,4799-content_disposition =>4800($sandbox?'attachment':'inline')4801.'; filename="'.$save_as.'"');4802local$/=undef;4803binmode STDOUT,':raw';4804print<$fd>;4805binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4806close$fd;4807}48084809sub git_blob {4810my$expires;48114812if(!defined$hash) {4813if(defined$file_name) {4814my$base=$hash_base|| git_get_head_hash($project);4815$hash= git_get_hash_by_path($base,$file_name,"blob")4816or die_error(404,"Cannot find file");4817}else{4818 die_error(400,"No file name defined");4819}4820}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4821# blobs defined by non-textual hash id's can be cached4822$expires="+1d";4823}48244825my$have_blame= gitweb_check_feature('blame');4826open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4827or die_error(500,"Couldn't cat$file_name,$hash");4828my$mimetype= blob_mimetype($fd,$file_name);4829if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4830close$fd;4831return git_blob_plain($mimetype);4832}4833# we can have blame only for text/* mimetype4834$have_blame&&= ($mimetype=~m!^text/!);48354836 git_header_html(undef,$expires);4837my$formats_nav='';4838if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4839if(defined$file_name) {4840if($have_blame) {4841$formats_nav.=4842$cgi->a({-href => href(action=>"blame", -replay=>1)},4843"blame") .4844" | ";4845}4846$formats_nav.=4847$cgi->a({-href => href(action=>"history", -replay=>1)},4848"history") .4849" | ".4850$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4851"raw") .4852" | ".4853$cgi->a({-href => href(action=>"blob",4854 hash_base=>"HEAD", file_name=>$file_name)},4855"HEAD");4856}else{4857$formats_nav.=4858$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4859"raw");4860}4861 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4862 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4863}else{4864print"<div class=\"page_nav\">\n".4865"<br/><br/></div>\n".4866"<div class=\"title\">$hash</div>\n";4867}4868 git_print_page_path($file_name,"blob",$hash_base);4869print"<div class=\"page_body\">\n";4870if($mimetype=~m!^image/!) {4871print qq!<img type="$mimetype"!;4872if($file_name) {4873print qq! alt="$file_name" title="$file_name"!;4874}4875print qq! src="! .4876 href(action=>"blob_plain", hash=>$hash,4877 hash_base=>$hash_base, file_name=>$file_name) .4878 qq!"/>\n!;4879}else{4880my$nr;4881while(my$line= <$fd>) {4882chomp$line;4883$nr++;4884$line= untabify($line);4885printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4886$nr,$nr,$nr, esc_html($line, -nbsp=>1);4887}4888}4889close$fd4890or print"Reading blob failed.\n";4891print"</div>";4892 git_footer_html();4893}48944895sub git_tree {4896if(!defined$hash_base) {4897$hash_base="HEAD";4898}4899if(!defined$hash) {4900if(defined$file_name) {4901$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4902}else{4903$hash=$hash_base;4904}4905}4906 die_error(404,"No such tree")unlessdefined($hash);49074908my@entries= ();4909{4910local$/="\0";4911open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4912or die_error(500,"Open git-ls-tree failed");4913@entries=map{chomp;$_} <$fd>;4914close$fd4915or die_error(404,"Reading tree failed");4916}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;5811local$/="\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;5881local$/="\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}