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 the author name of the given commit with the given tag1473# the author name is chopped and escaped according to the other1474# optional parameters (see chop_str).1475sub format_author_html {1476my$tag=shift;1477my$co=shift;1478my$author= chop_and_escape_str($co->{'author_name'},@_);1479return"<$tagclass=\"author\">".$author."</$tag>";1480}14811482# format git diff header line, i.e. "diff --(git|combined|cc) ..."1483sub format_git_diff_header_line {1484my$line=shift;1485my$diffinfo=shift;1486my($from,$to) =@_;14871488if($diffinfo->{'nparents'}) {1489# combined diff1490$line=~s!^(diff (.*?) )"?.*$!$1!;1491if($to->{'href'}) {1492$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1493 esc_path($to->{'file'}));1494}else{# file was deleted (no href)1495$line.= esc_path($to->{'file'});1496}1497}else{1498# "ordinary" diff1499$line=~s!^(diff (.*?) )"?a/.*$!$1!;1500if($from->{'href'}) {1501$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1502'a/'. esc_path($from->{'file'}));1503}else{# file was added (no href)1504$line.='a/'. esc_path($from->{'file'});1505}1506$line.=' ';1507if($to->{'href'}) {1508$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1509'b/'. esc_path($to->{'file'}));1510}else{# file was deleted1511$line.='b/'. esc_path($to->{'file'});1512}1513}15141515return"<div class=\"diff header\">$line</div>\n";1516}15171518# format extended diff header line, before patch itself1519sub format_extended_diff_header_line {1520my$line=shift;1521my$diffinfo=shift;1522my($from,$to) =@_;15231524# match <path>1525if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1526$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1527 esc_path($from->{'file'}));1528}1529if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1530$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1531 esc_path($to->{'file'}));1532}1533# match single <mode>1534if($line=~m/\s(\d{6})$/) {1535$line.='<span class="info"> ('.1536 file_type_long($1) .1537')</span>';1538}1539# match <hash>1540if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1541# can match only for combined diff1542$line='index ';1543for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1544if($from->{'href'}[$i]) {1545$line.=$cgi->a({-href=>$from->{'href'}[$i],1546-class=>"hash"},1547substr($diffinfo->{'from_id'}[$i],0,7));1548}else{1549$line.='0' x 7;1550}1551# separator1552$line.=','if($i<$diffinfo->{'nparents'} -1);1553}1554$line.='..';1555if($to->{'href'}) {1556$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1557substr($diffinfo->{'to_id'},0,7));1558}else{1559$line.='0' x 7;1560}15611562}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1563# can match only for ordinary diff1564my($from_link,$to_link);1565if($from->{'href'}) {1566$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1567substr($diffinfo->{'from_id'},0,7));1568}else{1569$from_link='0' x 7;1570}1571if($to->{'href'}) {1572$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1573substr($diffinfo->{'to_id'},0,7));1574}else{1575$to_link='0' x 7;1576}1577my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1578$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1579}15801581return$line."<br/>\n";1582}15831584# format from-file/to-file diff header1585sub format_diff_from_to_header {1586my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1587my$line;1588my$result='';15891590$line=$from_line;1591#assert($line =~ m/^---/) if DEBUG;1592# no extra formatting for "^--- /dev/null"1593if(!$diffinfo->{'nparents'}) {1594# ordinary (single parent) diff1595if($line=~m!^--- "?a/!) {1596if($from->{'href'}) {1597$line='--- a/'.1598$cgi->a({-href=>$from->{'href'}, -class=>"path"},1599 esc_path($from->{'file'}));1600}else{1601$line='--- a/'.1602 esc_path($from->{'file'});1603}1604}1605$result.= qq!<div class="diff from_file">$line</div>\n!;16061607}else{1608# combined diff (merge commit)1609for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1610if($from->{'href'}[$i]) {1611$line='--- '.1612$cgi->a({-href=>href(action=>"blobdiff",1613 hash_parent=>$diffinfo->{'from_id'}[$i],1614 hash_parent_base=>$parents[$i],1615 file_parent=>$from->{'file'}[$i],1616 hash=>$diffinfo->{'to_id'},1617 hash_base=>$hash,1618 file_name=>$to->{'file'}),1619-class=>"path",1620-title=>"diff". ($i+1)},1621$i+1) .1622'/'.1623$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1624 esc_path($from->{'file'}[$i]));1625}else{1626$line='--- /dev/null';1627}1628$result.= qq!<div class="diff from_file">$line</div>\n!;1629}1630}16311632$line=$to_line;1633#assert($line =~ m/^\+\+\+/) if DEBUG;1634# no extra formatting for "^+++ /dev/null"1635if($line=~m!^\+\+\+ "?b/!) {1636if($to->{'href'}) {1637$line='+++ b/'.1638$cgi->a({-href=>$to->{'href'}, -class=>"path"},1639 esc_path($to->{'file'}));1640}else{1641$line='+++ b/'.1642 esc_path($to->{'file'});1643}1644}1645$result.= qq!<div class="diff to_file">$line</div>\n!;16461647return$result;1648}16491650# create note for patch simplified by combined diff1651sub format_diff_cc_simplified {1652my($diffinfo,@parents) =@_;1653my$result='';16541655$result.="<div class=\"diff header\">".1656"diff --cc ";1657if(!is_deleted($diffinfo)) {1658$result.=$cgi->a({-href => href(action=>"blob",1659 hash_base=>$hash,1660 hash=>$diffinfo->{'to_id'},1661 file_name=>$diffinfo->{'to_file'}),1662-class=>"path"},1663 esc_path($diffinfo->{'to_file'}));1664}else{1665$result.= esc_path($diffinfo->{'to_file'});1666}1667$result.="</div>\n".# class="diff header"1668"<div class=\"diff nodifferences\">".1669"Simple merge".1670"</div>\n";# class="diff nodifferences"16711672return$result;1673}16741675# format patch (diff) line (not to be used for diff headers)1676sub format_diff_line {1677my$line=shift;1678my($from,$to) =@_;1679my$diff_class="";16801681chomp$line;16821683if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1684# combined diff1685my$prefix=substr($line,0,scalar@{$from->{'href'}});1686if($line=~m/^\@{3}/) {1687$diff_class=" chunk_header";1688}elsif($line=~m/^\\/) {1689$diff_class=" incomplete";1690}elsif($prefix=~tr/+/+/) {1691$diff_class=" add";1692}elsif($prefix=~tr/-/-/) {1693$diff_class=" rem";1694}1695}else{1696# assume ordinary diff1697my$char=substr($line,0,1);1698if($chareq'+') {1699$diff_class=" add";1700}elsif($chareq'-') {1701$diff_class=" rem";1702}elsif($chareq'@') {1703$diff_class=" chunk_header";1704}elsif($chareq"\\") {1705$diff_class=" incomplete";1706}1707}1708$line= untabify($line);1709if($from&&$to&&$line=~m/^\@{2} /) {1710my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1711$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;17121713$from_lines=0unlessdefined$from_lines;1714$to_lines=0unlessdefined$to_lines;17151716if($from->{'href'}) {1717$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1718-class=>"list"},$from_text);1719}1720if($to->{'href'}) {1721$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1722-class=>"list"},$to_text);1723}1724$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1725"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1726return"<div class=\"diff$diff_class\">$line</div>\n";1727}elsif($from&&$to&&$line=~m/^\@{3}/) {1728my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1729my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);17301731@from_text=split(' ',$ranges);1732for(my$i=0;$i<@from_text; ++$i) {1733($from_start[$i],$from_nlines[$i]) =1734(split(',',substr($from_text[$i],1)),0);1735}17361737$to_text=pop@from_text;1738$to_start=pop@from_start;1739$to_nlines=pop@from_nlines;17401741$line="<span class=\"chunk_info\">$prefix";1742for(my$i=0;$i<@from_text; ++$i) {1743if($from->{'href'}[$i]) {1744$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1745-class=>"list"},$from_text[$i]);1746}else{1747$line.=$from_text[$i];1748}1749$line.=" ";1750}1751if($to->{'href'}) {1752$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1753-class=>"list"},$to_text);1754}else{1755$line.=$to_text;1756}1757$line.="$prefix</span>".1758"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1759return"<div class=\"diff$diff_class\">$line</div>\n";1760}1761return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1762}17631764# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1765# linked. Pass the hash of the tree/commit to snapshot.1766sub format_snapshot_links {1767my($hash) =@_;1768my$num_fmts=@snapshot_fmts;1769if($num_fmts>1) {1770# A parenthesized list of links bearing format names.1771# e.g. "snapshot (_tar.gz_ _zip_)"1772return"snapshot (".join(' ',map1773$cgi->a({1774-href => href(1775 action=>"snapshot",1776 hash=>$hash,1777 snapshot_format=>$_1778)1779},$known_snapshot_formats{$_}{'display'})1780,@snapshot_fmts) .")";1781}elsif($num_fmts==1) {1782# A single "snapshot" link whose tooltip bears the format name.1783# i.e. "_snapshot_"1784my($fmt) =@snapshot_fmts;1785return1786$cgi->a({1787-href => href(1788 action=>"snapshot",1789 hash=>$hash,1790 snapshot_format=>$fmt1791),1792-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1793},"snapshot");1794}else{# $num_fmts == 01795returnundef;1796}1797}17981799## ......................................................................1800## functions returning values to be passed, perhaps after some1801## transformation, to other functions; e.g. returning arguments to href()18021803# returns hash to be passed to href to generate gitweb URL1804# in -title key it returns description of link1805sub get_feed_info {1806my$format=shift||'Atom';1807my%res= (action =>lc($format));18081809# feed links are possible only for project views1810return unless(defined$project);1811# some views should link to OPML, or to generic project feed,1812# or don't have specific feed yet (so they should use generic)1813return if($action=~/^(?:tags|heads|forks|tag|search)$/x);18141815my$branch;1816# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1817# from tag links; this also makes possible to detect branch links1818if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1819(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1820$branch=$1;1821}1822# find log type for feed description (title)1823my$type='log';1824if(defined$file_name) {1825$type="history of$file_name";1826$type.="/"if($actioneq'tree');1827$type.=" on '$branch'"if(defined$branch);1828}else{1829$type="log of$branch"if(defined$branch);1830}18311832$res{-title} =$type;1833$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1834$res{'file_name'} =$file_name;18351836return%res;1837}18381839## ----------------------------------------------------------------------1840## git utility subroutines, invoking git commands18411842# returns path to the core git executable and the --git-dir parameter as list1843sub git_cmd {1844return$GIT,'--git-dir='.$git_dir;1845}18461847# quote the given arguments for passing them to the shell1848# quote_command("command", "arg 1", "arg with ' and ! characters")1849# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1850# Try to avoid using this function wherever possible.1851sub quote_command {1852returnjoin(' ',1853map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);1854}18551856# get HEAD ref of given project as hash1857sub git_get_head_hash {1858my$project=shift;1859my$o_git_dir=$git_dir;1860my$retval=undef;1861$git_dir="$projectroot/$project";1862if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1863my$head= <$fd>;1864close$fd;1865if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1866$retval=$1;1867}1868}1869if(defined$o_git_dir) {1870$git_dir=$o_git_dir;1871}1872return$retval;1873}18741875# get type of given object1876sub git_get_type {1877my$hash=shift;18781879open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1880my$type= <$fd>;1881close$fdorreturn;1882chomp$type;1883return$type;1884}18851886# repository configuration1887our$config_file='';1888our%config;18891890# store multiple values for single key as anonymous array reference1891# single values stored directly in the hash, not as [ <value> ]1892sub hash_set_multi {1893my($hash,$key,$value) =@_;18941895if(!exists$hash->{$key}) {1896$hash->{$key} =$value;1897}elsif(!ref$hash->{$key}) {1898$hash->{$key} = [$hash->{$key},$value];1899}else{1900push@{$hash->{$key}},$value;1901}1902}19031904# return hash of git project configuration1905# optionally limited to some section, e.g. 'gitweb'1906sub git_parse_project_config {1907my$section_regexp=shift;1908my%config;19091910local$/="\0";19111912open my$fh,"-|", git_cmd(),"config",'-z','-l',1913orreturn;19141915while(my$keyval= <$fh>) {1916chomp$keyval;1917my($key,$value) =split(/\n/,$keyval,2);19181919 hash_set_multi(\%config,$key,$value)1920if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1921}1922close$fh;19231924return%config;1925}19261927# convert config value to boolean: 'true' or 'false'1928# no value, number > 0, 'true' and 'yes' values are true1929# rest of values are treated as false (never as error)1930sub config_to_bool {1931my$val=shift;19321933return1if!defined$val;# section.key19341935# strip leading and trailing whitespace1936$val=~s/^\s+//;1937$val=~s/\s+$//;19381939return(($val=~/^\d+$/&&$val) ||# section.key = 11940($val=~/^(?:true|yes)$/i));# section.key = true1941}19421943# convert config value to simple decimal number1944# an optional value suffix of 'k', 'm', or 'g' will cause the value1945# to be multiplied by 1024, 1048576, or 10737418241946sub config_to_int {1947my$val=shift;19481949# strip leading and trailing whitespace1950$val=~s/^\s+//;1951$val=~s/\s+$//;19521953if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1954$unit=lc($unit);1955# unknown unit is treated as 11956return$num* ($uniteq'g'?1073741824:1957$uniteq'm'?1048576:1958$uniteq'k'?1024:1);1959}1960return$val;1961}19621963# convert config value to array reference, if needed1964sub config_to_multi {1965my$val=shift;19661967returnref($val) ?$val: (defined($val) ? [$val] : []);1968}19691970sub git_get_project_config {1971my($key,$type) =@_;19721973# key sanity check1974return unless($key);1975$key=~s/^gitweb\.//;1976return if($key=~m/\W/);19771978# type sanity check1979if(defined$type) {1980$type=~s/^--//;1981$type=undef1982unless($typeeq'bool'||$typeeq'int');1983}19841985# get config1986if(!defined$config_file||1987$config_filene"$git_dir/config") {1988%config= git_parse_project_config('gitweb');1989$config_file="$git_dir/config";1990}19911992# check if config variable (key) exists1993return unlessexists$config{"gitweb.$key"};19941995# ensure given type1996if(!defined$type) {1997return$config{"gitweb.$key"};1998}elsif($typeeq'bool') {1999# backward compatibility: 'git config --bool' returns true/false2000return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2001}elsif($typeeq'int') {2002return config_to_int($config{"gitweb.$key"});2003}2004return$config{"gitweb.$key"};2005}20062007# get hash of given path at given ref2008sub git_get_hash_by_path {2009my$base=shift;2010my$path=shift||returnundef;2011my$type=shift;20122013$path=~ s,/+$,,;20142015open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2016or die_error(500,"Open git-ls-tree failed");2017my$line= <$fd>;2018close$fdorreturnundef;20192020if(!defined$line) {2021# there is no tree or hash given by $path at $base2022returnundef;2023}20242025#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2026$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2027if(defined$type&&$typene$2) {2028# type doesn't match2029returnundef;2030}2031return$3;2032}20332034# get path of entry with given hash at given tree-ish (ref)2035# used to get 'from' filename for combined diff (merge commit) for renames2036sub git_get_path_by_hash {2037my$base=shift||return;2038my$hash=shift||return;20392040local$/="\0";20412042open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2043orreturnundef;2044while(my$line= <$fd>) {2045chomp$line;20462047#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2048#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2049if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2050close$fd;2051return$1;2052}2053}2054close$fd;2055returnundef;2056}20572058## ......................................................................2059## git utility functions, directly accessing git repository20602061sub git_get_project_description {2062my$path=shift;20632064$git_dir="$projectroot/$path";2065open my$fd,'<',"$git_dir/description"2066orreturn git_get_project_config('description');2067my$descr= <$fd>;2068close$fd;2069if(defined$descr) {2070chomp$descr;2071}2072return$descr;2073}20742075sub git_get_project_ctags {2076my$path=shift;2077my$ctags= {};20782079$git_dir="$projectroot/$path";2080opendir my$dh,"$git_dir/ctags"2081orreturn$ctags;2082foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2083open my$ct,'<',$_ornext;2084my$val= <$ct>;2085chomp$val;2086close$ct;2087my$ctag=$_;$ctag=~ s#.*/##;2088$ctags->{$ctag} =$val;2089}2090closedir$dh;2091$ctags;2092}20932094sub git_populate_project_tagcloud {2095my$ctags=shift;20962097# First, merge different-cased tags; tags vote on casing2098my%ctags_lc;2099foreach(keys%$ctags) {2100$ctags_lc{lc$_}->{count} +=$ctags->{$_};2101if(not$ctags_lc{lc$_}->{topcount}2102or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2103$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2104$ctags_lc{lc$_}->{topname} =$_;2105}2106}21072108my$cloud;2109if(eval{require HTML::TagCloud;1; }) {2110$cloud= HTML::TagCloud->new;2111foreach(sort keys%ctags_lc) {2112# Pad the title with spaces so that the cloud looks2113# less crammed.2114my$title=$ctags_lc{$_}->{topname};2115$title=~s/ / /g;2116$title=~s/^/ /g;2117$title=~s/$/ /g;2118$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2119}2120}else{2121$cloud= \%ctags_lc;2122}2123$cloud;2124}21252126sub git_show_project_tagcloud {2127my($cloud,$count) =@_;2128print STDERR ref($cloud)."..\n";2129if(ref$cloudeq'HTML::TagCloud') {2130return$cloud->html_and_css($count);2131}else{2132my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2133return'<p align="center">'.join(', ',map{2134"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2135}splice(@tags,0,$count)) .'</p>';2136}2137}21382139sub git_get_project_url_list {2140my$path=shift;21412142$git_dir="$projectroot/$path";2143open my$fd,'<',"$git_dir/cloneurl"2144orreturnwantarray?2145@{ config_to_multi(git_get_project_config('url')) } :2146 config_to_multi(git_get_project_config('url'));2147my@git_project_url_list=map{chomp;$_} <$fd>;2148close$fd;21492150returnwantarray?@git_project_url_list: \@git_project_url_list;2151}21522153sub git_get_projects_list {2154my($filter) =@_;2155my@list;21562157$filter||='';2158$filter=~s/\.git$//;21592160my$check_forks= gitweb_check_feature('forks');21612162if(-d $projects_list) {2163# search in directory2164my$dir=$projects_list. ($filter?"/$filter":'');2165# remove the trailing "/"2166$dir=~s!/+$!!;2167my$pfxlen=length("$dir");2168my$pfxdepth= ($dir=~tr!/!!);21692170 File::Find::find({2171 follow_fast =>1,# follow symbolic links2172 follow_skip =>2,# ignore duplicates2173 dangling_symlinks =>0,# ignore dangling symlinks, silently2174 wanted =>sub{2175# skip project-list toplevel, if we get it.2176return if(m!^[/.]$!);2177# only directories can be git repositories2178return unless(-d $_);2179# don't traverse too deep (Find is super slow on os x)2180if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2181$File::Find::prune =1;2182return;2183}21842185my$subdir=substr($File::Find::name,$pfxlen+1);2186# we check related file in $projectroot2187my$path= ($filter?"$filter/":'') .$subdir;2188if(check_export_ok("$projectroot/$path")) {2189push@list, { path =>$path};2190$File::Find::prune =1;2191}2192},2193},"$dir");21942195}elsif(-f $projects_list) {2196# read from file(url-encoded):2197# 'git%2Fgit.git Linus+Torvalds'2198# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2199# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2200my%paths;2201open my$fd,'<',$projects_listorreturn;2202 PROJECT:2203while(my$line= <$fd>) {2204chomp$line;2205my($path,$owner) =split' ',$line;2206$path= unescape($path);2207$owner= unescape($owner);2208if(!defined$path) {2209next;2210}2211if($filterne'') {2212# looking for forks;2213my$pfx=substr($path,0,length($filter));2214if($pfxne$filter) {2215next PROJECT;2216}2217my$sfx=substr($path,length($filter));2218if($sfx!~/^\/.*\.git$/) {2219next PROJECT;2220}2221}elsif($check_forks) {2222 PATH:2223foreachmy$filter(keys%paths) {2224# looking for forks;2225my$pfx=substr($path,0,length($filter));2226if($pfxne$filter) {2227next PATH;2228}2229my$sfx=substr($path,length($filter));2230if($sfx!~/^\/.*\.git$/) {2231next PATH;2232}2233# is a fork, don't include it in2234# the list2235next PROJECT;2236}2237}2238if(check_export_ok("$projectroot/$path")) {2239my$pr= {2240 path =>$path,2241 owner => to_utf8($owner),2242};2243push@list,$pr;2244(my$forks_path=$path) =~s/\.git$//;2245$paths{$forks_path}++;2246}2247}2248close$fd;2249}2250return@list;2251}22522253our$gitweb_project_owner=undef;2254sub git_get_project_list_from_file {22552256return if(defined$gitweb_project_owner);22572258$gitweb_project_owner= {};2259# read from file (url-encoded):2260# 'git%2Fgit.git Linus+Torvalds'2261# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2262# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2263if(-f $projects_list) {2264open(my$fd,'<',$projects_list);2265while(my$line= <$fd>) {2266chomp$line;2267my($pr,$ow) =split' ',$line;2268$pr= unescape($pr);2269$ow= unescape($ow);2270$gitweb_project_owner->{$pr} = to_utf8($ow);2271}2272close$fd;2273}2274}22752276sub git_get_project_owner {2277my$project=shift;2278my$owner;22792280returnundefunless$project;2281$git_dir="$projectroot/$project";22822283if(!defined$gitweb_project_owner) {2284 git_get_project_list_from_file();2285}22862287if(exists$gitweb_project_owner->{$project}) {2288$owner=$gitweb_project_owner->{$project};2289}2290if(!defined$owner){2291$owner= git_get_project_config('owner');2292}2293if(!defined$owner) {2294$owner= get_file_owner("$git_dir");2295}22962297return$owner;2298}22992300sub git_get_last_activity {2301my($path) =@_;2302my$fd;23032304$git_dir="$projectroot/$path";2305open($fd,"-|", git_cmd(),'for-each-ref',2306'--format=%(committer)',2307'--sort=-committerdate',2308'--count=1',2309'refs/heads')orreturn;2310my$most_recent= <$fd>;2311close$fdorreturn;2312if(defined$most_recent&&2313$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2314my$timestamp=$1;2315my$age=time-$timestamp;2316return($age, age_string($age));2317}2318return(undef,undef);2319}23202321sub git_get_references {2322my$type=shift||"";2323my%refs;2324# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112325# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2326open my$fd,"-|", git_cmd(),"show-ref","--dereference",2327($type? ("--","refs/$type") : ())# use -- <pattern> if $type2328orreturn;23292330while(my$line= <$fd>) {2331chomp$line;2332if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2333if(defined$refs{$1}) {2334push@{$refs{$1}},$2;2335}else{2336$refs{$1} = [$2];2337}2338}2339}2340close$fdorreturn;2341return \%refs;2342}23432344sub git_get_rev_name_tags {2345my$hash=shift||returnundef;23462347open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2348orreturn;2349my$name_rev= <$fd>;2350close$fd;23512352if($name_rev=~ m|^$hash tags/(.*)$|) {2353return$1;2354}else{2355# catches also '$hash undefined' output2356returnundef;2357}2358}23592360## ----------------------------------------------------------------------2361## parse to hash functions23622363sub parse_date {2364my$epoch=shift;2365my$tz=shift||"-0000";23662367my%date;2368my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2369my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2370my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2371$date{'hour'} =$hour;2372$date{'minute'} =$min;2373$date{'mday'} =$mday;2374$date{'day'} =$days[$wday];2375$date{'month'} =$months[$mon];2376$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2377$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2378$date{'mday-time'} =sprintf"%d%s%02d:%02d",2379$mday,$months[$mon],$hour,$min;2380$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",23811900+$year,1+$mon,$mday,$hour,$min,$sec;23822383$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2384my$local=$epoch+ ((int$1+ ($2/60)) *3600);2385($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2386$date{'hour_local'} =$hour;2387$date{'minute_local'} =$min;2388$date{'tz_local'} =$tz;2389$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",23901900+$year,$mon+1,$mday,2391$hour,$min,$sec,$tz);2392return%date;2393}23942395sub parse_tag {2396my$tag_id=shift;2397my%tag;2398my@comment;23992400open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2401$tag{'id'} =$tag_id;2402while(my$line= <$fd>) {2403chomp$line;2404if($line=~m/^object ([0-9a-fA-F]{40})$/) {2405$tag{'object'} =$1;2406}elsif($line=~m/^type (.+)$/) {2407$tag{'type'} =$1;2408}elsif($line=~m/^tag (.+)$/) {2409$tag{'name'} =$1;2410}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2411$tag{'author'} =$1;2412$tag{'author_epoch'} =$2;2413$tag{'author_tz'} =$3;2414if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2415$tag{'author_name'} =$1;2416$tag{'author_email'} =$2;2417}else{2418$tag{'author_name'} =$tag{'author'};2419}2420}elsif($line=~m/--BEGIN/) {2421push@comment,$line;2422last;2423}elsif($lineeq"") {2424last;2425}2426}2427push@comment, <$fd>;2428$tag{'comment'} = \@comment;2429close$fdorreturn;2430if(!defined$tag{'name'}) {2431return2432};2433return%tag2434}24352436sub parse_commit_text {2437my($commit_text,$withparents) =@_;2438my@commit_lines=split'\n',$commit_text;2439my%co;24402441pop@commit_lines;# Remove '\0'24422443if(!@commit_lines) {2444return;2445}24462447my$header=shift@commit_lines;2448if($header!~m/^[0-9a-fA-F]{40}/) {2449return;2450}2451($co{'id'},my@parents) =split' ',$header;2452while(my$line=shift@commit_lines) {2453last if$lineeq"\n";2454if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2455$co{'tree'} =$1;2456}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2457push@parents,$1;2458}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2459$co{'author'} =$1;2460$co{'author_epoch'} =$2;2461$co{'author_tz'} =$3;2462if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2463$co{'author_name'} =$1;2464$co{'author_email'} =$2;2465}else{2466$co{'author_name'} =$co{'author'};2467}2468}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2469$co{'committer'} =$1;2470$co{'committer_epoch'} =$2;2471$co{'committer_tz'} =$3;2472$co{'committer_name'} =$co{'committer'};2473if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2474$co{'committer_name'} =$1;2475$co{'committer_email'} =$2;2476}else{2477$co{'committer_name'} =$co{'committer'};2478}2479}2480}2481if(!defined$co{'tree'}) {2482return;2483};2484$co{'parents'} = \@parents;2485$co{'parent'} =$parents[0];24862487foreachmy$title(@commit_lines) {2488$title=~s/^ //;2489if($titlene"") {2490$co{'title'} = chop_str($title,80,5);2491# remove leading stuff of merges to make the interesting part visible2492if(length($title) >50) {2493$title=~s/^Automatic //;2494$title=~s/^merge (of|with) /Merge ... /i;2495if(length($title) >50) {2496$title=~s/(http|rsync):\/\///;2497}2498if(length($title) >50) {2499$title=~s/(master|www|rsync)\.//;2500}2501if(length($title) >50) {2502$title=~s/kernel.org:?//;2503}2504if(length($title) >50) {2505$title=~s/\/pub\/scm//;2506}2507}2508$co{'title_short'} = chop_str($title,50,5);2509last;2510}2511}2512if(!defined$co{'title'} ||$co{'title'}eq"") {2513$co{'title'} =$co{'title_short'} ='(no commit message)';2514}2515# remove added spaces2516foreachmy$line(@commit_lines) {2517$line=~s/^ //;2518}2519$co{'comment'} = \@commit_lines;25202521my$age=time-$co{'committer_epoch'};2522$co{'age'} =$age;2523$co{'age_string'} = age_string($age);2524my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2525if($age>60*60*24*7*2) {2526$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2527$co{'age_string_age'} =$co{'age_string'};2528}else{2529$co{'age_string_date'} =$co{'age_string'};2530$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2531}2532return%co;2533}25342535sub parse_commit {2536my($commit_id) =@_;2537my%co;25382539local$/="\0";25402541open my$fd,"-|", git_cmd(),"rev-list",2542"--parents",2543"--header",2544"--max-count=1",2545$commit_id,2546"--",2547or die_error(500,"Open git-rev-list failed");2548%co= parse_commit_text(<$fd>,1);2549close$fd;25502551return%co;2552}25532554sub parse_commits {2555my($commit_id,$maxcount,$skip,$filename,@args) =@_;2556my@cos;25572558$maxcount||=1;2559$skip||=0;25602561local$/="\0";25622563open my$fd,"-|", git_cmd(),"rev-list",2564"--header",2565@args,2566("--max-count=".$maxcount),2567("--skip=".$skip),2568@extra_options,2569$commit_id,2570"--",2571($filename? ($filename) : ())2572or die_error(500,"Open git-rev-list failed");2573while(my$line= <$fd>) {2574my%co= parse_commit_text($line);2575push@cos, \%co;2576}2577close$fd;25782579returnwantarray?@cos: \@cos;2580}25812582# parse line of git-diff-tree "raw" output2583sub parse_difftree_raw_line {2584my$line=shift;2585my%res;25862587# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2588# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2589if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2590$res{'from_mode'} =$1;2591$res{'to_mode'} =$2;2592$res{'from_id'} =$3;2593$res{'to_id'} =$4;2594$res{'status'} =$5;2595$res{'similarity'} =$6;2596if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2597($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2598}else{2599$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2600}2601}2602# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2603# combined diff (for merge commit)2604elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2605$res{'nparents'} =length($1);2606$res{'from_mode'} = [split(' ',$2) ];2607$res{'to_mode'} =pop@{$res{'from_mode'}};2608$res{'from_id'} = [split(' ',$3) ];2609$res{'to_id'} =pop@{$res{'from_id'}};2610$res{'status'} = [split('',$4) ];2611$res{'to_file'} = unquote($5);2612}2613# 'c512b523472485aef4fff9e57b229d9d243c967f'2614elsif($line=~m/^([0-9a-fA-F]{40})$/) {2615$res{'commit'} =$1;2616}26172618returnwantarray?%res: \%res;2619}26202621# wrapper: return parsed line of git-diff-tree "raw" output2622# (the argument might be raw line, or parsed info)2623sub parsed_difftree_line {2624my$line_or_ref=shift;26252626if(ref($line_or_ref)eq"HASH") {2627# pre-parsed (or generated by hand)2628return$line_or_ref;2629}else{2630return parse_difftree_raw_line($line_or_ref);2631}2632}26332634# parse line of git-ls-tree output2635sub parse_ls_tree_line {2636my$line=shift;2637my%opts=@_;2638my%res;26392640#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2641$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;26422643$res{'mode'} =$1;2644$res{'type'} =$2;2645$res{'hash'} =$3;2646if($opts{'-z'}) {2647$res{'name'} =$4;2648}else{2649$res{'name'} = unquote($4);2650}26512652returnwantarray?%res: \%res;2653}26542655# generates _two_ hashes, references to which are passed as 2 and 3 argument2656sub parse_from_to_diffinfo {2657my($diffinfo,$from,$to,@parents) =@_;26582659if($diffinfo->{'nparents'}) {2660# combined diff2661$from->{'file'} = [];2662$from->{'href'} = [];2663 fill_from_file_info($diffinfo,@parents)2664unlessexists$diffinfo->{'from_file'};2665for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2666$from->{'file'}[$i] =2667defined$diffinfo->{'from_file'}[$i] ?2668$diffinfo->{'from_file'}[$i] :2669$diffinfo->{'to_file'};2670if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2671$from->{'href'}[$i] = href(action=>"blob",2672 hash_base=>$parents[$i],2673 hash=>$diffinfo->{'from_id'}[$i],2674 file_name=>$from->{'file'}[$i]);2675}else{2676$from->{'href'}[$i] =undef;2677}2678}2679}else{2680# ordinary (not combined) diff2681$from->{'file'} =$diffinfo->{'from_file'};2682if($diffinfo->{'status'}ne"A") {# not new (added) file2683$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2684 hash=>$diffinfo->{'from_id'},2685 file_name=>$from->{'file'});2686}else{2687delete$from->{'href'};2688}2689}26902691$to->{'file'} =$diffinfo->{'to_file'};2692if(!is_deleted($diffinfo)) {# file exists in result2693$to->{'href'} = href(action=>"blob", hash_base=>$hash,2694 hash=>$diffinfo->{'to_id'},2695 file_name=>$to->{'file'});2696}else{2697delete$to->{'href'};2698}2699}27002701## ......................................................................2702## parse to array of hashes functions27032704sub git_get_heads_list {2705my$limit=shift;2706my@headslist;27072708open my$fd,'-|', git_cmd(),'for-each-ref',2709($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2710'--format=%(objectname) %(refname) %(subject)%00%(committer)',2711'refs/heads'2712orreturn;2713while(my$line= <$fd>) {2714my%ref_item;27152716chomp$line;2717my($refinfo,$committerinfo) =split(/\0/,$line);2718my($hash,$name,$title) =split(' ',$refinfo,3);2719my($committer,$epoch,$tz) =2720($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2721$ref_item{'fullname'} =$name;2722$name=~s!^refs/heads/!!;27232724$ref_item{'name'} =$name;2725$ref_item{'id'} =$hash;2726$ref_item{'title'} =$title||'(no commit message)';2727$ref_item{'epoch'} =$epoch;2728if($epoch) {2729$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2730}else{2731$ref_item{'age'} ="unknown";2732}27332734push@headslist, \%ref_item;2735}2736close$fd;27372738returnwantarray?@headslist: \@headslist;2739}27402741sub git_get_tags_list {2742my$limit=shift;2743my@tagslist;27442745open my$fd,'-|', git_cmd(),'for-each-ref',2746($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2747'--format=%(objectname) %(objecttype) %(refname) '.2748'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2749'refs/tags'2750orreturn;2751while(my$line= <$fd>) {2752my%ref_item;27532754chomp$line;2755my($refinfo,$creatorinfo) =split(/\0/,$line);2756my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2757my($creator,$epoch,$tz) =2758($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2759$ref_item{'fullname'} =$name;2760$name=~s!^refs/tags/!!;27612762$ref_item{'type'} =$type;2763$ref_item{'id'} =$id;2764$ref_item{'name'} =$name;2765if($typeeq"tag") {2766$ref_item{'subject'} =$title;2767$ref_item{'reftype'} =$reftype;2768$ref_item{'refid'} =$refid;2769}else{2770$ref_item{'reftype'} =$type;2771$ref_item{'refid'} =$id;2772}27732774if($typeeq"tag"||$typeeq"commit") {2775$ref_item{'epoch'} =$epoch;2776if($epoch) {2777$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2778}else{2779$ref_item{'age'} ="unknown";2780}2781}27822783push@tagslist, \%ref_item;2784}2785close$fd;27862787returnwantarray?@tagslist: \@tagslist;2788}27892790## ----------------------------------------------------------------------2791## filesystem-related functions27922793sub get_file_owner {2794my$path=shift;27952796my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2797my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2798if(!defined$gcos) {2799returnundef;2800}2801my$owner=$gcos;2802$owner=~s/[,;].*$//;2803return to_utf8($owner);2804}28052806# assume that file exists2807sub insert_file {2808my$filename=shift;28092810open my$fd,'<',$filename;2811print map{ to_utf8($_) } <$fd>;2812close$fd;2813}28142815## ......................................................................2816## mimetype related functions28172818sub mimetype_guess_file {2819my$filename=shift;2820my$mimemap=shift;2821-r $mimemaporreturnundef;28222823my%mimemap;2824open(my$mh,'<',$mimemap)orreturnundef;2825while(<$mh>) {2826next ifm/^#/;# skip comments2827my($mimetype,$exts) =split(/\t+/);2828if(defined$exts) {2829my@exts=split(/\s+/,$exts);2830foreachmy$ext(@exts) {2831$mimemap{$ext} =$mimetype;2832}2833}2834}2835close($mh);28362837$filename=~/\.([^.]*)$/;2838return$mimemap{$1};2839}28402841sub mimetype_guess {2842my$filename=shift;2843my$mime;2844$filename=~/\./orreturnundef;28452846if($mimetypes_file) {2847my$file=$mimetypes_file;2848if($file!~m!^/!) {# if it is relative path2849# it is relative to project2850$file="$projectroot/$project/$file";2851}2852$mime= mimetype_guess_file($filename,$file);2853}2854$mime||= mimetype_guess_file($filename,'/etc/mime.types');2855return$mime;2856}28572858sub blob_mimetype {2859my$fd=shift;2860my$filename=shift;28612862if($filename) {2863my$mime= mimetype_guess($filename);2864$mimeandreturn$mime;2865}28662867# just in case2868return$default_blob_plain_mimetypeunless$fd;28692870if(-T $fd) {2871return'text/plain';2872}elsif(!$filename) {2873return'application/octet-stream';2874}elsif($filename=~m/\.png$/i) {2875return'image/png';2876}elsif($filename=~m/\.gif$/i) {2877return'image/gif';2878}elsif($filename=~m/\.jpe?g$/i) {2879return'image/jpeg';2880}else{2881return'application/octet-stream';2882}2883}28842885sub blob_contenttype {2886my($fd,$file_name,$type) =@_;28872888$type||= blob_mimetype($fd,$file_name);2889if($typeeq'text/plain'&&defined$default_text_plain_charset) {2890$type.="; charset=$default_text_plain_charset";2891}28922893return$type;2894}28952896## ======================================================================2897## functions printing HTML: header, footer, error page28982899sub git_header_html {2900my$status=shift||"200 OK";2901my$expires=shift;29022903my$title="$site_name";2904if(defined$project) {2905$title.=" - ". to_utf8($project);2906if(defined$action) {2907$title.="/$action";2908if(defined$file_name) {2909$title.=" - ". esc_path($file_name);2910if($actioneq"tree"&&$file_name!~ m|/$|) {2911$title.="/";2912}2913}2914}2915}2916my$content_type;2917# require explicit support from the UA if we are to send the page as2918# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2919# we have to do this because MSIE sometimes globs '*/*', pretending to2920# support xhtml+xml but choking when it gets what it asked for.2921if(defined$cgi->http('HTTP_ACCEPT') &&2922$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2923$cgi->Accept('application/xhtml+xml') !=0) {2924$content_type='application/xhtml+xml';2925}else{2926$content_type='text/html';2927}2928print$cgi->header(-type=>$content_type, -charset =>'utf-8',2929-status=>$status, -expires =>$expires);2930my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2931print<<EOF;2932<?xml version="1.0" encoding="utf-8"?>2933<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2934<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2935<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2936<!-- git core binaries version$git_version-->2937<head>2938<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2939<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2940<meta name="robots" content="index, nofollow"/>2941<title>$title</title>2942EOF2943# the stylesheet, favicon etc urls won't work correctly with path_info2944# unless we set the appropriate base URL2945if($ENV{'PATH_INFO'}) {2946print"<base href=\"".esc_url($base_url)."\"/>\n";2947}2948# print out each stylesheet that exist, providing backwards capability2949# for those people who defined $stylesheet in a config file2950if(defined$stylesheet) {2951print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2952}else{2953foreachmy$stylesheet(@stylesheets) {2954next unless$stylesheet;2955print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2956}2957}2958if(defined$project) {2959my%href_params= get_feed_info();2960if(!exists$href_params{'-title'}) {2961$href_params{'-title'} ='log';2962}29632964foreachmy$formatqw(RSS Atom){2965my$type=lc($format);2966my%link_attr= (2967'-rel'=>'alternate',2968'-title'=>"$project-$href_params{'-title'} -$formatfeed",2969'-type'=>"application/$type+xml"2970);29712972$href_params{'action'} =$type;2973$link_attr{'-href'} = href(%href_params);2974print"<link ".2975"rel=\"$link_attr{'-rel'}\"".2976"title=\"$link_attr{'-title'}\"".2977"href=\"$link_attr{'-href'}\"".2978"type=\"$link_attr{'-type'}\"".2979"/>\n";29802981$href_params{'extra_options'} ='--no-merges';2982$link_attr{'-href'} = href(%href_params);2983$link_attr{'-title'} .=' (no merges)';2984print"<link ".2985"rel=\"$link_attr{'-rel'}\"".2986"title=\"$link_attr{'-title'}\"".2987"href=\"$link_attr{'-href'}\"".2988"type=\"$link_attr{'-type'}\"".2989"/>\n";2990}29912992}else{2993printf('<link rel="alternate" title="%sprojects list" '.2994'href="%s" type="text/plain; charset=utf-8" />'."\n",2995$site_name, href(project=>undef, action=>"project_index"));2996printf('<link rel="alternate" title="%sprojects feeds" '.2997'href="%s" type="text/x-opml" />'."\n",2998$site_name, href(project=>undef, action=>"opml"));2999}3000if(defined$favicon) {3001printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3002}30033004print"</head>\n".3005"<body>\n";30063007if(-f $site_header) {3008 insert_file($site_header);3009}30103011print"<div class=\"page_header\">\n".3012$cgi->a({-href => esc_url($logo_url),3013-title =>$logo_label},3014qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3015print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3016if(defined$project) {3017print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3018if(defined$action) {3019print" /$action";3020}3021print"\n";3022}3023print"</div>\n";30243025my$have_search= gitweb_check_feature('search');3026if(defined$project&&$have_search) {3027if(!defined$searchtext) {3028$searchtext="";3029}3030my$search_hash;3031if(defined$hash_base) {3032$search_hash=$hash_base;3033}elsif(defined$hash) {3034$search_hash=$hash;3035}else{3036$search_hash="HEAD";3037}3038my$action=$my_uri;3039my$use_pathinfo= gitweb_check_feature('pathinfo');3040if($use_pathinfo) {3041$action.="/".esc_url($project);3042}3043print$cgi->startform(-method=>"get", -action =>$action) .3044"<div class=\"search\">\n".3045(!$use_pathinfo&&3046$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3047$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3048$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3049$cgi->popup_menu(-name =>'st', -default=>'commit',3050-values=> ['commit','grep','author','committer','pickaxe']) .3051$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3052" search:\n",3053$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3054"<span title=\"Extended regular expression\">".3055$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3056-checked =>$search_use_regexp) .3057"</span>".3058"</div>".3059$cgi->end_form() ."\n";3060}3061}30623063sub git_footer_html {3064my$feed_class='rss_logo';30653066print"<div class=\"page_footer\">\n";3067if(defined$project) {3068my$descr= git_get_project_description($project);3069if(defined$descr) {3070print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3071}30723073my%href_params= get_feed_info();3074if(!%href_params) {3075$feed_class.=' generic';3076}3077$href_params{'-title'} ||='log';30783079foreachmy$formatqw(RSS Atom){3080$href_params{'action'} =lc($format);3081print$cgi->a({-href => href(%href_params),3082-title =>"$href_params{'-title'}$formatfeed",3083-class=>$feed_class},$format)."\n";3084}30853086}else{3087print$cgi->a({-href => href(project=>undef, action=>"opml"),3088-class=>$feed_class},"OPML") ." ";3089print$cgi->a({-href => href(project=>undef, action=>"project_index"),3090-class=>$feed_class},"TXT") ."\n";3091}3092print"</div>\n";# class="page_footer"30933094if(-f $site_footer) {3095 insert_file($site_footer);3096}30973098print"</body>\n".3099"</html>";3100}31013102# die_error(<http_status_code>, <error_message>)3103# Example: die_error(404, 'Hash not found')3104# By convention, use the following status codes (as defined in RFC 2616):3105# 400: Invalid or missing CGI parameters, or3106# requested object exists but has wrong type.3107# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3108# this server or project.3109# 404: Requested object/revision/project doesn't exist.3110# 500: The server isn't configured properly, or3111# an internal error occurred (e.g. failed assertions caused by bugs), or3112# an unknown error occurred (e.g. the git binary died unexpectedly).3113sub die_error {3114my$status=shift||500;3115my$error=shift||"Internal server error";31163117my%http_responses= (400=>'400 Bad Request',3118403=>'403 Forbidden',3119404=>'404 Not Found',3120500=>'500 Internal Server Error');3121 git_header_html($http_responses{$status});3122print<<EOF;3123<div class="page_body">3124<br /><br />3125$status-$error3126<br />3127</div>3128EOF3129 git_footer_html();3130exit;3131}31323133## ----------------------------------------------------------------------3134## functions printing or outputting HTML: navigation31353136sub git_print_page_nav {3137my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3138$extra=''if!defined$extra;# pager or formats31393140my@navs=qw(summary shortlog log commit commitdiff tree);3141if($suppress) {3142@navs=grep{$_ne$suppress}@navs;3143}31443145my%arg=map{$_=> {action=>$_} }@navs;3146if(defined$head) {3147for(qw(commit commitdiff)) {3148$arg{$_}{'hash'} =$head;3149}3150if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3151for(qw(shortlog log)) {3152$arg{$_}{'hash'} =$head;3153}3154}3155}31563157$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3158$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;31593160my@actions= gitweb_get_feature('actions');3161my%repl= (3162'%'=>'%',3163'n'=>$project,# project name3164'f'=>$git_dir,# project path within filesystem3165'h'=>$treehead||'',# current hash ('h' parameter)3166'b'=>$treebase||'',# hash base ('hb' parameter)3167);3168while(@actions) {3169my($label,$link,$pos) =splice(@actions,0,3);3170# insert3171@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3172# munch munch3173$link=~s/%([%nfhb])/$repl{$1}/g;3174$arg{$label}{'_href'} =$link;3175}31763177print"<div class=\"page_nav\">\n".3178(join" | ",3179map{$_eq$current?3180$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3181}@navs);3182print"<br/>\n$extra<br/>\n".3183"</div>\n";3184}31853186sub format_paging_nav {3187my($action,$hash,$head,$page,$has_next_link) =@_;3188my$paging_nav;318931903191if($hashne$head||$page) {3192$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3193}else{3194$paging_nav.="HEAD";3195}31963197if($page>0) {3198$paging_nav.=" ⋅ ".3199$cgi->a({-href => href(-replay=>1, page=>$page-1),3200-accesskey =>"p", -title =>"Alt-p"},"prev");3201}else{3202$paging_nav.=" ⋅ prev";3203}32043205if($has_next_link) {3206$paging_nav.=" ⋅ ".3207$cgi->a({-href => href(-replay=>1, page=>$page+1),3208-accesskey =>"n", -title =>"Alt-n"},"next");3209}else{3210$paging_nav.=" ⋅ next";3211}32123213return$paging_nav;3214}32153216## ......................................................................3217## functions printing or outputting HTML: div32183219sub git_print_header_div {3220my($action,$title,$hash,$hash_base) =@_;3221my%args= ();32223223$args{'action'} =$action;3224$args{'hash'} =$hashif$hash;3225$args{'hash_base'} =$hash_baseif$hash_base;32263227print"<div class=\"header\">\n".3228$cgi->a({-href => href(%args), -class=>"title"},3229$title?$title:$action) .3230"\n</div>\n";3231}32323233sub print_local_time {3234my%date=@_;3235if($date{'hour_local'} <6) {3236printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3237$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3238}else{3239printf(" (%02d:%02d%s)",3240$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3241}3242}32433244# Outputs the author name and date in long form3245sub git_print_authorship {3246my$co=shift;3247my%opts=@_;3248my$tag=$opts{-tag} ||'div';32493250my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3251print"<$tagclass=\"author_date\">".3252 esc_html($co->{'author_name'}) .3253" [$ad{'rfc2822'}";3254 print_local_time(%ad)if($opts{-localtime});3255print"]</$tag>\n";3256}32573258# Outputs table rows containing the full author or committer information,3259# in the format expected for 'commit' view (& similia).3260# Parameters are a commit hash reference, followed by the list of people3261# to output information for. If the list is empty it defalts to both3262# author and committer.3263sub git_print_authorship_rows {3264my$co=shift;3265# too bad we can't use @people = @_ || ('author', 'committer')3266my@people=@_;3267@people= ('author','committer')unless@people;3268foreachmy$who(@people) {3269my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3270print"<tr><td>$who</td><td>". esc_html($co->{$who}) ."</td></tr>\n".3271"<tr>".3272"<td></td><td>$wd{'rfc2822'}";3273 print_local_time(%wd);3274print"</td>".3275"</tr>\n";3276}3277}32783279sub git_print_page_path {3280my$name=shift;3281my$type=shift;3282my$hb=shift;328332843285print"<div class=\"page_path\">";3286print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3287-title =>'tree root'}, to_utf8("[$project]"));3288print" / ";3289if(defined$name) {3290my@dirname=split'/',$name;3291my$basename=pop@dirname;3292my$fullname='';32933294foreachmy$dir(@dirname) {3295$fullname.= ($fullname?'/':'') .$dir;3296print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3297 hash_base=>$hb),3298-title =>$fullname}, esc_path($dir));3299print" / ";3300}3301if(defined$type&&$typeeq'blob') {3302print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3303 hash_base=>$hb),3304-title =>$name}, esc_path($basename));3305}elsif(defined$type&&$typeeq'tree') {3306print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3307 hash_base=>$hb),3308-title =>$name}, esc_path($basename));3309print" / ";3310}else{3311print esc_path($basename);3312}3313}3314print"<br/></div>\n";3315}33163317sub git_print_log {3318my$log=shift;3319my%opts=@_;33203321if($opts{'-remove_title'}) {3322# remove title, i.e. first line of log3323shift@$log;3324}3325# remove leading empty lines3326while(defined$log->[0] &&$log->[0]eq"") {3327shift@$log;3328}33293330# print log3331my$signoff=0;3332my$empty=0;3333foreachmy$line(@$log) {3334if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3335$signoff=1;3336$empty=0;3337if(!$opts{'-remove_signoff'}) {3338print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3339next;3340}else{3341# remove signoff lines3342next;3343}3344}else{3345$signoff=0;3346}33473348# print only one empty line3349# do not print empty line after signoff3350if($lineeq"") {3351next if($empty||$signoff);3352$empty=1;3353}else{3354$empty=0;3355}33563357print format_log_line_html($line) ."<br/>\n";3358}33593360if($opts{'-final_empty_line'}) {3361# end with single empty line3362print"<br/>\n"unless$empty;3363}3364}33653366# return link target (what link points to)3367sub git_get_link_target {3368my$hash=shift;3369my$link_target;33703371# read link3372open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3373orreturn;3374{3375local$/=undef;3376$link_target= <$fd>;3377}3378close$fd3379orreturn;33803381return$link_target;3382}33833384# given link target, and the directory (basedir) the link is in,3385# return target of link relative to top directory (top tree);3386# return undef if it is not possible (including absolute links).3387sub normalize_link_target {3388my($link_target,$basedir) =@_;33893390# absolute symlinks (beginning with '/') cannot be normalized3391return if(substr($link_target,0,1)eq'/');33923393# normalize link target to path from top (root) tree (dir)3394my$path;3395if($basedir) {3396$path=$basedir.'/'.$link_target;3397}else{3398# we are in top (root) tree (dir)3399$path=$link_target;3400}34013402# remove //, /./, and /../3403my@path_parts;3404foreachmy$part(split('/',$path)) {3405# discard '.' and ''3406next if(!$part||$parteq'.');3407# handle '..'3408if($parteq'..') {3409if(@path_parts) {3410pop@path_parts;3411}else{3412# link leads outside repository (outside top dir)3413return;3414}3415}else{3416push@path_parts,$part;3417}3418}3419$path=join('/',@path_parts);34203421return$path;3422}34233424# print tree entry (row of git_tree), but without encompassing <tr> element3425sub git_print_tree_entry {3426my($t,$basedir,$hash_base,$have_blame) =@_;34273428my%base_key= ();3429$base_key{'hash_base'} =$hash_baseifdefined$hash_base;34303431# The format of a table row is: mode list link. Where mode is3432# the mode of the entry, list is the name of the entry, an href,3433# and link is the action links of the entry.34343435print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3436if($t->{'type'}eq"blob") {3437print"<td class=\"list\">".3438$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3439 file_name=>"$basedir$t->{'name'}",%base_key),3440-class=>"list"}, esc_path($t->{'name'}));3441if(S_ISLNK(oct$t->{'mode'})) {3442my$link_target= git_get_link_target($t->{'hash'});3443if($link_target) {3444my$norm_target= normalize_link_target($link_target,$basedir);3445if(defined$norm_target) {3446print" -> ".3447$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3448 file_name=>$norm_target),3449-title =>$norm_target}, esc_path($link_target));3450}else{3451print" -> ". esc_path($link_target);3452}3453}3454}3455print"</td>\n";3456print"<td class=\"link\">";3457print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3458 file_name=>"$basedir$t->{'name'}",%base_key)},3459"blob");3460if($have_blame) {3461print" | ".3462$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3463 file_name=>"$basedir$t->{'name'}",%base_key)},3464"blame");3465}3466if(defined$hash_base) {3467print" | ".3468$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3469 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3470"history");3471}3472print" | ".3473$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3474 file_name=>"$basedir$t->{'name'}")},3475"raw");3476print"</td>\n";34773478}elsif($t->{'type'}eq"tree") {3479print"<td class=\"list\">";3480print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3481 file_name=>"$basedir$t->{'name'}",%base_key)},3482 esc_path($t->{'name'}));3483print"</td>\n";3484print"<td class=\"link\">";3485print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3486 file_name=>"$basedir$t->{'name'}",%base_key)},3487"tree");3488if(defined$hash_base) {3489print" | ".3490$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3491 file_name=>"$basedir$t->{'name'}")},3492"history");3493}3494print"</td>\n";3495}else{3496# unknown object: we can only present history for it3497# (this includes 'commit' object, i.e. submodule support)3498print"<td class=\"list\">".3499 esc_path($t->{'name'}) .3500"</td>\n";3501print"<td class=\"link\">";3502if(defined$hash_base) {3503print$cgi->a({-href => href(action=>"history",3504 hash_base=>$hash_base,3505 file_name=>"$basedir$t->{'name'}")},3506"history");3507}3508print"</td>\n";3509}3510}35113512## ......................................................................3513## functions printing large fragments of HTML35143515# get pre-image filenames for merge (combined) diff3516sub fill_from_file_info {3517my($diff,@parents) =@_;35183519$diff->{'from_file'} = [ ];3520$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3521for(my$i=0;$i<$diff->{'nparents'};$i++) {3522if($diff->{'status'}[$i]eq'R'||3523$diff->{'status'}[$i]eq'C') {3524$diff->{'from_file'}[$i] =3525 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3526}3527}35283529return$diff;3530}35313532# is current raw difftree line of file deletion3533sub is_deleted {3534my$diffinfo=shift;35353536return$diffinfo->{'to_id'}eq('0' x 40);3537}35383539# does patch correspond to [previous] difftree raw line3540# $diffinfo - hashref of parsed raw diff format3541# $patchinfo - hashref of parsed patch diff format3542# (the same keys as in $diffinfo)3543sub is_patch_split {3544my($diffinfo,$patchinfo) =@_;35453546returndefined$diffinfo&&defined$patchinfo3547&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3548}354935503551sub git_difftree_body {3552my($difftree,$hash,@parents) =@_;3553my($parent) =$parents[0];3554my$have_blame= gitweb_check_feature('blame');3555print"<div class=\"list_head\">\n";3556if($#{$difftree} >10) {3557print(($#{$difftree} +1) ." files changed:\n");3558}3559print"</div>\n";35603561print"<table class=\"".3562(@parents>1?"combined ":"") .3563"diff_tree\">\n";35643565# header only for combined diff in 'commitdiff' view3566my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3567if($has_header) {3568# table header3569print"<thead><tr>\n".3570"<th></th><th></th>\n";# filename, patchN link3571for(my$i=0;$i<@parents;$i++) {3572my$par=$parents[$i];3573print"<th>".3574$cgi->a({-href => href(action=>"commitdiff",3575 hash=>$hash, hash_parent=>$par),3576-title =>'commitdiff to parent number '.3577($i+1) .': '.substr($par,0,7)},3578$i+1) .3579" </th>\n";3580}3581print"</tr></thead>\n<tbody>\n";3582}35833584my$alternate=1;3585my$patchno=0;3586foreachmy$line(@{$difftree}) {3587my$diff= parsed_difftree_line($line);35883589if($alternate) {3590print"<tr class=\"dark\">\n";3591}else{3592print"<tr class=\"light\">\n";3593}3594$alternate^=1;35953596if(exists$diff->{'nparents'}) {# combined diff35973598 fill_from_file_info($diff,@parents)3599unlessexists$diff->{'from_file'};36003601if(!is_deleted($diff)) {3602# file exists in the result (child) commit3603print"<td>".3604$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3605 file_name=>$diff->{'to_file'},3606 hash_base=>$hash),3607-class=>"list"}, esc_path($diff->{'to_file'})) .3608"</td>\n";3609}else{3610print"<td>".3611 esc_path($diff->{'to_file'}) .3612"</td>\n";3613}36143615if($actioneq'commitdiff') {3616# link to patch3617$patchno++;3618print"<td class=\"link\">".3619$cgi->a({-href =>"#patch$patchno"},"patch") .3620" | ".3621"</td>\n";3622}36233624my$has_history=0;3625my$not_deleted=0;3626for(my$i=0;$i<$diff->{'nparents'};$i++) {3627my$hash_parent=$parents[$i];3628my$from_hash=$diff->{'from_id'}[$i];3629my$from_path=$diff->{'from_file'}[$i];3630my$status=$diff->{'status'}[$i];36313632$has_history||= ($statusne'A');3633$not_deleted||= ($statusne'D');36343635if($statuseq'A') {3636print"<td class=\"link\"align=\"right\"> | </td>\n";3637}elsif($statuseq'D') {3638print"<td class=\"link\">".3639$cgi->a({-href => href(action=>"blob",3640 hash_base=>$hash,3641 hash=>$from_hash,3642 file_name=>$from_path)},3643"blob". ($i+1)) .3644" | </td>\n";3645}else{3646if($diff->{'to_id'}eq$from_hash) {3647print"<td class=\"link nochange\">";3648}else{3649print"<td class=\"link\">";3650}3651print$cgi->a({-href => href(action=>"blobdiff",3652 hash=>$diff->{'to_id'},3653 hash_parent=>$from_hash,3654 hash_base=>$hash,3655 hash_parent_base=>$hash_parent,3656 file_name=>$diff->{'to_file'},3657 file_parent=>$from_path)},3658"diff". ($i+1)) .3659" | </td>\n";3660}3661}36623663print"<td class=\"link\">";3664if($not_deleted) {3665print$cgi->a({-href => href(action=>"blob",3666 hash=>$diff->{'to_id'},3667 file_name=>$diff->{'to_file'},3668 hash_base=>$hash)},3669"blob");3670print" | "if($has_history);3671}3672if($has_history) {3673print$cgi->a({-href => href(action=>"history",3674 file_name=>$diff->{'to_file'},3675 hash_base=>$hash)},3676"history");3677}3678print"</td>\n";36793680print"</tr>\n";3681next;# instead of 'else' clause, to avoid extra indent3682}3683# else ordinary diff36843685my($to_mode_oct,$to_mode_str,$to_file_type);3686my($from_mode_oct,$from_mode_str,$from_file_type);3687if($diff->{'to_mode'}ne('0' x 6)) {3688$to_mode_oct=oct$diff->{'to_mode'};3689if(S_ISREG($to_mode_oct)) {# only for regular file3690$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3691}3692$to_file_type= file_type($diff->{'to_mode'});3693}3694if($diff->{'from_mode'}ne('0' x 6)) {3695$from_mode_oct=oct$diff->{'from_mode'};3696if(S_ISREG($to_mode_oct)) {# only for regular file3697$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3698}3699$from_file_type= file_type($diff->{'from_mode'});3700}37013702if($diff->{'status'}eq"A") {# created3703my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3704$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3705$mode_chng.="]</span>";3706print"<td>";3707print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3708 hash_base=>$hash, file_name=>$diff->{'file'}),3709-class=>"list"}, esc_path($diff->{'file'}));3710print"</td>\n";3711print"<td>$mode_chng</td>\n";3712print"<td class=\"link\">";3713if($actioneq'commitdiff') {3714# link to patch3715$patchno++;3716print$cgi->a({-href =>"#patch$patchno"},"patch");3717print" | ";3718}3719print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3720 hash_base=>$hash, file_name=>$diff->{'file'})},3721"blob");3722print"</td>\n";37233724}elsif($diff->{'status'}eq"D") {# deleted3725my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3726print"<td>";3727print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3728 hash_base=>$parent, file_name=>$diff->{'file'}),3729-class=>"list"}, esc_path($diff->{'file'}));3730print"</td>\n";3731print"<td>$mode_chng</td>\n";3732print"<td class=\"link\">";3733if($actioneq'commitdiff') {3734# link to patch3735$patchno++;3736print$cgi->a({-href =>"#patch$patchno"},"patch");3737print" | ";3738}3739print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3740 hash_base=>$parent, file_name=>$diff->{'file'})},3741"blob") ." | ";3742if($have_blame) {3743print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3744 file_name=>$diff->{'file'})},3745"blame") ." | ";3746}3747print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3748 file_name=>$diff->{'file'})},3749"history");3750print"</td>\n";37513752}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3753my$mode_chnge="";3754if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3755$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3756if($from_file_typene$to_file_type) {3757$mode_chnge.=" from$from_file_typeto$to_file_type";3758}3759if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3760if($from_mode_str&&$to_mode_str) {3761$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3762}elsif($to_mode_str) {3763$mode_chnge.=" mode:$to_mode_str";3764}3765}3766$mode_chnge.="]</span>\n";3767}3768print"<td>";3769print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3770 hash_base=>$hash, file_name=>$diff->{'file'}),3771-class=>"list"}, esc_path($diff->{'file'}));3772print"</td>\n";3773print"<td>$mode_chnge</td>\n";3774print"<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 onlu mode changed)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->{'file'})},3786"diff") .3787" | ";3788}3789print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3790 hash_base=>$hash, file_name=>$diff->{'file'})},3791"blob") ." | ";3792if($have_blame) {3793print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3794 file_name=>$diff->{'file'})},3795"blame") ." | ";3796}3797print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3798 file_name=>$diff->{'file'})},3799"history");3800print"</td>\n";38013802}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3803my%status_name= ('R'=>'moved','C'=>'copied');3804my$nstatus=$status_name{$diff->{'status'}};3805my$mode_chng="";3806if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3807# mode also for directories, so we cannot use $to_mode_str3808$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3809}3810print"<td>".3811$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3812 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3813-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3814"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3815$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3816 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3817-class=>"list"}, esc_path($diff->{'from_file'})) .3818" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3819"<td class=\"link\">";3820if($actioneq'commitdiff') {3821# link to patch3822$patchno++;3823print$cgi->a({-href =>"#patch$patchno"},"patch") .3824" | ";3825}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3826# "commit" view and modified file (not only pure rename or copy)3827print$cgi->a({-href => href(action=>"blobdiff",3828 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3829 hash_base=>$hash, hash_parent_base=>$parent,3830 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3831"diff") .3832" | ";3833}3834print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3835 hash_base=>$parent, file_name=>$diff->{'to_file'})},3836"blob") ." | ";3837if($have_blame) {3838print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3839 file_name=>$diff->{'to_file'})},3840"blame") ." | ";3841}3842print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3843 file_name=>$diff->{'to_file'})},3844"history");3845print"</td>\n";38463847}# we should not encounter Unmerged (U) or Unknown (X) status3848print"</tr>\n";3849}3850print"</tbody>"if$has_header;3851print"</table>\n";3852}38533854sub git_patchset_body {3855my($fd,$difftree,$hash,@hash_parents) =@_;3856my($hash_parent) =$hash_parents[0];38573858my$is_combined= (@hash_parents>1);3859my$patch_idx=0;3860my$patch_number=0;3861my$patch_line;3862my$diffinfo;3863my$to_name;3864my(%from,%to);38653866print"<div class=\"patchset\">\n";38673868# skip to first patch3869while($patch_line= <$fd>) {3870chomp$patch_line;38713872last if($patch_line=~m/^diff /);3873}38743875 PATCH:3876while($patch_line) {38773878# parse "git diff" header line3879if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3880# $1 is from_name, which we do not use3881$to_name= unquote($2);3882$to_name=~s!^b/!!;3883}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3884# $1 is 'cc' or 'combined', which we do not use3885$to_name= unquote($2);3886}else{3887$to_name=undef;3888}38893890# check if current patch belong to current raw line3891# and parse raw git-diff line if needed3892if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3893# this is continuation of a split patch3894print"<div class=\"patch cont\">\n";3895}else{3896# advance raw git-diff output if needed3897$patch_idx++ifdefined$diffinfo;38983899# read and prepare patch information3900$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39013902# compact combined diff output can have some patches skipped3903# find which patch (using pathname of result) we are at now;3904if($is_combined) {3905while($to_namene$diffinfo->{'to_file'}) {3906print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3907 format_diff_cc_simplified($diffinfo,@hash_parents) .3908"</div>\n";# class="patch"39093910$patch_idx++;3911$patch_number++;39123913last if$patch_idx>$#$difftree;3914$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3915}3916}39173918# modifies %from, %to hashes3919 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);39203921# this is first patch for raw difftree line with $patch_idx index3922# we index @$difftree array from 0, but number patches from 13923print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3924}39253926# git diff header3927#assert($patch_line =~ m/^diff /) if DEBUG;3928#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3929$patch_number++;3930# print "git diff" header3931print format_git_diff_header_line($patch_line,$diffinfo,3932 \%from, \%to);39333934# print extended diff header3935print"<div class=\"diff extended_header\">\n";3936 EXTENDED_HEADER:3937while($patch_line= <$fd>) {3938chomp$patch_line;39393940last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);39413942print format_extended_diff_header_line($patch_line,$diffinfo,3943 \%from, \%to);3944}3945print"</div>\n";# class="diff extended_header"39463947# from-file/to-file diff header3948if(!$patch_line) {3949print"</div>\n";# class="patch"3950last PATCH;3951}3952next PATCH if($patch_line=~m/^diff /);3953#assert($patch_line =~ m/^---/) if DEBUG;39543955my$last_patch_line=$patch_line;3956$patch_line= <$fd>;3957chomp$patch_line;3958#assert($patch_line =~ m/^\+\+\+/) if DEBUG;39593960print format_diff_from_to_header($last_patch_line,$patch_line,3961$diffinfo, \%from, \%to,3962@hash_parents);39633964# the patch itself3965 LINE:3966while($patch_line= <$fd>) {3967chomp$patch_line;39683969next PATCH if($patch_line=~m/^diff /);39703971print format_diff_line($patch_line, \%from, \%to);3972}39733974}continue{3975print"</div>\n";# class="patch"3976}39773978# for compact combined (--cc) format, with chunk and patch simpliciaction3979# patchset might be empty, but there might be unprocessed raw lines3980for(++$patch_idxif$patch_number>0;3981$patch_idx<@$difftree;3982++$patch_idx) {3983# read and prepare patch information3984$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39853986# generate anchor for "patch" links in difftree / whatchanged part3987print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3988 format_diff_cc_simplified($diffinfo,@hash_parents) .3989"</div>\n";# class="patch"39903991$patch_number++;3992}39933994if($patch_number==0) {3995if(@hash_parents>1) {3996print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3997}else{3998print"<div class=\"diff nodifferences\">No differences found</div>\n";3999}4000}40014002print"</div>\n";# class="patchset"4003}40044005# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40064007# fills project list info (age, description, owner, forks) for each4008# project in the list, removing invalid projects from returned list4009# NOTE: modifies $projlist, but does not remove entries from it4010sub fill_project_list_info {4011my($projlist,$check_forks) =@_;4012my@projects;40134014my$show_ctags= gitweb_check_feature('ctags');4015 PROJECT:4016foreachmy$pr(@$projlist) {4017my(@activity) = git_get_last_activity($pr->{'path'});4018unless(@activity) {4019next PROJECT;4020}4021($pr->{'age'},$pr->{'age_string'}) =@activity;4022if(!defined$pr->{'descr'}) {4023my$descr= git_get_project_description($pr->{'path'}) ||"";4024$descr= to_utf8($descr);4025$pr->{'descr_long'} =$descr;4026$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4027}4028if(!defined$pr->{'owner'}) {4029$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4030}4031if($check_forks) {4032my$pname=$pr->{'path'};4033if(($pname=~s/\.git$//) &&4034($pname!~/\/$/) &&4035(-d "$projectroot/$pname")) {4036$pr->{'forks'} ="-d$projectroot/$pname";4037}else{4038$pr->{'forks'} =0;4039}4040}4041$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4042push@projects,$pr;4043}40444045return@projects;4046}40474048# print 'sort by' <th> element, generating 'sort by $name' replay link4049# if that order is not selected4050sub print_sort_th {4051my($name,$order,$header) =@_;4052$header||=ucfirst($name);40534054if($ordereq$name) {4055print"<th>$header</th>\n";4056}else{4057print"<th>".4058$cgi->a({-href => href(-replay=>1, order=>$name),4059-class=>"header"},$header) .4060"</th>\n";4061}4062}40634064sub git_project_list_body {4065# actually uses global variable $project4066my($projlist,$order,$from,$to,$extra,$no_header) =@_;40674068my$check_forks= gitweb_check_feature('forks');4069my@projects= fill_project_list_info($projlist,$check_forks);40704071$order||=$default_projects_order;4072$from=0unlessdefined$from;4073$to=$#projectsif(!defined$to||$#projects<$to);40744075my%order_info= (4076 project => { key =>'path', type =>'str'},4077 descr => { key =>'descr_long', type =>'str'},4078 owner => { key =>'owner', type =>'str'},4079 age => { key =>'age', type =>'num'}4080);4081my$oi=$order_info{$order};4082if($oi->{'type'}eq'str') {4083@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4084}else{4085@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4086}40874088my$show_ctags= gitweb_check_feature('ctags');4089if($show_ctags) {4090my%ctags;4091foreachmy$p(@projects) {4092foreachmy$ct(keys%{$p->{'ctags'}}) {4093$ctags{$ct} +=$p->{'ctags'}->{$ct};4094}4095}4096my$cloud= git_populate_project_tagcloud(\%ctags);4097print git_show_project_tagcloud($cloud,64);4098}40994100print"<table class=\"project_list\">\n";4101unless($no_header) {4102print"<tr>\n";4103if($check_forks) {4104print"<th></th>\n";4105}4106 print_sort_th('project',$order,'Project');4107 print_sort_th('descr',$order,'Description');4108 print_sort_th('owner',$order,'Owner');4109 print_sort_th('age',$order,'Last Change');4110print"<th></th>\n".# for links4111"</tr>\n";4112}4113my$alternate=1;4114my$tagfilter=$cgi->param('by_tag');4115for(my$i=$from;$i<=$to;$i++) {4116my$pr=$projects[$i];41174118next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4119next if$searchtextand not$pr->{'path'} =~/$searchtext/4120and not$pr->{'descr_long'} =~/$searchtext/;4121# Weed out forks or non-matching entries of search4122if($check_forks) {4123my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4124$forkbase="^$forkbase"if$forkbase;4125next ifnot$searchtextand not$tagfilterand$show_ctags4126and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4127}41284129if($alternate) {4130print"<tr class=\"dark\">\n";4131}else{4132print"<tr class=\"light\">\n";4133}4134$alternate^=1;4135if($check_forks) {4136print"<td>";4137if($pr->{'forks'}) {4138print"<!--$pr->{'forks'} -->\n";4139print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4140}4141print"</td>\n";4142}4143print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4144-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4145"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4146-class=>"list", -title =>$pr->{'descr_long'}},4147 esc_html($pr->{'descr'})) ."</td>\n".4148"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4149print"<td class=\"". age_class($pr->{'age'}) ."\">".4150(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4151"<td class=\"link\">".4152$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4153$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4154$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4155$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4156($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4157"</td>\n".4158"</tr>\n";4159}4160if(defined$extra) {4161print"<tr>\n";4162if($check_forks) {4163print"<td></td>\n";4164}4165print"<td colspan=\"5\">$extra</td>\n".4166"</tr>\n";4167}4168print"</table>\n";4169}41704171sub git_shortlog_body {4172# uses global variable $project4173my($commitlist,$from,$to,$refs,$extra) =@_;41744175$from=0unlessdefined$from;4176$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41774178print"<table class=\"shortlog\">\n";4179my$alternate=1;4180for(my$i=$from;$i<=$to;$i++) {4181my%co= %{$commitlist->[$i]};4182my$commit=$co{'id'};4183my$ref= format_ref_marker($refs,$commit);4184if($alternate) {4185print"<tr class=\"dark\">\n";4186}else{4187print"<tr class=\"light\">\n";4188}4189$alternate^=1;4190# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4191print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4192 format_author_html('td', \%co,10) ."<td>";4193print format_subject_html($co{'title'},$co{'title_short'},4194 href(action=>"commit", hash=>$commit),$ref);4195print"</td>\n".4196"<td class=\"link\">".4197$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4198$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4199$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4200my$snapshot_links= format_snapshot_links($commit);4201if(defined$snapshot_links) {4202print" | ".$snapshot_links;4203}4204print"</td>\n".4205"</tr>\n";4206}4207if(defined$extra) {4208print"<tr>\n".4209"<td colspan=\"4\">$extra</td>\n".4210"</tr>\n";4211}4212print"</table>\n";4213}42144215sub git_history_body {4216# Warning: assumes constant type (blob or tree) during history4217my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;42184219$from=0unlessdefined$from;4220$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});42214222print"<table class=\"history\">\n";4223my$alternate=1;4224for(my$i=$from;$i<=$to;$i++) {4225my%co= %{$commitlist->[$i]};4226if(!%co) {4227next;4228}4229my$commit=$co{'id'};42304231my$ref= format_ref_marker($refs,$commit);42324233if($alternate) {4234print"<tr class=\"dark\">\n";4235}else{4236print"<tr class=\"light\">\n";4237}4238$alternate^=1;4239print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4240# shortlog: format_author_html('td', \%co, 10)4241 format_author_html('td', \%co,15,3) ."<td>";4242# originally git_history used chop_str($co{'title'}, 50)4243print format_subject_html($co{'title'},$co{'title_short'},4244 href(action=>"commit", hash=>$commit),$ref);4245print"</td>\n".4246"<td class=\"link\">".4247$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4248$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");42494250if($ftypeeq'blob') {4251my$blob_current= git_get_hash_by_path($hash_base,$file_name);4252my$blob_parent= git_get_hash_by_path($commit,$file_name);4253if(defined$blob_current&&defined$blob_parent&&4254$blob_currentne$blob_parent) {4255print" | ".4256$cgi->a({-href => href(action=>"blobdiff",4257 hash=>$blob_current, hash_parent=>$blob_parent,4258 hash_base=>$hash_base, hash_parent_base=>$commit,4259 file_name=>$file_name)},4260"diff to current");4261}4262}4263print"</td>\n".4264"</tr>\n";4265}4266if(defined$extra) {4267print"<tr>\n".4268"<td colspan=\"4\">$extra</td>\n".4269"</tr>\n";4270}4271print"</table>\n";4272}42734274sub git_tags_body {4275# uses global variable $project4276my($taglist,$from,$to,$extra) =@_;4277$from=0unlessdefined$from;4278$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);42794280print"<table class=\"tags\">\n";4281my$alternate=1;4282for(my$i=$from;$i<=$to;$i++) {4283my$entry=$taglist->[$i];4284my%tag=%$entry;4285my$comment=$tag{'subject'};4286my$comment_short;4287if(defined$comment) {4288$comment_short= chop_str($comment,30,5);4289}4290if($alternate) {4291print"<tr class=\"dark\">\n";4292}else{4293print"<tr class=\"light\">\n";4294}4295$alternate^=1;4296if(defined$tag{'age'}) {4297print"<td><i>$tag{'age'}</i></td>\n";4298}else{4299print"<td></td>\n";4300}4301print"<td>".4302$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4303-class=>"list name"}, esc_html($tag{'name'})) .4304"</td>\n".4305"<td>";4306if(defined$comment) {4307print format_subject_html($comment,$comment_short,4308 href(action=>"tag", hash=>$tag{'id'}));4309}4310print"</td>\n".4311"<td class=\"selflink\">";4312if($tag{'type'}eq"tag") {4313print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4314}else{4315print" ";4316}4317print"</td>\n".4318"<td class=\"link\">"." | ".4319$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4320if($tag{'reftype'}eq"commit") {4321print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4322" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4323}elsif($tag{'reftype'}eq"blob") {4324print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4325}4326print"</td>\n".4327"</tr>";4328}4329if(defined$extra) {4330print"<tr>\n".4331"<td colspan=\"5\">$extra</td>\n".4332"</tr>\n";4333}4334print"</table>\n";4335}43364337sub git_heads_body {4338# uses global variable $project4339my($headlist,$head,$from,$to,$extra) =@_;4340$from=0unlessdefined$from;4341$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);43424343print"<table class=\"heads\">\n";4344my$alternate=1;4345for(my$i=$from;$i<=$to;$i++) {4346my$entry=$headlist->[$i];4347my%ref=%$entry;4348my$curr=$ref{'id'}eq$head;4349if($alternate) {4350print"<tr class=\"dark\">\n";4351}else{4352print"<tr class=\"light\">\n";4353}4354$alternate^=1;4355print"<td><i>$ref{'age'}</i></td>\n".4356($curr?"<td class=\"current_head\">":"<td>") .4357$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4358-class=>"list name"},esc_html($ref{'name'})) .4359"</td>\n".4360"<td class=\"link\">".4361$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4362$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4363$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4364"</td>\n".4365"</tr>";4366}4367if(defined$extra) {4368print"<tr>\n".4369"<td colspan=\"3\">$extra</td>\n".4370"</tr>\n";4371}4372print"</table>\n";4373}43744375sub git_search_grep_body {4376my($commitlist,$from,$to,$extra) =@_;4377$from=0unlessdefined$from;4378$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43794380print"<table class=\"commit_search\">\n";4381my$alternate=1;4382for(my$i=$from;$i<=$to;$i++) {4383my%co= %{$commitlist->[$i]};4384if(!%co) {4385next;4386}4387my$commit=$co{'id'};4388if($alternate) {4389print"<tr class=\"dark\">\n";4390}else{4391print"<tr class=\"light\">\n";4392}4393$alternate^=1;4394print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4395 format_author_html('td', \%co,15,5) .4396"<td>".4397$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4398-class=>"list subject"},4399 chop_and_escape_str($co{'title'},50) ."<br/>");4400my$comment=$co{'comment'};4401foreachmy$line(@$comment) {4402if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4403my($lead,$match,$trail) = ($1,$2,$3);4404$match= chop_str($match,70,5,'center');4405my$contextlen=int((80-length($match))/2);4406$contextlen=30if($contextlen>30);4407$lead= chop_str($lead,$contextlen,10,'left');4408$trail= chop_str($trail,$contextlen,10,'right');44094410$lead= esc_html($lead);4411$match= esc_html($match);4412$trail= esc_html($trail);44134414print"$lead<span class=\"match\">$match</span>$trail<br />";4415}4416}4417print"</td>\n".4418"<td class=\"link\">".4419$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4420" | ".4421$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4422" | ".4423$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4424print"</td>\n".4425"</tr>\n";4426}4427if(defined$extra) {4428print"<tr>\n".4429"<td colspan=\"3\">$extra</td>\n".4430"</tr>\n";4431}4432print"</table>\n";4433}44344435## ======================================================================4436## ======================================================================4437## actions44384439sub git_project_list {4440my$order=$input_params{'order'};4441if(defined$order&&$order!~m/none|project|descr|owner|age/) {4442 die_error(400,"Unknown order parameter");4443}44444445my@list= git_get_projects_list();4446if(!@list) {4447 die_error(404,"No projects found");4448}44494450 git_header_html();4451if(-f $home_text) {4452print"<div class=\"index_include\">\n";4453 insert_file($home_text);4454print"</div>\n";4455}4456print$cgi->startform(-method=>"get") .4457"<p class=\"projsearch\">Search:\n".4458$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4459"</p>".4460$cgi->end_form() ."\n";4461 git_project_list_body(\@list,$order);4462 git_footer_html();4463}44644465sub git_forks {4466my$order=$input_params{'order'};4467if(defined$order&&$order!~m/none|project|descr|owner|age/) {4468 die_error(400,"Unknown order parameter");4469}44704471my@list= git_get_projects_list($project);4472if(!@list) {4473 die_error(404,"No forks found");4474}44754476 git_header_html();4477 git_print_page_nav('','');4478 git_print_header_div('summary',"$projectforks");4479 git_project_list_body(\@list,$order);4480 git_footer_html();4481}44824483sub git_project_index {4484my@projects= git_get_projects_list($project);44854486print$cgi->header(4487-type =>'text/plain',4488-charset =>'utf-8',4489-content_disposition =>'inline; filename="index.aux"');44904491foreachmy$pr(@projects) {4492if(!exists$pr->{'owner'}) {4493$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4494}44954496my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4497# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4498$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4499$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4500$path=~s/ /\+/g;4501$owner=~s/ /\+/g;45024503print"$path$owner\n";4504}4505}45064507sub git_summary {4508my$descr= git_get_project_description($project) ||"none";4509my%co= parse_commit("HEAD");4510my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4511my$head=$co{'id'};45124513my$owner= git_get_project_owner($project);45144515my$refs= git_get_references();4516# These get_*_list functions return one more to allow us to see if4517# there are more ...4518my@taglist= git_get_tags_list(16);4519my@headlist= git_get_heads_list(16);4520my@forklist;4521my$check_forks= gitweb_check_feature('forks');45224523if($check_forks) {4524@forklist= git_get_projects_list($project);4525}45264527 git_header_html();4528 git_print_page_nav('summary','',$head);45294530print"<div class=\"title\"> </div>\n";4531print"<table class=\"projects_list\">\n".4532"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4533"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4534if(defined$cd{'rfc2822'}) {4535print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4536}45374538# use per project git URL list in $projectroot/$project/cloneurl4539# or make project git URL from git base URL and project name4540my$url_tag="URL";4541my@url_list= git_get_project_url_list($project);4542@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4543foreachmy$git_url(@url_list) {4544next unless$git_url;4545print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4546$url_tag="";4547}45484549# Tag cloud4550my$show_ctags= gitweb_check_feature('ctags');4551if($show_ctags) {4552my$ctags= git_get_project_ctags($project);4553my$cloud= git_populate_project_tagcloud($ctags);4554print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4555print"</td>\n<td>"unless%$ctags;4556print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4557print"</td>\n<td>"if%$ctags;4558print git_show_project_tagcloud($cloud,48);4559print"</td></tr>";4560}45614562print"</table>\n";45634564# If XSS prevention is on, we don't include README.html.4565# TODO: Allow a readme in some safe format.4566if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4567print"<div class=\"title\">readme</div>\n".4568"<div class=\"readme\">\n";4569 insert_file("$projectroot/$project/README.html");4570print"\n</div>\n";# class="readme"4571}45724573# we need to request one more than 16 (0..15) to check if4574# those 16 are all4575my@commitlist=$head? parse_commits($head,17) : ();4576if(@commitlist) {4577 git_print_header_div('shortlog');4578 git_shortlog_body(\@commitlist,0,15,$refs,4579$#commitlist<=15?undef:4580$cgi->a({-href => href(action=>"shortlog")},"..."));4581}45824583if(@taglist) {4584 git_print_header_div('tags');4585 git_tags_body(\@taglist,0,15,4586$#taglist<=15?undef:4587$cgi->a({-href => href(action=>"tags")},"..."));4588}45894590if(@headlist) {4591 git_print_header_div('heads');4592 git_heads_body(\@headlist,$head,0,15,4593$#headlist<=15?undef:4594$cgi->a({-href => href(action=>"heads")},"..."));4595}45964597if(@forklist) {4598 git_print_header_div('forks');4599 git_project_list_body(\@forklist,'age',0,15,4600$#forklist<=15?undef:4601$cgi->a({-href => href(action=>"forks")},"..."),4602'no_header');4603}46044605 git_footer_html();4606}46074608sub git_tag {4609my$head= git_get_head_hash($project);4610 git_header_html();4611 git_print_page_nav('','',$head,undef,$head);4612my%tag= parse_tag($hash);46134614if(!%tag) {4615 die_error(404,"Unknown tag object");4616}46174618 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4619print"<div class=\"title_text\">\n".4620"<table class=\"object_header\">\n".4621"<tr>\n".4622"<td>object</td>\n".4623"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4624$tag{'object'}) ."</td>\n".4625"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4626$tag{'type'}) ."</td>\n".4627"</tr>\n";4628if(defined($tag{'author'})) {4629 git_print_authorship_rows(\%tag,'author');4630}4631print"</table>\n\n".4632"</div>\n";4633print"<div class=\"page_body\">";4634my$comment=$tag{'comment'};4635foreachmy$line(@$comment) {4636chomp$line;4637print esc_html($line, -nbsp=>1) ."<br/>\n";4638}4639print"</div>\n";4640 git_footer_html();4641}46424643sub git_blame {4644# permissions4645 gitweb_check_feature('blame')4646or die_error(403,"Blame view not allowed");46474648# error checking4649 die_error(400,"No file name given")unless$file_name;4650$hash_base||= git_get_head_hash($project);4651 die_error(404,"Couldn't find base commit")unless$hash_base;4652my%co= parse_commit($hash_base)4653or die_error(404,"Commit not found");4654my$ftype="blob";4655if(!defined$hash) {4656$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4657or die_error(404,"Error looking up file");4658}else{4659$ftype= git_get_type($hash);4660if($ftype!~"blob") {4661 die_error(400,"Object is not a blob");4662}4663}46644665# run git-blame --porcelain4666open my$fd,"-|", git_cmd(),"blame",'-p',4667$hash_base,'--',$file_name4668or die_error(500,"Open git-blame failed");46694670# page header4671 git_header_html();4672my$formats_nav=4673$cgi->a({-href => href(action=>"blob", -replay=>1)},4674"blob") .4675" | ".4676$cgi->a({-href => href(action=>"history", -replay=>1)},4677"history") .4678" | ".4679$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4680"HEAD");4681 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4682 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4683 git_print_page_path($file_name,$ftype,$hash_base);46844685# page body4686my@rev_color=qw(light2 dark2);4687my$num_colors=scalar(@rev_color);4688my$current_color=0;4689my%metainfo= ();46904691print<<HTML;4692<div class="page_body">4693<table class="blame">4694<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4695HTML4696 LINE:4697while(my$line= <$fd>) {4698chomp$line;4699# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4700# no <lines in group> for subsequent lines in group of lines4701my($full_rev,$orig_lineno,$lineno,$group_size) =4702($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4703if(!exists$metainfo{$full_rev}) {4704$metainfo{$full_rev} = {};4705}4706my$meta=$metainfo{$full_rev};4707my$data;4708while($data= <$fd>) {4709chomp$data;4710last if($data=~s/^\t//);# contents of line4711if($data=~/^(\S+) (.*)$/) {4712$meta->{$1} =$2;4713}4714}4715my$short_rev=substr($full_rev,0,8);4716my$author=$meta->{'author'};4717my%date=4718 parse_date($meta->{'author-time'},$meta->{'author-tz'});4719my$date=$date{'iso-tz'};4720if($group_size) {4721$current_color= ($current_color+1) %$num_colors;4722}4723print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4724if($group_size) {4725print"<td class=\"sha1\"";4726print" title=\"". esc_html($author) .",$date\"";4727print" rowspan=\"$group_size\""if($group_size>1);4728print">";4729print$cgi->a({-href => href(action=>"commit",4730 hash=>$full_rev,4731 file_name=>$file_name)},4732 esc_html($short_rev));4733print"</td>\n";4734}4735my$parent_commit;4736if(!exists$meta->{'parent'}) {4737open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4738or die_error(500,"Open git-rev-parse failed");4739$parent_commit= <$dd>;4740close$dd;4741chomp($parent_commit);4742$meta->{'parent'} =$parent_commit;4743}else{4744$parent_commit=$meta->{'parent'};4745}4746my$blamed= href(action =>'blame',4747 file_name =>$meta->{'filename'},4748 hash_base =>$parent_commit);4749print"<td class=\"linenr\">";4750print$cgi->a({ -href =>"$blamed#l$orig_lineno",4751-class=>"linenr"},4752 esc_html($lineno));4753print"</td>";4754print"<td class=\"pre\">". esc_html($data) ."</td>\n";4755print"</tr>\n";4756}4757print"</table>\n";4758print"</div>";4759close$fd4760or print"Reading blob failed\n";47614762# page footer4763 git_footer_html();4764}47654766sub git_tags {4767my$head= git_get_head_hash($project);4768 git_header_html();4769 git_print_page_nav('','',$head,undef,$head);4770 git_print_header_div('summary',$project);47714772my@tagslist= git_get_tags_list();4773if(@tagslist) {4774 git_tags_body(\@tagslist);4775}4776 git_footer_html();4777}47784779sub git_heads {4780my$head= git_get_head_hash($project);4781 git_header_html();4782 git_print_page_nav('','',$head,undef,$head);4783 git_print_header_div('summary',$project);47844785my@headslist= git_get_heads_list();4786if(@headslist) {4787 git_heads_body(\@headslist,$head);4788}4789 git_footer_html();4790}47914792sub git_blob_plain {4793my$type=shift;4794my$expires;47954796if(!defined$hash) {4797if(defined$file_name) {4798my$base=$hash_base|| git_get_head_hash($project);4799$hash= git_get_hash_by_path($base,$file_name,"blob")4800or die_error(404,"Cannot find file");4801}else{4802 die_error(400,"No file name defined");4803}4804}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4805# blobs defined by non-textual hash id's can be cached4806$expires="+1d";4807}48084809open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4810or die_error(500,"Open git-cat-file blob '$hash' failed");48114812# content-type (can include charset)4813$type= blob_contenttype($fd,$file_name,$type);48144815# "save as" filename, even when no $file_name is given4816my$save_as="$hash";4817if(defined$file_name) {4818$save_as=$file_name;4819}elsif($type=~m/^text\//) {4820$save_as.='.txt';4821}48224823# With XSS prevention on, blobs of all types except a few known safe4824# ones are served with "Content-Disposition: attachment" to make sure4825# they don't run in our security domain. For certain image types,4826# blob view writes an <img> tag referring to blob_plain view, and we4827# want to be sure not to break that by serving the image as an4828# attachment (though Firefox 3 doesn't seem to care).4829my$sandbox=$prevent_xss&&4830$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;48314832print$cgi->header(4833-type =>$type,4834-expires =>$expires,4835-content_disposition =>4836($sandbox?'attachment':'inline')4837.'; filename="'.$save_as.'"');4838local$/=undef;4839binmode STDOUT,':raw';4840print<$fd>;4841binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4842close$fd;4843}48444845sub git_blob {4846my$expires;48474848if(!defined$hash) {4849if(defined$file_name) {4850my$base=$hash_base|| git_get_head_hash($project);4851$hash= git_get_hash_by_path($base,$file_name,"blob")4852or die_error(404,"Cannot find file");4853}else{4854 die_error(400,"No file name defined");4855}4856}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4857# blobs defined by non-textual hash id's can be cached4858$expires="+1d";4859}48604861my$have_blame= gitweb_check_feature('blame');4862open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4863or die_error(500,"Couldn't cat$file_name,$hash");4864my$mimetype= blob_mimetype($fd,$file_name);4865if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4866close$fd;4867return git_blob_plain($mimetype);4868}4869# we can have blame only for text/* mimetype4870$have_blame&&= ($mimetype=~m!^text/!);48714872 git_header_html(undef,$expires);4873my$formats_nav='';4874if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4875if(defined$file_name) {4876if($have_blame) {4877$formats_nav.=4878$cgi->a({-href => href(action=>"blame", -replay=>1)},4879"blame") .4880" | ";4881}4882$formats_nav.=4883$cgi->a({-href => href(action=>"history", -replay=>1)},4884"history") .4885" | ".4886$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4887"raw") .4888" | ".4889$cgi->a({-href => href(action=>"blob",4890 hash_base=>"HEAD", file_name=>$file_name)},4891"HEAD");4892}else{4893$formats_nav.=4894$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4895"raw");4896}4897 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4898 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4899}else{4900print"<div class=\"page_nav\">\n".4901"<br/><br/></div>\n".4902"<div class=\"title\">$hash</div>\n";4903}4904 git_print_page_path($file_name,"blob",$hash_base);4905print"<div class=\"page_body\">\n";4906if($mimetype=~m!^image/!) {4907print qq!<img type="$mimetype"!;4908if($file_name) {4909print qq! alt="$file_name" title="$file_name"!;4910}4911print qq! src="! .4912 href(action=>"blob_plain", hash=>$hash,4913 hash_base=>$hash_base, file_name=>$file_name) .4914 qq!"/>\n!;4915}else{4916my$nr;4917while(my$line= <$fd>) {4918chomp$line;4919$nr++;4920$line= untabify($line);4921printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4922$nr,$nr,$nr, esc_html($line, -nbsp=>1);4923}4924}4925close$fd4926or print"Reading blob failed.\n";4927print"</div>";4928 git_footer_html();4929}49304931sub git_tree {4932if(!defined$hash_base) {4933$hash_base="HEAD";4934}4935if(!defined$hash) {4936if(defined$file_name) {4937$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4938}else{4939$hash=$hash_base;4940}4941}4942 die_error(404,"No such tree")unlessdefined($hash);49434944my@entries= ();4945{4946local$/="\0";4947open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4948or die_error(500,"Open git-ls-tree failed");4949@entries=map{chomp;$_} <$fd>;4950close$fd4951or die_error(404,"Reading tree failed");4952}49534954my$refs= git_get_references();4955my$ref= format_ref_marker($refs,$hash_base);4956 git_header_html();4957my$basedir='';4958my$have_blame= gitweb_check_feature('blame');4959if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4960my@views_nav= ();4961if(defined$file_name) {4962push@views_nav,4963$cgi->a({-href => href(action=>"history", -replay=>1)},4964"history"),4965$cgi->a({-href => href(action=>"tree",4966 hash_base=>"HEAD", file_name=>$file_name)},4967"HEAD"),4968}4969my$snapshot_links= format_snapshot_links($hash);4970if(defined$snapshot_links) {4971# FIXME: Should be available when we have no hash base as well.4972push@views_nav,$snapshot_links;4973}4974 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4975 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4976}else{4977undef$hash_base;4978print"<div class=\"page_nav\">\n";4979print"<br/><br/></div>\n";4980print"<div class=\"title\">$hash</div>\n";4981}4982if(defined$file_name) {4983$basedir=$file_name;4984if($basedirne''&&substr($basedir, -1)ne'/') {4985$basedir.='/';4986}4987 git_print_page_path($file_name,'tree',$hash_base);4988}4989print"<div class=\"page_body\">\n";4990print"<table class=\"tree\">\n";4991my$alternate=1;4992# '..' (top directory) link if possible4993if(defined$hash_base&&4994defined$file_name&&$file_name=~m![^/]+$!) {4995if($alternate) {4996print"<tr class=\"dark\">\n";4997}else{4998print"<tr class=\"light\">\n";4999}5000$alternate^=1;50015002my$up=$file_name;5003$up=~s!/?[^/]+$!!;5004undef$upunless$up;5005# based on git_print_tree_entry5006print'<td class="mode">'. mode_str('040000') ."</td>\n";5007print'<td class="list">';5008print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,5009 file_name=>$up)},5010"..");5011print"</td>\n";5012print"<td class=\"link\"></td>\n";50135014print"</tr>\n";5015}5016foreachmy$line(@entries) {5017my%t= parse_ls_tree_line($line, -z =>1);50185019if($alternate) {5020print"<tr class=\"dark\">\n";5021}else{5022print"<tr class=\"light\">\n";5023}5024$alternate^=1;50255026 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);50275028print"</tr>\n";5029}5030print"</table>\n".5031"</div>";5032 git_footer_html();5033}50345035sub git_snapshot {5036my$format=$input_params{'snapshot_format'};5037if(!@snapshot_fmts) {5038 die_error(403,"Snapshots not allowed");5039}5040# default to first supported snapshot format5041$format||=$snapshot_fmts[0];5042if($format!~m/^[a-z0-9]+$/) {5043 die_error(400,"Invalid snapshot format parameter");5044}elsif(!exists($known_snapshot_formats{$format})) {5045 die_error(400,"Unknown snapshot format");5046}elsif(!grep($_eq$format,@snapshot_fmts)) {5047 die_error(403,"Unsupported snapshot format");5048}50495050if(!defined$hash) {5051$hash= git_get_head_hash($project);5052}50535054my$name=$project;5055$name=~ s,([^/])/*\.git$,$1,;5056$name= basename($name);5057my$filename= to_utf8($name);5058$name=~s/\047/\047\\\047\047/g;5059my$cmd;5060$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5061$cmd= quote_command(5062 git_cmd(),'archive',5063"--format=$known_snapshot_formats{$format}{'format'}",5064"--prefix=$name/",$hash);5065if(exists$known_snapshot_formats{$format}{'compressor'}) {5066$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5067}50685069print$cgi->header(5070-type =>$known_snapshot_formats{$format}{'type'},5071-content_disposition =>'inline; filename="'."$filename".'"',5072-status =>'200 OK');50735074open my$fd,"-|",$cmd5075or die_error(500,"Execute git-archive failed");5076binmode STDOUT,':raw';5077print<$fd>;5078binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5079close$fd;5080}50815082sub git_log {5083my$head= git_get_head_hash($project);5084if(!defined$hash) {5085$hash=$head;5086}5087if(!defined$page) {5088$page=0;5089}5090my$refs= git_get_references();50915092my@commitlist= parse_commits($hash,101, (100*$page));50935094my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);50955096my($patch_max) = gitweb_get_feature('patches');5097if($patch_max) {5098if($patch_max<0||@commitlist<=$patch_max) {5099$paging_nav.=" ⋅ ".5100$cgi->a({-href => href(action=>"patches", -replay=>1)},5101"patches");5102}5103}51045105 git_header_html();5106 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);51075108if(!@commitlist) {5109my%co= parse_commit($hash);51105111 git_print_header_div('summary',$project);5112print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5113}5114my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5115for(my$i=0;$i<=$to;$i++) {5116my%co= %{$commitlist[$i]};5117next if!%co;5118my$commit=$co{'id'};5119my$ref= format_ref_marker($refs,$commit);5120my%ad= parse_date($co{'author_epoch'});5121 git_print_header_div('commit',5122"<span class=\"age\">$co{'age_string'}</span>".5123 esc_html($co{'title'}) .$ref,5124$commit);5125print"<div class=\"title_text\">\n".5126"<div class=\"log_link\">\n".5127$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5128" | ".5129$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5130" | ".5131$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5132"<br/>\n".5133"</div>\n";5134 git_print_authorship(\%co, -tag =>'span');5135print"<br/>\n</div>\n";51365137print"<div class=\"log_body\">\n";5138 git_print_log($co{'comment'}, -final_empty_line=>1);5139print"</div>\n";5140}5141if($#commitlist>=100) {5142print"<div class=\"page_nav\">\n";5143print$cgi->a({-href => href(-replay=>1, page=>$page+1),5144-accesskey =>"n", -title =>"Alt-n"},"next");5145print"</div>\n";5146}5147 git_footer_html();5148}51495150sub git_commit {5151$hash||=$hash_base||"HEAD";5152my%co= parse_commit($hash)5153or die_error(404,"Unknown commit object");51545155my$parent=$co{'parent'};5156my$parents=$co{'parents'};# listref51575158# we need to prepare $formats_nav before any parameter munging5159my$formats_nav;5160if(!defined$parent) {5161# --root commitdiff5162$formats_nav.='(initial)';5163}elsif(@$parents==1) {5164# single parent commit5165$formats_nav.=5166'(parent: '.5167$cgi->a({-href => href(action=>"commit",5168 hash=>$parent)},5169 esc_html(substr($parent,0,7))) .5170')';5171}else{5172# merge commit5173$formats_nav.=5174'(merge: '.5175join(' ',map{5176$cgi->a({-href => href(action=>"commit",5177 hash=>$_)},5178 esc_html(substr($_,0,7)));5179}@$parents) .5180')';5181}5182if(gitweb_check_feature('patches')) {5183$formats_nav.=" | ".5184$cgi->a({-href => href(action=>"patch", -replay=>1)},5185"patch");5186}51875188if(!defined$parent) {5189$parent="--root";5190}5191my@difftree;5192open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5193@diff_opts,5194(@$parents<=1?$parent:'-c'),5195$hash,"--"5196or die_error(500,"Open git-diff-tree failed");5197@difftree=map{chomp;$_} <$fd>;5198close$fdor die_error(404,"Reading git-diff-tree failed");51995200# non-textual hash id's can be cached5201my$expires;5202if($hash=~m/^[0-9a-fA-F]{40}$/) {5203$expires="+1d";5204}5205my$refs= git_get_references();5206my$ref= format_ref_marker($refs,$co{'id'});52075208 git_header_html(undef,$expires);5209 git_print_page_nav('commit','',5210$hash,$co{'tree'},$hash,5211$formats_nav);52125213if(defined$co{'parent'}) {5214 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5215}else{5216 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5217}5218print"<div class=\"title_text\">\n".5219"<table class=\"object_header\">\n";5220 git_print_authorship_rows(\%co);5221print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5222print"<tr>".5223"<td>tree</td>".5224"<td class=\"sha1\">".5225$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5226class=>"list"},$co{'tree'}) .5227"</td>".5228"<td class=\"link\">".5229$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5230"tree");5231my$snapshot_links= format_snapshot_links($hash);5232if(defined$snapshot_links) {5233print" | ".$snapshot_links;5234}5235print"</td>".5236"</tr>\n";52375238foreachmy$par(@$parents) {5239print"<tr>".5240"<td>parent</td>".5241"<td class=\"sha1\">".5242$cgi->a({-href => href(action=>"commit", hash=>$par),5243class=>"list"},$par) .5244"</td>".5245"<td class=\"link\">".5246$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5247" | ".5248$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5249"</td>".5250"</tr>\n";5251}5252print"</table>".5253"</div>\n";52545255print"<div class=\"page_body\">\n";5256 git_print_log($co{'comment'});5257print"</div>\n";52585259 git_difftree_body(\@difftree,$hash,@$parents);52605261 git_footer_html();5262}52635264sub git_object {5265# object is defined by:5266# - hash or hash_base alone5267# - hash_base and file_name5268my$type;52695270# - hash or hash_base alone5271if($hash|| ($hash_base&& !defined$file_name)) {5272my$object_id=$hash||$hash_base;52735274open my$fd,"-|", quote_command(5275 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5276or die_error(404,"Object does not exist");5277$type= <$fd>;5278chomp$type;5279close$fd5280or die_error(404,"Object does not exist");52815282# - hash_base and file_name5283}elsif($hash_base&&defined$file_name) {5284$file_name=~ s,/+$,,;52855286system(git_cmd(),"cat-file",'-e',$hash_base) ==05287or die_error(404,"Base object does not exist");52885289# here errors should not hapen5290open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5291or die_error(500,"Open git-ls-tree failed");5292my$line= <$fd>;5293close$fd;52945295#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5296unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5297 die_error(404,"File or directory for given base does not exist");5298}5299$type=$2;5300$hash=$3;5301}else{5302 die_error(400,"Not enough information to find object");5303}53045305print$cgi->redirect(-uri => href(action=>$type, -full=>1,5306 hash=>$hash, hash_base=>$hash_base,5307 file_name=>$file_name),5308-status =>'302 Found');5309}53105311sub git_blobdiff {5312my$format=shift||'html';53135314my$fd;5315my@difftree;5316my%diffinfo;5317my$expires;53185319# preparing $fd and %diffinfo for git_patchset_body5320# new style URI5321if(defined$hash_base&&defined$hash_parent_base) {5322if(defined$file_name) {5323# read raw output5324open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5325$hash_parent_base,$hash_base,5326"--", (defined$file_parent?$file_parent: ()),$file_name5327or die_error(500,"Open git-diff-tree failed");5328@difftree=map{chomp;$_} <$fd>;5329close$fd5330or die_error(404,"Reading git-diff-tree failed");5331@difftree5332or die_error(404,"Blob diff not found");53335334}elsif(defined$hash&&5335$hash=~/[0-9a-fA-F]{40}/) {5336# try to find filename from $hash53375338# read filtered raw output5339open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5340$hash_parent_base,$hash_base,"--"5341or die_error(500,"Open git-diff-tree failed");5342@difftree=5343# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5344# $hash == to_id5345grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5346map{chomp;$_} <$fd>;5347close$fd5348or die_error(404,"Reading git-diff-tree failed");5349@difftree5350or die_error(404,"Blob diff not found");53515352}else{5353 die_error(400,"Missing one of the blob diff parameters");5354}53555356if(@difftree>1) {5357 die_error(400,"Ambiguous blob diff specification");5358}53595360%diffinfo= parse_difftree_raw_line($difftree[0]);5361$file_parent||=$diffinfo{'from_file'} ||$file_name;5362$file_name||=$diffinfo{'to_file'};53635364$hash_parent||=$diffinfo{'from_id'};5365$hash||=$diffinfo{'to_id'};53665367# non-textual hash id's can be cached5368if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5369$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5370$expires='+1d';5371}53725373# open patch output5374open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5375'-p', ($formateq'html'?"--full-index": ()),5376$hash_parent_base,$hash_base,5377"--", (defined$file_parent?$file_parent: ()),$file_name5378or die_error(500,"Open git-diff-tree failed");5379}53805381# old/legacy style URI -- not generated anymore since 1.4.3.5382if(!%diffinfo) {5383 die_error('404 Not Found',"Missing one of the blob diff parameters")5384}53855386# header5387if($formateq'html') {5388my$formats_nav=5389$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5390"raw");5391 git_header_html(undef,$expires);5392if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5393 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5394 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5395}else{5396print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5397print"<div class=\"title\">$hashvs$hash_parent</div>\n";5398}5399if(defined$file_name) {5400 git_print_page_path($file_name,"blob",$hash_base);5401}else{5402print"<div class=\"page_path\"></div>\n";5403}54045405}elsif($formateq'plain') {5406print$cgi->header(5407-type =>'text/plain',5408-charset =>'utf-8',5409-expires =>$expires,5410-content_disposition =>'inline; filename="'."$file_name".'.patch"');54115412print"X-Git-Url: ".$cgi->self_url() ."\n\n";54135414}else{5415 die_error(400,"Unknown blobdiff format");5416}54175418# patch5419if($formateq'html') {5420print"<div class=\"page_body\">\n";54215422 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5423close$fd;54245425print"</div>\n";# class="page_body"5426 git_footer_html();54275428}else{5429while(my$line= <$fd>) {5430$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5431$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;54325433print$line;54345435last if$line=~m!^\+\+\+!;5436}5437local$/=undef;5438print<$fd>;5439close$fd;5440}5441}54425443sub git_blobdiff_plain {5444 git_blobdiff('plain');5445}54465447sub git_commitdiff {5448my%params=@_;5449my$format=$params{-format} ||'html';54505451my($patch_max) = gitweb_get_feature('patches');5452if($formateq'patch') {5453 die_error(403,"Patch view not allowed")unless$patch_max;5454}54555456$hash||=$hash_base||"HEAD";5457my%co= parse_commit($hash)5458or die_error(404,"Unknown commit object");54595460# choose format for commitdiff for merge5461if(!defined$hash_parent&& @{$co{'parents'}} >1) {5462$hash_parent='--cc';5463}5464# we need to prepare $formats_nav before almost any parameter munging5465my$formats_nav;5466if($formateq'html') {5467$formats_nav=5468$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5469"raw");5470if($patch_max) {5471$formats_nav.=" | ".5472$cgi->a({-href => href(action=>"patch", -replay=>1)},5473"patch");5474}54755476if(defined$hash_parent&&5477$hash_parentne'-c'&&$hash_parentne'--cc') {5478# commitdiff with two commits given5479my$hash_parent_short=$hash_parent;5480if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5481$hash_parent_short=substr($hash_parent,0,7);5482}5483$formats_nav.=5484' (from';5485for(my$i=0;$i< @{$co{'parents'}};$i++) {5486if($co{'parents'}[$i]eq$hash_parent) {5487$formats_nav.=' parent '. ($i+1);5488last;5489}5490}5491$formats_nav.=': '.5492$cgi->a({-href => href(action=>"commitdiff",5493 hash=>$hash_parent)},5494 esc_html($hash_parent_short)) .5495')';5496}elsif(!$co{'parent'}) {5497# --root commitdiff5498$formats_nav.=' (initial)';5499}elsif(scalar@{$co{'parents'}} ==1) {5500# single parent commit5501$formats_nav.=5502' (parent: '.5503$cgi->a({-href => href(action=>"commitdiff",5504 hash=>$co{'parent'})},5505 esc_html(substr($co{'parent'},0,7))) .5506')';5507}else{5508# merge commit5509if($hash_parenteq'--cc') {5510$formats_nav.=' | '.5511$cgi->a({-href => href(action=>"commitdiff",5512 hash=>$hash, hash_parent=>'-c')},5513'combined');5514}else{# $hash_parent eq '-c'5515$formats_nav.=' | '.5516$cgi->a({-href => href(action=>"commitdiff",5517 hash=>$hash, hash_parent=>'--cc')},5518'compact');5519}5520$formats_nav.=5521' (merge: '.5522join(' ',map{5523$cgi->a({-href => href(action=>"commitdiff",5524 hash=>$_)},5525 esc_html(substr($_,0,7)));5526} @{$co{'parents'}} ) .5527')';5528}5529}55305531my$hash_parent_param=$hash_parent;5532if(!defined$hash_parent_param) {5533# --cc for multiple parents, --root for parentless5534$hash_parent_param=5535@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5536}55375538# read commitdiff5539my$fd;5540my@difftree;5541if($formateq'html') {5542open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5543"--no-commit-id","--patch-with-raw","--full-index",5544$hash_parent_param,$hash,"--"5545or die_error(500,"Open git-diff-tree failed");55465547while(my$line= <$fd>) {5548chomp$line;5549# empty line ends raw part of diff-tree output5550last unless$line;5551push@difftree,scalar parse_difftree_raw_line($line);5552}55535554}elsif($formateq'plain') {5555open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5556'-p',$hash_parent_param,$hash,"--"5557or die_error(500,"Open git-diff-tree failed");5558}elsif($formateq'patch') {5559# For commit ranges, we limit the output to the number of5560# patches specified in the 'patches' feature.5561# For single commits, we limit the output to a single patch,5562# diverging from the git-format-patch default.5563my@commit_spec= ();5564if($hash_parent) {5565if($patch_max>0) {5566push@commit_spec,"-$patch_max";5567}5568push@commit_spec,'-n',"$hash_parent..$hash";5569}else{5570if($params{-single}) {5571push@commit_spec,'-1';5572}else{5573if($patch_max>0) {5574push@commit_spec,"-$patch_max";5575}5576push@commit_spec,"-n";5577}5578push@commit_spec,'--root',$hash;5579}5580open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5581'--stdout',@commit_spec5582or die_error(500,"Open git-format-patch failed");5583}else{5584 die_error(400,"Unknown commitdiff format");5585}55865587# non-textual hash id's can be cached5588my$expires;5589if($hash=~m/^[0-9a-fA-F]{40}$/) {5590$expires="+1d";5591}55925593# write commit message5594if($formateq'html') {5595my$refs= git_get_references();5596my$ref= format_ref_marker($refs,$co{'id'});55975598 git_header_html(undef,$expires);5599 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5600 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5601print"<div class=\"title_text\">\n".5602"<table class=\"object_header\">\n";5603 git_print_authorship_rows(\%co);5604print"</table>".5605"</div>\n";5606print"<div class=\"page_body\">\n";5607if(@{$co{'comment'}} >1) {5608print"<div class=\"log\">\n";5609 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5610print"</div>\n";# class="log"5611}56125613}elsif($formateq'plain') {5614my$refs= git_get_references("tags");5615my$tagname= git_get_rev_name_tags($hash);5616my$filename= basename($project) ."-$hash.patch";56175618print$cgi->header(5619-type =>'text/plain',5620-charset =>'utf-8',5621-expires =>$expires,5622-content_disposition =>'inline; filename="'."$filename".'"');5623my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5624print"From: ". to_utf8($co{'author'}) ."\n";5625print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5626print"Subject: ". to_utf8($co{'title'}) ."\n";56275628print"X-Git-Tag:$tagname\n"if$tagname;5629print"X-Git-Url: ".$cgi->self_url() ."\n\n";56305631foreachmy$line(@{$co{'comment'}}) {5632print to_utf8($line) ."\n";5633}5634print"---\n\n";5635}elsif($formateq'patch') {5636my$filename= basename($project) ."-$hash.patch";56375638print$cgi->header(5639-type =>'text/plain',5640-charset =>'utf-8',5641-expires =>$expires,5642-content_disposition =>'inline; filename="'."$filename".'"');5643}56445645# write patch5646if($formateq'html') {5647my$use_parents= !defined$hash_parent||5648$hash_parenteq'-c'||$hash_parenteq'--cc';5649 git_difftree_body(\@difftree,$hash,5650$use_parents? @{$co{'parents'}} :$hash_parent);5651print"<br/>\n";56525653 git_patchset_body($fd, \@difftree,$hash,5654$use_parents? @{$co{'parents'}} :$hash_parent);5655close$fd;5656print"</div>\n";# class="page_body"5657 git_footer_html();56585659}elsif($formateq'plain') {5660local$/=undef;5661print<$fd>;5662close$fd5663or print"Reading git-diff-tree failed\n";5664}elsif($formateq'patch') {5665local$/=undef;5666print<$fd>;5667close$fd5668or print"Reading git-format-patch failed\n";5669}5670}56715672sub git_commitdiff_plain {5673 git_commitdiff(-format =>'plain');5674}56755676# format-patch-style patches5677sub git_patch {5678 git_commitdiff(-format =>'patch', -single=>1);5679}56805681sub git_patches {5682 git_commitdiff(-format =>'patch');5683}56845685sub git_history {5686if(!defined$hash_base) {5687$hash_base= git_get_head_hash($project);5688}5689if(!defined$page) {5690$page=0;5691}5692my$ftype;5693my%co= parse_commit($hash_base)5694or die_error(404,"Unknown commit object");56955696my$refs= git_get_references();5697my$limit=sprintf("--max-count=%i", (100* ($page+1)));56985699my@commitlist= parse_commits($hash_base,101, (100*$page),5700$file_name,"--full-history")5701or die_error(404,"No such file or directory on given branch");57025703if(!defined$hash&&defined$file_name) {5704# some commits could have deleted file in question,5705# and not have it in tree, but one of them has to have it5706for(my$i=0;$i<=@commitlist;$i++) {5707$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5708last ifdefined$hash;5709}5710}5711if(defined$hash) {5712$ftype= git_get_type($hash);5713}5714if(!defined$ftype) {5715 die_error(500,"Unknown type of object");5716}57175718my$paging_nav='';5719if($page>0) {5720$paging_nav.=5721$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5722 file_name=>$file_name)},5723"first");5724$paging_nav.=" ⋅ ".5725$cgi->a({-href => href(-replay=>1, page=>$page-1),5726-accesskey =>"p", -title =>"Alt-p"},"prev");5727}else{5728$paging_nav.="first";5729$paging_nav.=" ⋅ prev";5730}5731my$next_link='';5732if($#commitlist>=100) {5733$next_link=5734$cgi->a({-href => href(-replay=>1, page=>$page+1),5735-accesskey =>"n", -title =>"Alt-n"},"next");5736$paging_nav.=" ⋅$next_link";5737}else{5738$paging_nav.=" ⋅ next";5739}57405741 git_header_html();5742 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5743 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5744 git_print_page_path($file_name,$ftype,$hash_base);57455746 git_history_body(\@commitlist,0,99,5747$refs,$hash_base,$ftype,$next_link);57485749 git_footer_html();5750}57515752sub git_search {5753 gitweb_check_feature('search')or die_error(403,"Search is disabled");5754if(!defined$searchtext) {5755 die_error(400,"Text field is empty");5756}5757if(!defined$hash) {5758$hash= git_get_head_hash($project);5759}5760my%co= parse_commit($hash);5761if(!%co) {5762 die_error(404,"Unknown commit object");5763}5764if(!defined$page) {5765$page=0;5766}57675768$searchtype||='commit';5769if($searchtypeeq'pickaxe') {5770# pickaxe may take all resources of your box and run for several minutes5771# with every query - so decide by yourself how public you make this feature5772 gitweb_check_feature('pickaxe')5773or die_error(403,"Pickaxe is disabled");5774}5775if($searchtypeeq'grep') {5776 gitweb_check_feature('grep')5777or die_error(403,"Grep is disabled");5778}57795780 git_header_html();57815782if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5783my$greptype;5784if($searchtypeeq'commit') {5785$greptype="--grep=";5786}elsif($searchtypeeq'author') {5787$greptype="--author=";5788}elsif($searchtypeeq'committer') {5789$greptype="--committer=";5790}5791$greptype.=$searchtext;5792my@commitlist= parse_commits($hash,101, (100*$page),undef,5793$greptype,'--regexp-ignore-case',5794$search_use_regexp?'--extended-regexp':'--fixed-strings');57955796my$paging_nav='';5797if($page>0) {5798$paging_nav.=5799$cgi->a({-href => href(action=>"search", hash=>$hash,5800 searchtext=>$searchtext,5801 searchtype=>$searchtype)},5802"first");5803$paging_nav.=" ⋅ ".5804$cgi->a({-href => href(-replay=>1, page=>$page-1),5805-accesskey =>"p", -title =>"Alt-p"},"prev");5806}else{5807$paging_nav.="first";5808$paging_nav.=" ⋅ prev";5809}5810my$next_link='';5811if($#commitlist>=100) {5812$next_link=5813$cgi->a({-href => href(-replay=>1, page=>$page+1),5814-accesskey =>"n", -title =>"Alt-n"},"next");5815$paging_nav.=" ⋅$next_link";5816}else{5817$paging_nav.=" ⋅ next";5818}58195820if($#commitlist>=100) {5821}58225823 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5824 git_print_header_div('commit', esc_html($co{'title'}),$hash);5825 git_search_grep_body(\@commitlist,0,99,$next_link);5826}58275828if($searchtypeeq'pickaxe') {5829 git_print_page_nav('','',$hash,$co{'tree'},$hash);5830 git_print_header_div('commit', esc_html($co{'title'}),$hash);58315832print"<table class=\"pickaxe search\">\n";5833my$alternate=1;5834local$/="\n";5835open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5836'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5837($search_use_regexp?'--pickaxe-regex': ());5838undef%co;5839my@files;5840while(my$line= <$fd>) {5841chomp$line;5842next unless$line;58435844my%set= parse_difftree_raw_line($line);5845if(defined$set{'commit'}) {5846# finish previous commit5847if(%co) {5848print"</td>\n".5849"<td class=\"link\">".5850$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5851" | ".5852$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5853print"</td>\n".5854"</tr>\n";5855}58565857if($alternate) {5858print"<tr class=\"dark\">\n";5859}else{5860print"<tr class=\"light\">\n";5861}5862$alternate^=1;5863%co= parse_commit($set{'commit'});5864my$author= chop_and_escape_str($co{'author_name'},15,5);5865print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5866"<td><i>$author</i></td>\n".5867"<td>".5868$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5869-class=>"list subject"},5870 chop_and_escape_str($co{'title'},50) ."<br/>");5871}elsif(defined$set{'to_id'}) {5872next if($set{'to_id'} =~m/^0{40}$/);58735874print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5875 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5876-class=>"list"},5877"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5878"<br/>\n";5879}5880}5881close$fd;58825883# finish last commit (warning: repetition!)5884if(%co) {5885print"</td>\n".5886"<td class=\"link\">".5887$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5888" | ".5889$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5890print"</td>\n".5891"</tr>\n";5892}58935894print"</table>\n";5895}58965897if($searchtypeeq'grep') {5898 git_print_page_nav('','',$hash,$co{'tree'},$hash);5899 git_print_header_div('commit', esc_html($co{'title'}),$hash);59005901print"<table class=\"grep_search\">\n";5902my$alternate=1;5903my$matches=0;5904local$/="\n";5905open my$fd,"-|", git_cmd(),'grep','-n',5906$search_use_regexp? ('-E','-i') :'-F',5907$searchtext,$co{'tree'};5908my$lastfile='';5909while(my$line= <$fd>) {5910chomp$line;5911my($file,$lno,$ltext,$binary);5912last if($matches++>1000);5913if($line=~/^Binary file (.+) matches$/) {5914$file=$1;5915$binary=1;5916}else{5917(undef,$file,$lno,$ltext) =split(/:/,$line,4);5918}5919if($filene$lastfile) {5920$lastfileand print"</td></tr>\n";5921if($alternate++) {5922print"<tr class=\"dark\">\n";5923}else{5924print"<tr class=\"light\">\n";5925}5926print"<td class=\"list\">".5927$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5928 file_name=>"$file"),5929-class=>"list"}, esc_path($file));5930print"</td><td>\n";5931$lastfile=$file;5932}5933if($binary) {5934print"<div class=\"binary\">Binary file</div>\n";5935}else{5936$ltext= untabify($ltext);5937if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5938$ltext= esc_html($1, -nbsp=>1);5939$ltext.='<span class="match">';5940$ltext.= esc_html($2, -nbsp=>1);5941$ltext.='</span>';5942$ltext.= esc_html($3, -nbsp=>1);5943}else{5944$ltext= esc_html($ltext, -nbsp=>1);5945}5946print"<div class=\"pre\">".5947$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5948 file_name=>"$file").'#l'.$lno,5949-class=>"linenr"},sprintf('%4i',$lno))5950.' '.$ltext."</div>\n";5951}5952}5953if($lastfile) {5954print"</td></tr>\n";5955if($matches>1000) {5956print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5957}5958}else{5959print"<div class=\"diff nodifferences\">No matches found</div>\n";5960}5961close$fd;59625963print"</table>\n";5964}5965 git_footer_html();5966}59675968sub git_search_help {5969 git_header_html();5970 git_print_page_nav('','',$hash,$hash,$hash);5971print<<EOT;5972<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5973regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5974the pattern entered is recognized as the POSIX extended5975<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5976insensitive).</p>5977<dl>5978<dt><b>commit</b></dt>5979<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5980EOT5981my$have_grep= gitweb_check_feature('grep');5982if($have_grep) {5983print<<EOT;5984<dt><b>grep</b></dt>5985<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5986 a different one) are searched for the given pattern. On large trees, this search can take5987a while and put some strain on the server, so please use it with some consideration. Note that5988due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5989case-sensitive.</dd>5990EOT5991}5992print<<EOT;5993<dt><b>author</b></dt>5994<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5995<dt><b>committer</b></dt>5996<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5997EOT5998my$have_pickaxe= gitweb_check_feature('pickaxe');5999if($have_pickaxe) {6000print<<EOT;6001<dt><b>pickaxe</b></dt>6002<dd>All commits that caused the string to appear or disappear from any file (changes that6003added, removed or "modified" the string) will be listed. This search can take a while and6004takes a lot of strain on the server, so please use it wisely. Note that since you may be6005interested even in changes just changing the case as well, this search is case sensitive.</dd>6006EOT6007}6008print"</dl>\n";6009 git_footer_html();6010}60116012sub git_shortlog {6013my$head= git_get_head_hash($project);6014if(!defined$hash) {6015$hash=$head;6016}6017if(!defined$page) {6018$page=0;6019}6020my$refs= git_get_references();60216022my$commit_hash=$hash;6023if(defined$hash_parent) {6024$commit_hash="$hash_parent..$hash";6025}6026my@commitlist= parse_commits($commit_hash,101, (100*$page));60276028my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6029my$next_link='';6030if($#commitlist>=100) {6031$next_link=6032$cgi->a({-href => href(-replay=>1, page=>$page+1),6033-accesskey =>"n", -title =>"Alt-n"},"next");6034}6035my$patch_max= gitweb_check_feature('patches');6036if($patch_max) {6037if($patch_max<0||@commitlist<=$patch_max) {6038$paging_nav.=" ⋅ ".6039$cgi->a({-href => href(action=>"patches", -replay=>1)},6040"patches");6041}6042}60436044 git_header_html();6045 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6046 git_print_header_div('summary',$project);60476048 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);60496050 git_footer_html();6051}60526053## ......................................................................6054## feeds (RSS, Atom; OPML)60556056sub git_feed {6057my$format=shift||'atom';6058my$have_blame= gitweb_check_feature('blame');60596060# Atom: http://www.atomenabled.org/developers/syndication/6061# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6062if($formatne'rss'&&$formatne'atom') {6063 die_error(400,"Unknown web feed format");6064}60656066# log/feed of current (HEAD) branch, log of given branch, history of file/directory6067my$head=$hash||'HEAD';6068my@commitlist= parse_commits($head,150,0,$file_name);60696070my%latest_commit;6071my%latest_date;6072my$content_type="application/$format+xml";6073if(defined$cgi->http('HTTP_ACCEPT') &&6074$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6075# browser (feed reader) prefers text/xml6076$content_type='text/xml';6077}6078if(defined($commitlist[0])) {6079%latest_commit= %{$commitlist[0]};6080my$latest_epoch=$latest_commit{'committer_epoch'};6081%latest_date= parse_date($latest_epoch);6082my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6083if(defined$if_modified) {6084my$since;6085if(eval{require HTTP::Date;1; }) {6086$since= HTTP::Date::str2time($if_modified);6087}elsif(eval{require Time::ParseDate;1; }) {6088$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6089}6090if(defined$since&&$latest_epoch<=$since) {6091print$cgi->header(6092-type =>$content_type,6093-charset =>'utf-8',6094-last_modified =>$latest_date{'rfc2822'},6095-status =>'304 Not Modified');6096return;6097}6098}6099print$cgi->header(6100-type =>$content_type,6101-charset =>'utf-8',6102-last_modified =>$latest_date{'rfc2822'});6103}else{6104print$cgi->header(6105-type =>$content_type,6106-charset =>'utf-8');6107}61086109# Optimization: skip generating the body if client asks only6110# for Last-Modified date.6111return if($cgi->request_method()eq'HEAD');61126113# header variables6114my$title="$site_name-$project/$action";6115my$feed_type='log';6116if(defined$hash) {6117$title.=" - '$hash'";6118$feed_type='branch log';6119if(defined$file_name) {6120$title.=" ::$file_name";6121$feed_type='history';6122}6123}elsif(defined$file_name) {6124$title.=" -$file_name";6125$feed_type='history';6126}6127$title.="$feed_type";6128my$descr= git_get_project_description($project);6129if(defined$descr) {6130$descr= esc_html($descr);6131}else{6132$descr="$project".6133($formateq'rss'?'RSS':'Atom') .6134" feed";6135}6136my$owner= git_get_project_owner($project);6137$owner= esc_html($owner);61386139#header6140my$alt_url;6141if(defined$file_name) {6142$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6143}elsif(defined$hash) {6144$alt_url= href(-full=>1, action=>"log", hash=>$hash);6145}else{6146$alt_url= href(-full=>1, action=>"summary");6147}6148print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6149if($formateq'rss') {6150print<<XML;6151<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6152<channel>6153XML6154print"<title>$title</title>\n".6155"<link>$alt_url</link>\n".6156"<description>$descr</description>\n".6157"<language>en</language>\n".6158# project owner is responsible for 'editorial' content6159"<managingEditor>$owner</managingEditor>\n";6160if(defined$logo||defined$favicon) {6161# prefer the logo to the favicon, since RSS6162# doesn't allow both6163my$img= esc_url($logo||$favicon);6164print"<image>\n".6165"<url>$img</url>\n".6166"<title>$title</title>\n".6167"<link>$alt_url</link>\n".6168"</image>\n";6169}6170if(%latest_date) {6171print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6172print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6173}6174print"<generator>gitweb v.$version/$git_version</generator>\n";6175}elsif($formateq'atom') {6176print<<XML;6177<feed xmlns="http://www.w3.org/2005/Atom">6178XML6179print"<title>$title</title>\n".6180"<subtitle>$descr</subtitle>\n".6181'<link rel="alternate" type="text/html" href="'.6182$alt_url.'" />'."\n".6183'<link rel="self" type="'.$content_type.'" href="'.6184$cgi->self_url() .'" />'."\n".6185"<id>". href(-full=>1) ."</id>\n".6186# use project owner for feed author6187"<author><name>$owner</name></author>\n";6188if(defined$favicon) {6189print"<icon>". esc_url($favicon) ."</icon>\n";6190}6191if(defined$logo_url) {6192# not twice as wide as tall: 72 x 27 pixels6193print"<logo>". esc_url($logo) ."</logo>\n";6194}6195if(!%latest_date) {6196# dummy date to keep the feed valid until commits trickle in:6197print"<updated>1970-01-01T00:00:00Z</updated>\n";6198}else{6199print"<updated>$latest_date{'iso-8601'}</updated>\n";6200}6201print"<generator version='$version/$git_version'>gitweb</generator>\n";6202}62036204# contents6205for(my$i=0;$i<=$#commitlist;$i++) {6206my%co= %{$commitlist[$i]};6207my$commit=$co{'id'};6208# we read 150, we always show 30 and the ones more recent than 48 hours6209if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6210last;6211}6212my%cd= parse_date($co{'author_epoch'});62136214# get list of changed files6215open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6216$co{'parent'} ||"--root",6217$co{'id'},"--", (defined$file_name?$file_name: ())6218ornext;6219my@difftree=map{chomp;$_} <$fd>;6220close$fd6221ornext;62226223# print element (entry, item)6224my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6225if($formateq'rss') {6226print"<item>\n".6227"<title>". esc_html($co{'title'}) ."</title>\n".6228"<author>". esc_html($co{'author'}) ."</author>\n".6229"<pubDate>$cd{'rfc2822'}</pubDate>\n".6230"<guid isPermaLink=\"true\">$co_url</guid>\n".6231"<link>$co_url</link>\n".6232"<description>". esc_html($co{'title'}) ."</description>\n".6233"<content:encoded>".6234"<![CDATA[\n";6235}elsif($formateq'atom') {6236print"<entry>\n".6237"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6238"<updated>$cd{'iso-8601'}</updated>\n".6239"<author>\n".6240" <name>". esc_html($co{'author_name'}) ."</name>\n";6241if($co{'author_email'}) {6242print" <email>". esc_html($co{'author_email'}) ."</email>\n";6243}6244print"</author>\n".6245# use committer for contributor6246"<contributor>\n".6247" <name>". esc_html($co{'committer_name'}) ."</name>\n";6248if($co{'committer_email'}) {6249print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6250}6251print"</contributor>\n".6252"<published>$cd{'iso-8601'}</published>\n".6253"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6254"<id>$co_url</id>\n".6255"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6256"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6257}6258my$comment=$co{'comment'};6259print"<pre>\n";6260foreachmy$line(@$comment) {6261$line= esc_html($line);6262print"$line\n";6263}6264print"</pre><ul>\n";6265foreachmy$difftree_line(@difftree) {6266my%difftree= parse_difftree_raw_line($difftree_line);6267next if!$difftree{'from_id'};62686269my$file=$difftree{'file'} ||$difftree{'to_file'};62706271print"<li>".6272"[".6273$cgi->a({-href => href(-full=>1, action=>"blobdiff",6274 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6275 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6276 file_name=>$file, file_parent=>$difftree{'from_file'}),6277-title =>"diff"},'D');6278if($have_blame) {6279print$cgi->a({-href => href(-full=>1, action=>"blame",6280 file_name=>$file, hash_base=>$commit),6281-title =>"blame"},'B');6282}6283# if this is not a feed of a file history6284if(!defined$file_name||$file_namene$file) {6285print$cgi->a({-href => href(-full=>1, action=>"history",6286 file_name=>$file, hash=>$commit),6287-title =>"history"},'H');6288}6289$file= esc_path($file);6290print"] ".6291"$file</li>\n";6292}6293if($formateq'rss') {6294print"</ul>]]>\n".6295"</content:encoded>\n".6296"</item>\n";6297}elsif($formateq'atom') {6298print"</ul>\n</div>\n".6299"</content>\n".6300"</entry>\n";6301}6302}63036304# end of feed6305if($formateq'rss') {6306print"</channel>\n</rss>\n";6307}elsif($formateq'atom') {6308print"</feed>\n";6309}6310}63116312sub git_rss {6313 git_feed('rss');6314}63156316sub git_atom {6317 git_feed('atom');6318}63196320sub git_opml {6321my@list= git_get_projects_list();63226323print$cgi->header(6324-type =>'text/xml',6325-charset =>'utf-8',6326-content_disposition =>'inline; filename="opml.xml"');63276328print<<XML;6329<?xml version="1.0" encoding="utf-8"?>6330<opml version="1.0">6331<head>6332 <title>$site_nameOPML Export</title>6333</head>6334<body>6335<outline text="git RSS feeds">6336XML63376338foreachmy$pr(@list) {6339my%proj=%$pr;6340my$head= git_get_head_hash($proj{'path'});6341if(!defined$head) {6342next;6343}6344$git_dir="$projectroot/$proj{'path'}";6345my%co= parse_commit($head);6346if(!%co) {6347next;6348}63496350my$path= esc_html(chop_str($proj{'path'},25,5));6351my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6352my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6353print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6354}6355print<<XML;6356</outline>6357</body>6358</opml>6359XML6360}