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# if we're called with PATH_INFO, we have to strip that 31# from the URL to find our real URL 32# we make $path_info global because it's also used later on 33my$path_info=$ENV{"PATH_INFO"}; 34if($path_info) { 35$my_url=~ s,\Q$path_info\E$,,; 36$my_uri=~ s,\Q$path_info\E$,,; 37} 38 39# core git executable to use 40# this can just be "git" if your webserver has a sensible PATH 41our$GIT="++GIT_BINDIR++/git"; 42 43# absolute fs-path which will be prepended to the project path 44#our $projectroot = "/pub/scm"; 45our$projectroot="++GITWEB_PROJECTROOT++"; 46 47# fs traversing limit for getting project list 48# the number is relative to the projectroot 49our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 50 51# target of the home link on top of all pages 52our$home_link=$my_uri||"/"; 53 54# string of the home link on top of all pages 55our$home_link_str="++GITWEB_HOME_LINK_STR++"; 56 57# name of your site or organization to appear in page titles 58# replace this with something more descriptive for clearer bookmarks 59our$site_name="++GITWEB_SITENAME++" 60|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 61 62# filename of html text to include at top of each page 63our$site_header="++GITWEB_SITE_HEADER++"; 64# html text to include at home page 65our$home_text="++GITWEB_HOMETEXT++"; 66# filename of html text to include at bottom of each page 67our$site_footer="++GITWEB_SITE_FOOTER++"; 68 69# URI of stylesheets 70our@stylesheets= ("++GITWEB_CSS++"); 71# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 72our$stylesheet=undef; 73# URI of GIT logo (72x27 size) 74our$logo="++GITWEB_LOGO++"; 75# URI of GIT favicon, assumed to be image/png type 76our$favicon="++GITWEB_FAVICON++"; 77 78# URI and label (title) of GIT logo link 79#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 80#our $logo_label = "git documentation"; 81our$logo_url="http://git.or.cz/"; 82our$logo_label="git homepage"; 83 84# source of projects list 85our$projects_list="++GITWEB_LIST++"; 86 87# the width (in characters) of the projects list "Description" column 88our$projects_list_description_width=25; 89 90# default order of projects list 91# valid values are none, project, descr, owner, and age 92our$default_projects_order="project"; 93 94# show repository only if this file exists 95# (only effective if this variable evaluates to true) 96our$export_ok="++GITWEB_EXPORT_OK++"; 97 98# only allow viewing of repositories also shown on the overview page 99our$strict_export="++GITWEB_STRICT_EXPORT++"; 100 101# list of git base URLs used for URL to where fetch project from, 102# i.e. full URL is "$git_base_url/$project" 103our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 104 105# default blob_plain mimetype and default charset for text/plain blob 106our$default_blob_plain_mimetype='text/plain'; 107our$default_text_plain_charset=undef; 108 109# file to use for guessing MIME types before trying /etc/mime.types 110# (relative to the current git repository) 111our$mimetypes_file=undef; 112 113# assume this charset if line contains non-UTF-8 characters; 114# it should be valid encoding (see Encoding::Supported(3pm) for list), 115# for which encoding all byte sequences are valid, for example 116# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 117# could be even 'utf-8' for the old behavior) 118our$fallback_encoding='latin1'; 119 120# rename detection options for git-diff and git-diff-tree 121# - default is '-M', with the cost proportional to 122# (number of removed files) * (number of new files). 123# - more costly is '-C' (which implies '-M'), with the cost proportional to 124# (number of changed files + number of removed files) * (number of new files) 125# - even more costly is '-C', '--find-copies-harder' with cost 126# (number of files in the original tree) * (number of new files) 127# - one might want to include '-B' option, e.g. '-B', '-M' 128our@diff_opts= ('-M');# taken from git_commit 129 130# information about snapshot formats that gitweb is capable of serving 131our%known_snapshot_formats= ( 132# name => { 133# 'display' => display name, 134# 'type' => mime type, 135# 'suffix' => filename suffix, 136# 'format' => --format for git-archive, 137# 'compressor' => [compressor command and arguments] 138# (array reference, optional)} 139# 140'tgz'=> { 141'display'=>'tar.gz', 142'type'=>'application/x-gzip', 143'suffix'=>'.tar.gz', 144'format'=>'tar', 145'compressor'=> ['gzip']}, 146 147'tbz2'=> { 148'display'=>'tar.bz2', 149'type'=>'application/x-bzip2', 150'suffix'=>'.tar.bz2', 151'format'=>'tar', 152'compressor'=> ['bzip2']}, 153 154'zip'=> { 155'display'=>'zip', 156'type'=>'application/x-zip', 157'suffix'=>'.zip', 158'format'=>'zip'}, 159); 160 161# Aliases so we understand old gitweb.snapshot values in repository 162# configuration. 163our%known_snapshot_format_aliases= ( 164'gzip'=>'tgz', 165'bzip2'=>'tbz2', 166 167# backward compatibility: legacy gitweb config support 168'x-gzip'=>undef,'gz'=>undef, 169'x-bzip2'=>undef,'bz2'=>undef, 170'x-zip'=>undef,''=>undef, 171); 172 173# You define site-wide feature defaults here; override them with 174# $GITWEB_CONFIG as necessary. 175our%feature= ( 176# feature => { 177# 'sub' => feature-sub (subroutine), 178# 'override' => allow-override (boolean), 179# 'default' => [ default options...] (array reference)} 180# 181# if feature is overridable (it means that allow-override has true value), 182# then feature-sub will be called with default options as parameters; 183# return value of feature-sub indicates if to enable specified feature 184# 185# if there is no 'sub' key (no feature-sub), then feature cannot be 186# overriden 187# 188# use gitweb_check_feature(<feature>) to check if <feature> is enabled 189 190# Enable the 'blame' blob view, showing the last commit that modified 191# each line in the file. This can be very CPU-intensive. 192 193# To enable system wide have in $GITWEB_CONFIG 194# $feature{'blame'}{'default'} = [1]; 195# To have project specific config enable override in $GITWEB_CONFIG 196# $feature{'blame'}{'override'} = 1; 197# and in project config gitweb.blame = 0|1; 198'blame'=> { 199'sub'=> \&feature_blame, 200'override'=>0, 201'default'=> [0]}, 202 203# Enable the 'snapshot' link, providing a compressed archive of any 204# tree. This can potentially generate high traffic if you have large 205# project. 206 207# Value is a list of formats defined in %known_snapshot_formats that 208# you wish to offer. 209# To disable system wide have in $GITWEB_CONFIG 210# $feature{'snapshot'}{'default'} = []; 211# To have project specific config enable override in $GITWEB_CONFIG 212# $feature{'snapshot'}{'override'} = 1; 213# and in project config, a comma-separated list of formats or "none" 214# to disable. Example: gitweb.snapshot = tbz2,zip; 215'snapshot'=> { 216'sub'=> \&feature_snapshot, 217'override'=>0, 218'default'=> ['tgz']}, 219 220# Enable text search, which will list the commits which match author, 221# committer or commit text to a given string. Enabled by default. 222# Project specific override is not supported. 223'search'=> { 224'override'=>0, 225'default'=> [1]}, 226 227# Enable grep search, which will list the files in currently selected 228# tree containing the given string. Enabled by default. This can be 229# potentially CPU-intensive, of course. 230 231# To enable system wide have in $GITWEB_CONFIG 232# $feature{'grep'}{'default'} = [1]; 233# To have project specific config enable override in $GITWEB_CONFIG 234# $feature{'grep'}{'override'} = 1; 235# and in project config gitweb.grep = 0|1; 236'grep'=> { 237'override'=>0, 238'default'=> [1]}, 239 240# Enable the pickaxe search, which will list the commits that modified 241# a given string in a file. This can be practical and quite faster 242# alternative to 'blame', but still potentially CPU-intensive. 243 244# To enable system wide have in $GITWEB_CONFIG 245# $feature{'pickaxe'}{'default'} = [1]; 246# To have project specific config enable override in $GITWEB_CONFIG 247# $feature{'pickaxe'}{'override'} = 1; 248# and in project config gitweb.pickaxe = 0|1; 249'pickaxe'=> { 250'sub'=> \&feature_pickaxe, 251'override'=>0, 252'default'=> [1]}, 253 254# Make gitweb use an alternative format of the URLs which can be 255# more readable and natural-looking: project name is embedded 256# directly in the path and the query string contains other 257# auxiliary information. All gitweb installations recognize 258# URL in either format; this configures in which formats gitweb 259# generates links. 260 261# To enable system wide have in $GITWEB_CONFIG 262# $feature{'pathinfo'}{'default'} = [1]; 263# Project specific override is not supported. 264 265# Note that you will need to change the default location of CSS, 266# favicon, logo and possibly other files to an absolute URL. Also, 267# if gitweb.cgi serves as your indexfile, you will need to force 268# $my_uri to contain the script name in your $GITWEB_CONFIG. 269'pathinfo'=> { 270'override'=>0, 271'default'=> [0]}, 272 273# Make gitweb consider projects in project root subdirectories 274# to be forks of existing projects. Given project $projname.git, 275# projects matching $projname/*.git will not be shown in the main 276# projects list, instead a '+' mark will be added to $projname 277# there and a 'forks' view will be enabled for the project, listing 278# all the forks. If project list is taken from a file, forks have 279# to be listed after the main project. 280 281# To enable system wide have in $GITWEB_CONFIG 282# $feature{'forks'}{'default'} = [1]; 283# Project specific override is not supported. 284'forks'=> { 285'override'=>0, 286'default'=> [0]}, 287 288# Insert custom links to the action bar of all project pages. 289# This enables you mainly to link to third-party scripts integrating 290# into gitweb; e.g. git-browser for graphical history representation 291# or custom web-based repository administration interface. 292 293# The 'default' value consists of a list of triplets in the form 294# (label, link, position) where position is the label after which 295# to inster the link and link is a format string where %n expands 296# to the project name, %f to the project path within the filesystem, 297# %h to the current hash (h gitweb parameter) and %b to the current 298# hash base (hb gitweb parameter). 299 300# To enable system wide have in $GITWEB_CONFIG e.g. 301# $feature{'actions'}{'default'} = [('graphiclog', 302# '/git-browser/by-commit.html?r=%n', 'summary')]; 303# Project specific override is not supported. 304'actions'=> { 305'override'=>0, 306'default'=> []}, 307 308# Allow gitweb scan project content tags described in ctags/ 309# of project repository, and display the popular Web 2.0-ish 310# "tag cloud" near the project list. Note that this is something 311# COMPLETELY different from the normal Git tags. 312 313# gitweb by itself can show existing tags, but it does not handle 314# tagging itself; you need an external application for that. 315# For an example script, check Girocco's cgi/tagproj.cgi. 316# You may want to install the HTML::TagCloud Perl module to get 317# a pretty tag cloud instead of just a list of tags. 318 319# To enable system wide have in $GITWEB_CONFIG 320# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 321# Project specific override is not supported. 322'ctags'=> { 323'override'=>0, 324'default'=> [0]}, 325); 326 327sub gitweb_check_feature { 328my($name) =@_; 329return unlessexists$feature{$name}; 330my($sub,$override,@defaults) = ( 331$feature{$name}{'sub'}, 332$feature{$name}{'override'}, 333@{$feature{$name}{'default'}}); 334if(!$override) {return@defaults; } 335if(!defined$sub) { 336warn"feature$nameis not overrideable"; 337return@defaults; 338} 339return$sub->(@defaults); 340} 341 342sub feature_blame { 343my($val) = git_get_project_config('blame','--bool'); 344 345if($valeq'true') { 346return1; 347}elsif($valeq'false') { 348return0; 349} 350 351return$_[0]; 352} 353 354sub feature_snapshot { 355my(@fmts) =@_; 356 357my($val) = git_get_project_config('snapshot'); 358 359if($val) { 360@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 361} 362 363return@fmts; 364} 365 366sub feature_grep { 367my($val) = git_get_project_config('grep','--bool'); 368 369if($valeq'true') { 370return(1); 371}elsif($valeq'false') { 372return(0); 373} 374 375return($_[0]); 376} 377 378sub feature_pickaxe { 379my($val) = git_get_project_config('pickaxe','--bool'); 380 381if($valeq'true') { 382return(1); 383}elsif($valeq'false') { 384return(0); 385} 386 387return($_[0]); 388} 389 390# checking HEAD file with -e is fragile if the repository was 391# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 392# and then pruned. 393sub check_head_link { 394my($dir) =@_; 395my$headfile="$dir/HEAD"; 396return((-e $headfile) || 397(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 398} 399 400sub check_export_ok { 401my($dir) =@_; 402return(check_head_link($dir) && 403(!$export_ok|| -e "$dir/$export_ok")); 404} 405 406# process alternate names for backward compatibility 407# filter out unsupported (unknown) snapshot formats 408sub filter_snapshot_fmts { 409my@fmts=@_; 410 411@fmts=map{ 412exists$known_snapshot_format_aliases{$_} ? 413$known_snapshot_format_aliases{$_} :$_}@fmts; 414@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 415 416} 417 418our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 419if(-e $GITWEB_CONFIG) { 420do$GITWEB_CONFIG; 421}else{ 422our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 423do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 424} 425 426# version of the core git binary 427our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 428 429$projects_list||=$projectroot; 430 431# ====================================================================== 432# input validation and dispatch 433 434# input parameters can be collected from a variety of sources (presently, CGI 435# and PATH_INFO), so we define an %input_params hash that collects them all 436# together during validation: this allows subsequent uses (e.g. href()) to be 437# agnostic of the parameter origin 438 439my%input_params= (); 440 441# input parameters are stored with the long parameter name as key. This will 442# also be used in the href subroutine to convert parameters to their CGI 443# equivalent, and since the href() usage is the most frequent one, we store 444# the name -> CGI key mapping here, instead of the reverse. 445# 446# XXX: Warning: If you touch this, check the search form for updating, 447# too. 448 449my@cgi_param_mapping= ( 450 project =>"p", 451 action =>"a", 452 file_name =>"f", 453 file_parent =>"fp", 454 hash =>"h", 455 hash_parent =>"hp", 456 hash_base =>"hb", 457 hash_parent_base =>"hpb", 458 page =>"pg", 459 order =>"o", 460 searchtext =>"s", 461 searchtype =>"st", 462 snapshot_format =>"sf", 463 extra_options =>"opt", 464 search_use_regexp =>"sr", 465); 466my%cgi_param_mapping=@cgi_param_mapping; 467 468# we will also need to know the possible actions, for validation 469my%actions= ( 470"blame"=> \&git_blame, 471"blobdiff"=> \&git_blobdiff, 472"blobdiff_plain"=> \&git_blobdiff_plain, 473"blob"=> \&git_blob, 474"blob_plain"=> \&git_blob_plain, 475"commitdiff"=> \&git_commitdiff, 476"commitdiff_plain"=> \&git_commitdiff_plain, 477"commit"=> \&git_commit, 478"forks"=> \&git_forks, 479"heads"=> \&git_heads, 480"history"=> \&git_history, 481"log"=> \&git_log, 482"rss"=> \&git_rss, 483"atom"=> \&git_atom, 484"search"=> \&git_search, 485"search_help"=> \&git_search_help, 486"shortlog"=> \&git_shortlog, 487"summary"=> \&git_summary, 488"tag"=> \&git_tag, 489"tags"=> \&git_tags, 490"tree"=> \&git_tree, 491"snapshot"=> \&git_snapshot, 492"object"=> \&git_object, 493# those below don't need $project 494"opml"=> \&git_opml, 495"project_list"=> \&git_project_list, 496"project_index"=> \&git_project_index, 497); 498 499# finally, we have the hash of allowed extra_options for the commands that 500# allow them 501my%allowed_options= ( 502"--no-merges"=> [qw(rss atom log shortlog history)], 503); 504 505# fill %input_params with the CGI parameters. All values except for 'opt' 506# should be single values, but opt can be an array. We should probably 507# build an array of parameters that can be multi-valued, but since for the time 508# being it's only this one, we just single it out 509while(my($name,$symbol) =each%cgi_param_mapping) { 510if($symboleq'opt') { 511$input_params{$name} = [$cgi->param($symbol) ]; 512}else{ 513$input_params{$name} =$cgi->param($symbol); 514} 515} 516 517# now read PATH_INFO and update the parameter list for missing parameters 518sub evaluate_path_info { 519return ifdefined$input_params{'project'}; 520return if!$path_info; 521$path_info=~ s,^/+,,; 522return if!$path_info; 523 524# find which part of PATH_INFO is project 525my$project=$path_info; 526$project=~ s,/+$,,; 527while($project&& !check_head_link("$projectroot/$project")) { 528$project=~ s,/*[^/]*$,,; 529} 530return unless$project; 531$input_params{'project'} =$project; 532 533# do not change any parameters if an action is given using the query string 534return if$input_params{'action'}; 535$path_info=~ s,^\Q$project\E/*,,; 536 537my($refname,$pathname) =split(/:/,$path_info,2); 538if(defined$pathname) { 539# we got "project.git/branch:filename" or "project.git/branch:dir/" 540# we could use git_get_type(branch:pathname), but it needs $git_dir 541$pathname=~ s,^/+,,; 542if(!$pathname||substr($pathname, -1)eq"/") { 543$input_params{'action'} ="tree"; 544$pathname=~ s,/$,,; 545}else{ 546$input_params{'action'} ="blob_plain"; 547} 548$input_params{'hash_base'} ||=$refname; 549$input_params{'file_name'} ||=$pathname; 550}elsif(defined$refname) { 551# we got "project.git/branch" 552$input_params{'action'} ="shortlog"; 553$input_params{'hash'} ||=$refname; 554} 555} 556evaluate_path_info(); 557 558our$action=$input_params{'action'}; 559if(defined$action) { 560if(!validate_action($action)) { 561 die_error(400,"Invalid action parameter"); 562} 563} 564 565# parameters which are pathnames 566our$project=$input_params{'project'}; 567if(defined$project) { 568if(!validate_project($project)) { 569undef$project; 570 die_error(404,"No such project"); 571} 572} 573 574our$file_name=$input_params{'file_name'}; 575if(defined$file_name) { 576if(!validate_pathname($file_name)) { 577 die_error(400,"Invalid file parameter"); 578} 579} 580 581our$file_parent=$input_params{'file_parent'}; 582if(defined$file_parent) { 583if(!validate_pathname($file_parent)) { 584 die_error(400,"Invalid file parent parameter"); 585} 586} 587 588# parameters which are refnames 589our$hash=$input_params{'hash'}; 590if(defined$hash) { 591if(!validate_refname($hash)) { 592 die_error(400,"Invalid hash parameter"); 593} 594} 595 596our$hash_parent=$input_params{'hash_parent'}; 597if(defined$hash_parent) { 598if(!validate_refname($hash_parent)) { 599 die_error(400,"Invalid hash parent parameter"); 600} 601} 602 603our$hash_base=$input_params{'hash_base'}; 604if(defined$hash_base) { 605if(!validate_refname($hash_base)) { 606 die_error(400,"Invalid hash base parameter"); 607} 608} 609 610our@extra_options= @{$input_params{'extra_options'}}; 611# @extra_options is always defined, since it can only be (currently) set from 612# CGI, and $cgi->param() returns the empty array in array context if the param 613# is not set 614foreachmy$opt(@extra_options) { 615if(not exists$allowed_options{$opt}) { 616 die_error(400,"Invalid option parameter"); 617} 618if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 619 die_error(400,"Invalid option parameter for this action"); 620} 621} 622 623our$hash_parent_base=$input_params{'hash_parent_base'}; 624if(defined$hash_parent_base) { 625if(!validate_refname($hash_parent_base)) { 626 die_error(400,"Invalid hash parent base parameter"); 627} 628} 629 630# other parameters 631our$page=$input_params{'page'}; 632if(defined$page) { 633if($page=~m/[^0-9]/) { 634 die_error(400,"Invalid page parameter"); 635} 636} 637 638our$searchtype=$input_params{'searchtype'}; 639if(defined$searchtype) { 640if($searchtype=~m/[^a-z]/) { 641 die_error(400,"Invalid searchtype parameter"); 642} 643} 644 645our$search_use_regexp=$input_params{'search_use_regexp'}; 646 647our$searchtext=$input_params{'searchtext'}; 648our$search_regexp; 649if(defined$searchtext) { 650if(length($searchtext) <2) { 651 die_error(403,"At least two characters are required for search parameter"); 652} 653$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 654} 655 656# path to the current git repository 657our$git_dir; 658$git_dir="$projectroot/$project"if$project; 659 660# dispatch 661if(!defined$action) { 662if(defined$hash) { 663$action= git_get_type($hash); 664}elsif(defined$hash_base&&defined$file_name) { 665$action= git_get_type("$hash_base:$file_name"); 666}elsif(defined$project) { 667$action='summary'; 668}else{ 669$action='project_list'; 670} 671} 672if(!defined($actions{$action})) { 673 die_error(400,"Unknown action"); 674} 675if($action!~m/^(opml|project_list|project_index)$/&& 676!$project) { 677 die_error(400,"Project needed"); 678} 679$actions{$action}->(); 680exit; 681 682## ====================================================================== 683## action links 684 685sub href (%) { 686my%params=@_; 687# default is to use -absolute url() i.e. $my_uri 688my$href=$params{-full} ?$my_url:$my_uri; 689 690$params{'project'} =$projectunlessexists$params{'project'}; 691 692if($params{-replay}) { 693while(my($name,$symbol) =each%cgi_param_mapping) { 694if(!exists$params{$name}) { 695$params{$name} =$input_params{$name}; 696} 697} 698} 699 700my($use_pathinfo) = gitweb_check_feature('pathinfo'); 701if($use_pathinfo) { 702# use PATH_INFO for project name 703$href.="/".esc_url($params{'project'})ifdefined$params{'project'}; 704delete$params{'project'}; 705 706# Summary just uses the project path URL 707if(defined$params{'action'} &&$params{'action'}eq'summary') { 708delete$params{'action'}; 709} 710} 711 712# now encode the parameters explicitly 713my@result= (); 714for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 715my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 716if(defined$params{$name}) { 717if(ref($params{$name})eq"ARRAY") { 718foreachmy$par(@{$params{$name}}) { 719push@result,$symbol."=". esc_param($par); 720} 721}else{ 722push@result,$symbol."=". esc_param($params{$name}); 723} 724} 725} 726$href.="?".join(';',@result)ifscalar@result; 727 728return$href; 729} 730 731 732## ====================================================================== 733## validation, quoting/unquoting and escaping 734 735sub validate_action { 736my$input=shift||returnundef; 737returnundefunlessexists$actions{$input}; 738return$input; 739} 740 741sub validate_project { 742my$input=shift||returnundef; 743if(!validate_pathname($input) || 744!(-d "$projectroot/$input") || 745!check_head_link("$projectroot/$input") || 746($export_ok&& !(-e "$projectroot/$input/$export_ok")) || 747($strict_export&& !project_in_list($input))) { 748returnundef; 749}else{ 750return$input; 751} 752} 753 754sub validate_pathname { 755my$input=shift||returnundef; 756 757# no '.' or '..' as elements of path, i.e. no '.' nor '..' 758# at the beginning, at the end, and between slashes. 759# also this catches doubled slashes 760if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 761returnundef; 762} 763# no null characters 764if($input=~m!\0!) { 765returnundef; 766} 767return$input; 768} 769 770sub validate_refname { 771my$input=shift||returnundef; 772 773# textual hashes are O.K. 774if($input=~m/^[0-9a-fA-F]{40}$/) { 775return$input; 776} 777# it must be correct pathname 778$input= validate_pathname($input) 779orreturnundef; 780# restrictions on ref name according to git-check-ref-format 781if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 782returnundef; 783} 784return$input; 785} 786 787# decode sequences of octets in utf8 into Perl's internal form, 788# which is utf-8 with utf8 flag set if needed. gitweb writes out 789# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 790sub to_utf8 { 791my$str=shift; 792if(utf8::valid($str)) { 793 utf8::decode($str); 794return$str; 795}else{ 796return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 797} 798} 799 800# quote unsafe chars, but keep the slash, even when it's not 801# correct, but quoted slashes look too horrible in bookmarks 802sub esc_param { 803my$str=shift; 804$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg; 805$str=~s/\+/%2B/g; 806$str=~s/ /\+/g; 807return$str; 808} 809 810# quote unsafe chars in whole URL, so some charactrs cannot be quoted 811sub esc_url { 812my$str=shift; 813$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg; 814$str=~s/\+/%2B/g; 815$str=~s/ /\+/g; 816return$str; 817} 818 819# replace invalid utf8 character with SUBSTITUTION sequence 820sub esc_html ($;%) { 821my$str=shift; 822my%opts=@_; 823 824$str= to_utf8($str); 825$str=$cgi->escapeHTML($str); 826if($opts{'-nbsp'}) { 827$str=~s/ / /g; 828} 829$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg; 830return$str; 831} 832 833# quote control characters and escape filename to HTML 834sub esc_path { 835my$str=shift; 836my%opts=@_; 837 838$str= to_utf8($str); 839$str=$cgi->escapeHTML($str); 840if($opts{'-nbsp'}) { 841$str=~s/ / /g; 842} 843$str=~ s|([[:cntrl:]])|quot_cec($1)|eg; 844return$str; 845} 846 847# Make control characters "printable", using character escape codes (CEC) 848sub quot_cec { 849my$cntrl=shift; 850my%opts=@_; 851my%es= (# character escape codes, aka escape sequences 852"\t"=>'\t',# tab (HT) 853"\n"=>'\n',# line feed (LF) 854"\r"=>'\r',# carrige return (CR) 855"\f"=>'\f',# form feed (FF) 856"\b"=>'\b',# backspace (BS) 857"\a"=>'\a',# alarm (bell) (BEL) 858"\e"=>'\e',# escape (ESC) 859"\013"=>'\v',# vertical tab (VT) 860"\000"=>'\0',# nul character (NUL) 861); 862my$chr= ( (exists$es{$cntrl}) 863?$es{$cntrl} 864:sprintf('\%2x',ord($cntrl)) ); 865if($opts{-nohtml}) { 866return$chr; 867}else{ 868return"<span class=\"cntrl\">$chr</span>"; 869} 870} 871 872# Alternatively use unicode control pictures codepoints, 873# Unicode "printable representation" (PR) 874sub quot_upr { 875my$cntrl=shift; 876my%opts=@_; 877 878my$chr=sprintf('&#%04d;',0x2400+ord($cntrl)); 879if($opts{-nohtml}) { 880return$chr; 881}else{ 882return"<span class=\"cntrl\">$chr</span>"; 883} 884} 885 886# git may return quoted and escaped filenames 887sub unquote { 888my$str=shift; 889 890sub unq { 891my$seq=shift; 892my%es= (# character escape codes, aka escape sequences 893't'=>"\t",# tab (HT, TAB) 894'n'=>"\n",# newline (NL) 895'r'=>"\r",# return (CR) 896'f'=>"\f",# form feed (FF) 897'b'=>"\b",# backspace (BS) 898'a'=>"\a",# alarm (bell) (BEL) 899'e'=>"\e",# escape (ESC) 900'v'=>"\013",# vertical tab (VT) 901); 902 903if($seq=~m/^[0-7]{1,3}$/) { 904# octal char sequence 905returnchr(oct($seq)); 906}elsif(exists$es{$seq}) { 907# C escape sequence, aka character escape code 908return$es{$seq}; 909} 910# quoted ordinary character 911return$seq; 912} 913 914if($str=~m/^"(.*)"$/) { 915# needs unquoting 916$str=$1; 917$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg; 918} 919return$str; 920} 921 922# escape tabs (convert tabs to spaces) 923sub untabify { 924my$line=shift; 925 926while((my$pos=index($line,"\t")) != -1) { 927if(my$count= (8- ($pos%8))) { 928my$spaces=' ' x $count; 929$line=~s/\t/$spaces/; 930} 931} 932 933return$line; 934} 935 936sub project_in_list { 937my$project=shift; 938my@list= git_get_projects_list(); 939return@list&&scalar(grep{$_->{'path'}eq$project}@list); 940} 941 942## ---------------------------------------------------------------------- 943## HTML aware string manipulation 944 945# Try to chop given string on a word boundary between position 946# $len and $len+$add_len. If there is no word boundary there, 947# chop at $len+$add_len. Do not chop if chopped part plus ellipsis 948# (marking chopped part) would be longer than given string. 949sub chop_str { 950my$str=shift; 951my$len=shift; 952my$add_len=shift||10; 953my$where=shift||'right';# 'left' | 'center' | 'right' 954 955# Make sure perl knows it is utf8 encoded so we don't 956# cut in the middle of a utf8 multibyte char. 957$str= to_utf8($str); 958 959# allow only $len chars, but don't cut a word if it would fit in $add_len 960# if it doesn't fit, cut it if it's still longer than the dots we would add 961# remove chopped character entities entirely 962 963# when chopping in the middle, distribute $len into left and right part 964# return early if chopping wouldn't make string shorter 965if($whereeq'center') { 966return$strif($len+5>=length($str));# filler is length 5 967$len=int($len/2); 968}else{ 969return$strif($len+4>=length($str));# filler is length 4 970} 971 972# regexps: ending and beginning with word part up to $add_len 973my$endre=qr/.{$len}\w{0,$add_len}/; 974my$begre=qr/\w{0,$add_len}.{$len}/; 975 976if($whereeq'left') { 977$str=~m/^(.*?)($begre)$/; 978my($lead,$body) = ($1,$2); 979if(length($lead) >4) { 980$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/); 981$lead=" ..."; 982} 983return"$lead$body"; 984 985}elsif($whereeq'center') { 986$str=~m/^($endre)(.*)$/; 987my($left,$str) = ($1,$2); 988$str=~m/^(.*?)($begre)$/; 989my($mid,$right) = ($1,$2); 990if(length($mid) >5) { 991$left=~s/&[^;]*$//; 992$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/); 993$mid=" ... "; 994} 995return"$left$mid$right"; 996 997}else{ 998$str=~m/^($endre)(.*)$/; 999my$body=$1;1000my$tail=$2;1001if(length($tail) >4) {1002$body=~s/&[^;]*$//;1003$tail="... ";1004}1005return"$body$tail";1006}1007}10081009# takes the same arguments as chop_str, but also wraps a <span> around the1010# result with a title attribute if it does get chopped. Additionally, the1011# string is HTML-escaped.1012sub chop_and_escape_str {1013my($str) =@_;10141015my$chopped= chop_str(@_);1016if($choppedeq$str) {1017return esc_html($chopped);1018}else{1019$str=~s/([[:cntrl:]])/?/g;1020return$cgi->span({-title=>$str}, esc_html($chopped));1021}1022}10231024## ----------------------------------------------------------------------1025## functions returning short strings10261027# CSS class for given age value (in seconds)1028sub age_class {1029my$age=shift;10301031if(!defined$age) {1032return"noage";1033}elsif($age<60*60*2) {1034return"age0";1035}elsif($age<60*60*24*2) {1036return"age1";1037}else{1038return"age2";1039}1040}10411042# convert age in seconds to "nn units ago" string1043sub age_string {1044my$age=shift;1045my$age_str;10461047if($age>60*60*24*365*2) {1048$age_str= (int$age/60/60/24/365);1049$age_str.=" years ago";1050}elsif($age>60*60*24*(365/12)*2) {1051$age_str=int$age/60/60/24/(365/12);1052$age_str.=" months ago";1053}elsif($age>60*60*24*7*2) {1054$age_str=int$age/60/60/24/7;1055$age_str.=" weeks ago";1056}elsif($age>60*60*24*2) {1057$age_str=int$age/60/60/24;1058$age_str.=" days ago";1059}elsif($age>60*60*2) {1060$age_str=int$age/60/60;1061$age_str.=" hours ago";1062}elsif($age>60*2) {1063$age_str=int$age/60;1064$age_str.=" min ago";1065}elsif($age>2) {1066$age_str=int$age;1067$age_str.=" sec ago";1068}else{1069$age_str.=" right now";1070}1071return$age_str;1072}10731074useconstant{1075 S_IFINVALID =>0030000,1076 S_IFGITLINK =>0160000,1077};10781079# submodule/subproject, a commit object reference1080sub S_ISGITLINK($) {1081my$mode=shift;10821083return(($mode& S_IFMT) == S_IFGITLINK)1084}10851086# convert file mode in octal to symbolic file mode string1087sub mode_str {1088my$mode=oct shift;10891090if(S_ISGITLINK($mode)) {1091return'm---------';1092}elsif(S_ISDIR($mode& S_IFMT)) {1093return'drwxr-xr-x';1094}elsif(S_ISLNK($mode)) {1095return'lrwxrwxrwx';1096}elsif(S_ISREG($mode)) {1097# git cares only about the executable bit1098if($mode& S_IXUSR) {1099return'-rwxr-xr-x';1100}else{1101return'-rw-r--r--';1102};1103}else{1104return'----------';1105}1106}11071108# convert file mode in octal to file type string1109sub file_type {1110my$mode=shift;11111112if($mode!~m/^[0-7]+$/) {1113return$mode;1114}else{1115$mode=oct$mode;1116}11171118if(S_ISGITLINK($mode)) {1119return"submodule";1120}elsif(S_ISDIR($mode& S_IFMT)) {1121return"directory";1122}elsif(S_ISLNK($mode)) {1123return"symlink";1124}elsif(S_ISREG($mode)) {1125return"file";1126}else{1127return"unknown";1128}1129}11301131# convert file mode in octal to file type description string1132sub file_type_long {1133my$mode=shift;11341135if($mode!~m/^[0-7]+$/) {1136return$mode;1137}else{1138$mode=oct$mode;1139}11401141if(S_ISGITLINK($mode)) {1142return"submodule";1143}elsif(S_ISDIR($mode& S_IFMT)) {1144return"directory";1145}elsif(S_ISLNK($mode)) {1146return"symlink";1147}elsif(S_ISREG($mode)) {1148if($mode& S_IXUSR) {1149return"executable";1150}else{1151return"file";1152};1153}else{1154return"unknown";1155}1156}115711581159## ----------------------------------------------------------------------1160## functions returning short HTML fragments, or transforming HTML fragments1161## which don't belong to other sections11621163# format line of commit message.1164sub format_log_line_html {1165my$line=shift;11661167$line= esc_html($line, -nbsp=>1);1168if($line=~m/([0-9a-fA-F]{8,40})/) {1169my$hash_text=$1;1170my$link=1171$cgi->a({-href => href(action=>"object", hash=>$hash_text),1172-class=>"text"},$hash_text);1173$line=~s/$hash_text/$link/;1174}1175return$line;1176}11771178# format marker of refs pointing to given object11791180# the destination action is chosen based on object type and current context:1181# - for annotated tags, we choose the tag view unless it's the current view1182# already, in which case we go to shortlog view1183# - for other refs, we keep the current view if we're in history, shortlog or1184# log view, and select shortlog otherwise1185sub format_ref_marker {1186my($refs,$id) =@_;1187my$markers='';11881189if(defined$refs->{$id}) {1190foreachmy$ref(@{$refs->{$id}}) {1191# this code exploits the fact that non-lightweight tags are the1192# only indirect objects, and that they are the only objects for which1193# we want to use tag instead of shortlog as action1194my($type,$name) =qw();1195my$indirect= ($ref=~s/\^\{\}$//);1196# e.g. tags/v2.6.11 or heads/next1197if($ref=~m!^(.*?)s?/(.*)$!) {1198$type=$1;1199$name=$2;1200}else{1201$type="ref";1202$name=$ref;1203}12041205my$class=$type;1206$class.=" indirect"if$indirect;12071208my$dest_action="shortlog";12091210if($indirect) {1211$dest_action="tag"unless$actioneq"tag";1212}elsif($action=~/^(history|(short)?log)$/) {1213$dest_action=$action;1214}12151216my$dest="";1217$dest.="refs/"unless$ref=~ m!^refs/!;1218$dest.=$ref;12191220my$link=$cgi->a({1221-href => href(1222 action=>$dest_action,1223 hash=>$dest1224)},$name);12251226$markers.=" <span class=\"$class\"title=\"$ref\">".1227$link."</span>";1228}1229}12301231if($markers) {1232return' <span class="refs">'.$markers.'</span>';1233}else{1234return"";1235}1236}12371238# format, perhaps shortened and with markers, title line1239sub format_subject_html {1240my($long,$short,$href,$extra) =@_;1241$extra=''unlessdefined($extra);12421243if(length($short) <length($long)) {1244return$cgi->a({-href =>$href, -class=>"list subject",1245-title => to_utf8($long)},1246 esc_html($short) .$extra);1247}else{1248return$cgi->a({-href =>$href, -class=>"list subject"},1249 esc_html($long) .$extra);1250}1251}12521253# format git diff header line, i.e. "diff --(git|combined|cc) ..."1254sub format_git_diff_header_line {1255my$line=shift;1256my$diffinfo=shift;1257my($from,$to) =@_;12581259if($diffinfo->{'nparents'}) {1260# combined diff1261$line=~s!^(diff (.*?) )"?.*$!$1!;1262if($to->{'href'}) {1263$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1264 esc_path($to->{'file'}));1265}else{# file was deleted (no href)1266$line.= esc_path($to->{'file'});1267}1268}else{1269# "ordinary" diff1270$line=~s!^(diff (.*?) )"?a/.*$!$1!;1271if($from->{'href'}) {1272$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1273'a/'. esc_path($from->{'file'}));1274}else{# file was added (no href)1275$line.='a/'. esc_path($from->{'file'});1276}1277$line.=' ';1278if($to->{'href'}) {1279$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1280'b/'. esc_path($to->{'file'}));1281}else{# file was deleted1282$line.='b/'. esc_path($to->{'file'});1283}1284}12851286return"<div class=\"diff header\">$line</div>\n";1287}12881289# format extended diff header line, before patch itself1290sub format_extended_diff_header_line {1291my$line=shift;1292my$diffinfo=shift;1293my($from,$to) =@_;12941295# match <path>1296if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1297$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1298 esc_path($from->{'file'}));1299}1300if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1301$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1302 esc_path($to->{'file'}));1303}1304# match single <mode>1305if($line=~m/\s(\d{6})$/) {1306$line.='<span class="info"> ('.1307 file_type_long($1) .1308')</span>';1309}1310# match <hash>1311if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1312# can match only for combined diff1313$line='index ';1314for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1315if($from->{'href'}[$i]) {1316$line.=$cgi->a({-href=>$from->{'href'}[$i],1317-class=>"hash"},1318substr($diffinfo->{'from_id'}[$i],0,7));1319}else{1320$line.='0' x 7;1321}1322# separator1323$line.=','if($i<$diffinfo->{'nparents'} -1);1324}1325$line.='..';1326if($to->{'href'}) {1327$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1328substr($diffinfo->{'to_id'},0,7));1329}else{1330$line.='0' x 7;1331}13321333}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1334# can match only for ordinary diff1335my($from_link,$to_link);1336if($from->{'href'}) {1337$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1338substr($diffinfo->{'from_id'},0,7));1339}else{1340$from_link='0' x 7;1341}1342if($to->{'href'}) {1343$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1344substr($diffinfo->{'to_id'},0,7));1345}else{1346$to_link='0' x 7;1347}1348my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1349$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1350}13511352return$line."<br/>\n";1353}13541355# format from-file/to-file diff header1356sub format_diff_from_to_header {1357my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1358my$line;1359my$result='';13601361$line=$from_line;1362#assert($line =~ m/^---/) if DEBUG;1363# no extra formatting for "^--- /dev/null"1364if(!$diffinfo->{'nparents'}) {1365# ordinary (single parent) diff1366if($line=~m!^--- "?a/!) {1367if($from->{'href'}) {1368$line='--- a/'.1369$cgi->a({-href=>$from->{'href'}, -class=>"path"},1370 esc_path($from->{'file'}));1371}else{1372$line='--- a/'.1373 esc_path($from->{'file'});1374}1375}1376$result.= qq!<div class="diff from_file">$line</div>\n!;13771378}else{1379# combined diff (merge commit)1380for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1381if($from->{'href'}[$i]) {1382$line='--- '.1383$cgi->a({-href=>href(action=>"blobdiff",1384 hash_parent=>$diffinfo->{'from_id'}[$i],1385 hash_parent_base=>$parents[$i],1386 file_parent=>$from->{'file'}[$i],1387 hash=>$diffinfo->{'to_id'},1388 hash_base=>$hash,1389 file_name=>$to->{'file'}),1390-class=>"path",1391-title=>"diff". ($i+1)},1392$i+1) .1393'/'.1394$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1395 esc_path($from->{'file'}[$i]));1396}else{1397$line='--- /dev/null';1398}1399$result.= qq!<div class="diff from_file">$line</div>\n!;1400}1401}14021403$line=$to_line;1404#assert($line =~ m/^\+\+\+/) if DEBUG;1405# no extra formatting for "^+++ /dev/null"1406if($line=~m!^\+\+\+ "?b/!) {1407if($to->{'href'}) {1408$line='+++ b/'.1409$cgi->a({-href=>$to->{'href'}, -class=>"path"},1410 esc_path($to->{'file'}));1411}else{1412$line='+++ b/'.1413 esc_path($to->{'file'});1414}1415}1416$result.= qq!<div class="diff to_file">$line</div>\n!;14171418return$result;1419}14201421# create note for patch simplified by combined diff1422sub format_diff_cc_simplified {1423my($diffinfo,@parents) =@_;1424my$result='';14251426$result.="<div class=\"diff header\">".1427"diff --cc ";1428if(!is_deleted($diffinfo)) {1429$result.=$cgi->a({-href => href(action=>"blob",1430 hash_base=>$hash,1431 hash=>$diffinfo->{'to_id'},1432 file_name=>$diffinfo->{'to_file'}),1433-class=>"path"},1434 esc_path($diffinfo->{'to_file'}));1435}else{1436$result.= esc_path($diffinfo->{'to_file'});1437}1438$result.="</div>\n".# class="diff header"1439"<div class=\"diff nodifferences\">".1440"Simple merge".1441"</div>\n";# class="diff nodifferences"14421443return$result;1444}14451446# format patch (diff) line (not to be used for diff headers)1447sub format_diff_line {1448my$line=shift;1449my($from,$to) =@_;1450my$diff_class="";14511452chomp$line;14531454if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1455# combined diff1456my$prefix=substr($line,0,scalar@{$from->{'href'}});1457if($line=~m/^\@{3}/) {1458$diff_class=" chunk_header";1459}elsif($line=~m/^\\/) {1460$diff_class=" incomplete";1461}elsif($prefix=~tr/+/+/) {1462$diff_class=" add";1463}elsif($prefix=~tr/-/-/) {1464$diff_class=" rem";1465}1466}else{1467# assume ordinary diff1468my$char=substr($line,0,1);1469if($chareq'+') {1470$diff_class=" add";1471}elsif($chareq'-') {1472$diff_class=" rem";1473}elsif($chareq'@') {1474$diff_class=" chunk_header";1475}elsif($chareq"\\") {1476$diff_class=" incomplete";1477}1478}1479$line= untabify($line);1480if($from&&$to&&$line=~m/^\@{2} /) {1481my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1482$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;14831484$from_lines=0unlessdefined$from_lines;1485$to_lines=0unlessdefined$to_lines;14861487if($from->{'href'}) {1488$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1489-class=>"list"},$from_text);1490}1491if($to->{'href'}) {1492$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1493-class=>"list"},$to_text);1494}1495$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1496"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1497return"<div class=\"diff$diff_class\">$line</div>\n";1498}elsif($from&&$to&&$line=~m/^\@{3}/) {1499my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1500my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);15011502@from_text=split(' ',$ranges);1503for(my$i=0;$i<@from_text; ++$i) {1504($from_start[$i],$from_nlines[$i]) =1505(split(',',substr($from_text[$i],1)),0);1506}15071508$to_text=pop@from_text;1509$to_start=pop@from_start;1510$to_nlines=pop@from_nlines;15111512$line="<span class=\"chunk_info\">$prefix";1513for(my$i=0;$i<@from_text; ++$i) {1514if($from->{'href'}[$i]) {1515$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1516-class=>"list"},$from_text[$i]);1517}else{1518$line.=$from_text[$i];1519}1520$line.=" ";1521}1522if($to->{'href'}) {1523$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1524-class=>"list"},$to_text);1525}else{1526$line.=$to_text;1527}1528$line.="$prefix</span>".1529"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1530return"<div class=\"diff$diff_class\">$line</div>\n";1531}1532return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1533}15341535# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1536# linked. Pass the hash of the tree/commit to snapshot.1537sub format_snapshot_links {1538my($hash) =@_;1539my@snapshot_fmts= gitweb_check_feature('snapshot');1540@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);1541my$num_fmts=@snapshot_fmts;1542if($num_fmts>1) {1543# A parenthesized list of links bearing format names.1544# e.g. "snapshot (_tar.gz_ _zip_)"1545return"snapshot (".join(' ',map1546$cgi->a({1547-href => href(1548 action=>"snapshot",1549 hash=>$hash,1550 snapshot_format=>$_1551)1552},$known_snapshot_formats{$_}{'display'})1553,@snapshot_fmts) .")";1554}elsif($num_fmts==1) {1555# A single "snapshot" link whose tooltip bears the format name.1556# i.e. "_snapshot_"1557my($fmt) =@snapshot_fmts;1558return1559$cgi->a({1560-href => href(1561 action=>"snapshot",1562 hash=>$hash,1563 snapshot_format=>$fmt1564),1565-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1566},"snapshot");1567}else{# $num_fmts == 01568returnundef;1569}1570}15711572## ......................................................................1573## functions returning values to be passed, perhaps after some1574## transformation, to other functions; e.g. returning arguments to href()15751576# returns hash to be passed to href to generate gitweb URL1577# in -title key it returns description of link1578sub get_feed_info {1579my$format=shift||'Atom';1580my%res= (action =>lc($format));15811582# feed links are possible only for project views1583return unless(defined$project);1584# some views should link to OPML, or to generic project feed,1585# or don't have specific feed yet (so they should use generic)1586return if($action=~/^(?:tags|heads|forks|tag|search)$/x);15871588my$branch;1589# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1590# from tag links; this also makes possible to detect branch links1591if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1592(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1593$branch=$1;1594}1595# find log type for feed description (title)1596my$type='log';1597if(defined$file_name) {1598$type="history of$file_name";1599$type.="/"if($actioneq'tree');1600$type.=" on '$branch'"if(defined$branch);1601}else{1602$type="log of$branch"if(defined$branch);1603}16041605$res{-title} =$type;1606$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1607$res{'file_name'} =$file_name;16081609return%res;1610}16111612## ----------------------------------------------------------------------1613## git utility subroutines, invoking git commands16141615# returns path to the core git executable and the --git-dir parameter as list1616sub git_cmd {1617return$GIT,'--git-dir='.$git_dir;1618}16191620# quote the given arguments for passing them to the shell1621# quote_command("command", "arg 1", "arg with ' and ! characters")1622# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1623# Try to avoid using this function wherever possible.1624sub quote_command {1625returnjoin(' ',1626map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1627}16281629# get HEAD ref of given project as hash1630sub git_get_head_hash {1631my$project=shift;1632my$o_git_dir=$git_dir;1633my$retval=undef;1634$git_dir="$projectroot/$project";1635if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1636my$head= <$fd>;1637close$fd;1638if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1639$retval=$1;1640}1641}1642if(defined$o_git_dir) {1643$git_dir=$o_git_dir;1644}1645return$retval;1646}16471648# get type of given object1649sub git_get_type {1650my$hash=shift;16511652open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1653my$type= <$fd>;1654close$fdorreturn;1655chomp$type;1656return$type;1657}16581659# repository configuration1660our$config_file='';1661our%config;16621663# store multiple values for single key as anonymous array reference1664# single values stored directly in the hash, not as [ <value> ]1665sub hash_set_multi {1666my($hash,$key,$value) =@_;16671668if(!exists$hash->{$key}) {1669$hash->{$key} =$value;1670}elsif(!ref$hash->{$key}) {1671$hash->{$key} = [$hash->{$key},$value];1672}else{1673push@{$hash->{$key}},$value;1674}1675}16761677# return hash of git project configuration1678# optionally limited to some section, e.g. 'gitweb'1679sub git_parse_project_config {1680my$section_regexp=shift;1681my%config;16821683local$/="\0";16841685open my$fh,"-|", git_cmd(),"config",'-z','-l',1686orreturn;16871688while(my$keyval= <$fh>) {1689chomp$keyval;1690my($key,$value) =split(/\n/,$keyval,2);16911692 hash_set_multi(\%config,$key,$value)1693if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1694}1695close$fh;16961697return%config;1698}16991700# convert config value to boolean, 'true' or 'false'1701# no value, number > 0, 'true' and 'yes' values are true1702# rest of values are treated as false (never as error)1703sub config_to_bool {1704my$val=shift;17051706# strip leading and trailing whitespace1707$val=~s/^\s+//;1708$val=~s/\s+$//;17091710return(!defined$val||# section.key1711($val=~/^\d+$/&&$val) ||# section.key = 11712($val=~/^(?:true|yes)$/i));# section.key = true1713}17141715# convert config value to simple decimal number1716# an optional value suffix of 'k', 'm', or 'g' will cause the value1717# to be multiplied by 1024, 1048576, or 10737418241718sub config_to_int {1719my$val=shift;17201721# strip leading and trailing whitespace1722$val=~s/^\s+//;1723$val=~s/\s+$//;17241725if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1726$unit=lc($unit);1727# unknown unit is treated as 11728return$num* ($uniteq'g'?1073741824:1729$uniteq'm'?1048576:1730$uniteq'k'?1024:1);1731}1732return$val;1733}17341735# convert config value to array reference, if needed1736sub config_to_multi {1737my$val=shift;17381739returnref($val) ?$val: (defined($val) ? [$val] : []);1740}17411742sub git_get_project_config {1743my($key,$type) =@_;17441745# key sanity check1746return unless($key);1747$key=~s/^gitweb\.//;1748return if($key=~m/\W/);17491750# type sanity check1751if(defined$type) {1752$type=~s/^--//;1753$type=undef1754unless($typeeq'bool'||$typeeq'int');1755}17561757# get config1758if(!defined$config_file||1759$config_filene"$git_dir/config") {1760%config= git_parse_project_config('gitweb');1761$config_file="$git_dir/config";1762}17631764# ensure given type1765if(!defined$type) {1766return$config{"gitweb.$key"};1767}elsif($typeeq'bool') {1768# backward compatibility: 'git config --bool' returns true/false1769return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1770}elsif($typeeq'int') {1771return config_to_int($config{"gitweb.$key"});1772}1773return$config{"gitweb.$key"};1774}17751776# get hash of given path at given ref1777sub git_get_hash_by_path {1778my$base=shift;1779my$path=shift||returnundef;1780my$type=shift;17811782$path=~ s,/+$,,;17831784open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1785or die_error(500,"Open git-ls-tree failed");1786my$line= <$fd>;1787close$fdorreturnundef;17881789if(!defined$line) {1790# there is no tree or hash given by $path at $base1791returnundef;1792}17931794#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1795$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1796if(defined$type&&$typene$2) {1797# type doesn't match1798returnundef;1799}1800return$3;1801}18021803# get path of entry with given hash at given tree-ish (ref)1804# used to get 'from' filename for combined diff (merge commit) for renames1805sub git_get_path_by_hash {1806my$base=shift||return;1807my$hash=shift||return;18081809local$/="\0";18101811open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base1812orreturnundef;1813while(my$line= <$fd>) {1814chomp$line;18151816#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'1817#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'1818if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {1819close$fd;1820return$1;1821}1822}1823close$fd;1824returnundef;1825}18261827## ......................................................................1828## git utility functions, directly accessing git repository18291830sub git_get_project_description {1831my$path=shift;18321833$git_dir="$projectroot/$path";1834open my$fd,"$git_dir/description"1835orreturn git_get_project_config('description');1836my$descr= <$fd>;1837close$fd;1838if(defined$descr) {1839chomp$descr;1840}1841return$descr;1842}18431844sub git_get_project_ctags {1845my$path=shift;1846my$ctags= {};18471848$git_dir="$projectroot/$path";1849foreach(<$git_dir/ctags/*>) {1850open CT,$_ornext;1851my$val= <CT>;1852chomp$val;1853close CT;1854my$ctag=$_;$ctag=~ s#.*/##;1855$ctags->{$ctag} =$val;1856}1857$ctags;1858}18591860sub git_populate_project_tagcloud {1861my$ctags=shift;18621863# First, merge different-cased tags; tags vote on casing1864my%ctags_lc;1865foreach(keys%$ctags) {1866$ctags_lc{lc$_}->{count} +=$ctags->{$_};1867if(not$ctags_lc{lc$_}->{topcount}1868or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {1869$ctags_lc{lc$_}->{topcount} =$ctags->{$_};1870$ctags_lc{lc$_}->{topname} =$_;1871}1872}18731874my$cloud;1875if(eval{require HTML::TagCloud;1; }) {1876$cloud= HTML::TagCloud->new;1877foreach(sort keys%ctags_lc) {1878# Pad the title with spaces so that the cloud looks1879# less crammed.1880my$title=$ctags_lc{$_}->{topname};1881$title=~s/ / /g;1882$title=~s/^/ /g;1883$title=~s/$/ /g;1884$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});1885}1886}else{1887$cloud= \%ctags_lc;1888}1889$cloud;1890}18911892sub git_show_project_tagcloud {1893my($cloud,$count) =@_;1894print STDERR ref($cloud)."..\n";1895if(ref$cloudeq'HTML::TagCloud') {1896return$cloud->html_and_css($count);1897}else{1898my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;1899return'<p align="center">'.join(', ',map{1900"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"1901}splice(@tags,0,$count)) .'</p>';1902}1903}19041905sub git_get_project_url_list {1906my$path=shift;19071908$git_dir="$projectroot/$path";1909open my$fd,"$git_dir/cloneurl"1910orreturnwantarray?1911@{ config_to_multi(git_get_project_config('url')) } :1912 config_to_multi(git_get_project_config('url'));1913my@git_project_url_list=map{chomp;$_} <$fd>;1914close$fd;19151916returnwantarray?@git_project_url_list: \@git_project_url_list;1917}19181919sub git_get_projects_list {1920my($filter) =@_;1921my@list;19221923$filter||='';1924$filter=~s/\.git$//;19251926my($check_forks) = gitweb_check_feature('forks');19271928if(-d $projects_list) {1929# search in directory1930my$dir=$projects_list. ($filter?"/$filter":'');1931# remove the trailing "/"1932$dir=~s!/+$!!;1933my$pfxlen=length("$dir");1934my$pfxdepth= ($dir=~tr!/!!);19351936 File::Find::find({1937 follow_fast =>1,# follow symbolic links1938 follow_skip =>2,# ignore duplicates1939 dangling_symlinks =>0,# ignore dangling symlinks, silently1940 wanted =>sub{1941# skip project-list toplevel, if we get it.1942return if(m!^[/.]$!);1943# only directories can be git repositories1944return unless(-d $_);1945# don't traverse too deep (Find is super slow on os x)1946if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {1947$File::Find::prune =1;1948return;1949}19501951my$subdir=substr($File::Find::name,$pfxlen+1);1952# we check related file in $projectroot1953if(check_export_ok("$projectroot/$filter/$subdir")) {1954push@list, { path => ($filter?"$filter/":'') .$subdir};1955$File::Find::prune =1;1956}1957},1958},"$dir");19591960}elsif(-f $projects_list) {1961# read from file(url-encoded):1962# 'git%2Fgit.git Linus+Torvalds'1963# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'1964# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'1965my%paths;1966open my($fd),$projects_listorreturn;1967 PROJECT:1968while(my$line= <$fd>) {1969chomp$line;1970my($path,$owner) =split' ',$line;1971$path= unescape($path);1972$owner= unescape($owner);1973if(!defined$path) {1974next;1975}1976if($filterne'') {1977# looking for forks;1978my$pfx=substr($path,0,length($filter));1979if($pfxne$filter) {1980next PROJECT;1981}1982my$sfx=substr($path,length($filter));1983if($sfx!~/^\/.*\.git$/) {1984next PROJECT;1985}1986}elsif($check_forks) {1987 PATH:1988foreachmy$filter(keys%paths) {1989# looking for forks;1990my$pfx=substr($path,0,length($filter));1991if($pfxne$filter) {1992next PATH;1993}1994my$sfx=substr($path,length($filter));1995if($sfx!~/^\/.*\.git$/) {1996next PATH;1997}1998# is a fork, don't include it in1999# the list2000next PROJECT;2001}2002}2003if(check_export_ok("$projectroot/$path")) {2004my$pr= {2005 path =>$path,2006 owner => to_utf8($owner),2007};2008push@list,$pr;2009(my$forks_path=$path) =~s/\.git$//;2010$paths{$forks_path}++;2011}2012}2013close$fd;2014}2015return@list;2016}20172018our$gitweb_project_owner=undef;2019sub git_get_project_list_from_file {20202021return if(defined$gitweb_project_owner);20222023$gitweb_project_owner= {};2024# read from file (url-encoded):2025# 'git%2Fgit.git Linus+Torvalds'2026# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2027# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2028if(-f $projects_list) {2029open(my$fd,$projects_list);2030while(my$line= <$fd>) {2031chomp$line;2032my($pr,$ow) =split' ',$line;2033$pr= unescape($pr);2034$ow= unescape($ow);2035$gitweb_project_owner->{$pr} = to_utf8($ow);2036}2037close$fd;2038}2039}20402041sub git_get_project_owner {2042my$project=shift;2043my$owner;20442045returnundefunless$project;2046$git_dir="$projectroot/$project";20472048if(!defined$gitweb_project_owner) {2049 git_get_project_list_from_file();2050}20512052if(exists$gitweb_project_owner->{$project}) {2053$owner=$gitweb_project_owner->{$project};2054}2055if(!defined$owner){2056$owner= git_get_project_config('owner');2057}2058if(!defined$owner) {2059$owner= get_file_owner("$git_dir");2060}20612062return$owner;2063}20642065sub git_get_last_activity {2066my($path) =@_;2067my$fd;20682069$git_dir="$projectroot/$path";2070open($fd,"-|", git_cmd(),'for-each-ref',2071'--format=%(committer)',2072'--sort=-committerdate',2073'--count=1',2074'refs/heads')orreturn;2075my$most_recent= <$fd>;2076close$fdorreturn;2077if(defined$most_recent&&2078$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2079my$timestamp=$1;2080my$age=time-$timestamp;2081return($age, age_string($age));2082}2083return(undef,undef);2084}20852086sub git_get_references {2087my$type=shift||"";2088my%refs;2089# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112090# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2091open my$fd,"-|", git_cmd(),"show-ref","--dereference",2092($type? ("--","refs/$type") : ())# use -- <pattern> if $type2093orreturn;20942095while(my$line= <$fd>) {2096chomp$line;2097if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2098if(defined$refs{$1}) {2099push@{$refs{$1}},$2;2100}else{2101$refs{$1} = [$2];2102}2103}2104}2105close$fdorreturn;2106return \%refs;2107}21082109sub git_get_rev_name_tags {2110my$hash=shift||returnundef;21112112open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2113orreturn;2114my$name_rev= <$fd>;2115close$fd;21162117if($name_rev=~ m|^$hash tags/(.*)$|) {2118return$1;2119}else{2120# catches also '$hash undefined' output2121returnundef;2122}2123}21242125## ----------------------------------------------------------------------2126## parse to hash functions21272128sub parse_date {2129my$epoch=shift;2130my$tz=shift||"-0000";21312132my%date;2133my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2134my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2135my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2136$date{'hour'} =$hour;2137$date{'minute'} =$min;2138$date{'mday'} =$mday;2139$date{'day'} =$days[$wday];2140$date{'month'} =$months[$mon];2141$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2142$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2143$date{'mday-time'} =sprintf"%d%s%02d:%02d",2144$mday,$months[$mon],$hour,$min;2145$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",21461900+$year,1+$mon,$mday,$hour,$min,$sec;21472148$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2149my$local=$epoch+ ((int$1+ ($2/60)) *3600);2150($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2151$date{'hour_local'} =$hour;2152$date{'minute_local'} =$min;2153$date{'tz_local'} =$tz;2154$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",21551900+$year,$mon+1,$mday,2156$hour,$min,$sec,$tz);2157return%date;2158}21592160sub parse_tag {2161my$tag_id=shift;2162my%tag;2163my@comment;21642165open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2166$tag{'id'} =$tag_id;2167while(my$line= <$fd>) {2168chomp$line;2169if($line=~m/^object ([0-9a-fA-F]{40})$/) {2170$tag{'object'} =$1;2171}elsif($line=~m/^type (.+)$/) {2172$tag{'type'} =$1;2173}elsif($line=~m/^tag (.+)$/) {2174$tag{'name'} =$1;2175}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2176$tag{'author'} =$1;2177$tag{'epoch'} =$2;2178$tag{'tz'} =$3;2179}elsif($line=~m/--BEGIN/) {2180push@comment,$line;2181last;2182}elsif($lineeq"") {2183last;2184}2185}2186push@comment, <$fd>;2187$tag{'comment'} = \@comment;2188close$fdorreturn;2189if(!defined$tag{'name'}) {2190return2191};2192return%tag2193}21942195sub parse_commit_text {2196my($commit_text,$withparents) =@_;2197my@commit_lines=split'\n',$commit_text;2198my%co;21992200pop@commit_lines;# Remove '\0'22012202if(!@commit_lines) {2203return;2204}22052206my$header=shift@commit_lines;2207if($header!~m/^[0-9a-fA-F]{40}/) {2208return;2209}2210($co{'id'},my@parents) =split' ',$header;2211while(my$line=shift@commit_lines) {2212last if$lineeq"\n";2213if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2214$co{'tree'} =$1;2215}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2216push@parents,$1;2217}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2218$co{'author'} =$1;2219$co{'author_epoch'} =$2;2220$co{'author_tz'} =$3;2221if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2222$co{'author_name'} =$1;2223$co{'author_email'} =$2;2224}else{2225$co{'author_name'} =$co{'author'};2226}2227}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2228$co{'committer'} =$1;2229$co{'committer_epoch'} =$2;2230$co{'committer_tz'} =$3;2231$co{'committer_name'} =$co{'committer'};2232if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2233$co{'committer_name'} =$1;2234$co{'committer_email'} =$2;2235}else{2236$co{'committer_name'} =$co{'committer'};2237}2238}2239}2240if(!defined$co{'tree'}) {2241return;2242};2243$co{'parents'} = \@parents;2244$co{'parent'} =$parents[0];22452246foreachmy$title(@commit_lines) {2247$title=~s/^ //;2248if($titlene"") {2249$co{'title'} = chop_str($title,80,5);2250# remove leading stuff of merges to make the interesting part visible2251if(length($title) >50) {2252$title=~s/^Automatic //;2253$title=~s/^merge (of|with) /Merge ... /i;2254if(length($title) >50) {2255$title=~s/(http|rsync):\/\///;2256}2257if(length($title) >50) {2258$title=~s/(master|www|rsync)\.//;2259}2260if(length($title) >50) {2261$title=~s/kernel.org:?//;2262}2263if(length($title) >50) {2264$title=~s/\/pub\/scm//;2265}2266}2267$co{'title_short'} = chop_str($title,50,5);2268last;2269}2270}2271if(!defined$co{'title'} ||$co{'title'}eq"") {2272$co{'title'} =$co{'title_short'} ='(no commit message)';2273}2274# remove added spaces2275foreachmy$line(@commit_lines) {2276$line=~s/^ //;2277}2278$co{'comment'} = \@commit_lines;22792280my$age=time-$co{'committer_epoch'};2281$co{'age'} =$age;2282$co{'age_string'} = age_string($age);2283my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2284if($age>60*60*24*7*2) {2285$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2286$co{'age_string_age'} =$co{'age_string'};2287}else{2288$co{'age_string_date'} =$co{'age_string'};2289$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2290}2291return%co;2292}22932294sub parse_commit {2295my($commit_id) =@_;2296my%co;22972298local$/="\0";22992300open my$fd,"-|", git_cmd(),"rev-list",2301"--parents",2302"--header",2303"--max-count=1",2304$commit_id,2305"--",2306or die_error(500,"Open git-rev-list failed");2307%co= parse_commit_text(<$fd>,1);2308close$fd;23092310return%co;2311}23122313sub parse_commits {2314my($commit_id,$maxcount,$skip,$filename,@args) =@_;2315my@cos;23162317$maxcount||=1;2318$skip||=0;23192320local$/="\0";23212322open my$fd,"-|", git_cmd(),"rev-list",2323"--header",2324@args,2325("--max-count=".$maxcount),2326("--skip=".$skip),2327@extra_options,2328$commit_id,2329"--",2330($filename? ($filename) : ())2331or die_error(500,"Open git-rev-list failed");2332while(my$line= <$fd>) {2333my%co= parse_commit_text($line);2334push@cos, \%co;2335}2336close$fd;23372338returnwantarray?@cos: \@cos;2339}23402341# parse line of git-diff-tree "raw" output2342sub parse_difftree_raw_line {2343my$line=shift;2344my%res;23452346# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2347# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2348if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2349$res{'from_mode'} =$1;2350$res{'to_mode'} =$2;2351$res{'from_id'} =$3;2352$res{'to_id'} =$4;2353$res{'status'} =$5;2354$res{'similarity'} =$6;2355if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2356($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2357}else{2358$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2359}2360}2361# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2362# combined diff (for merge commit)2363elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2364$res{'nparents'} =length($1);2365$res{'from_mode'} = [split(' ',$2) ];2366$res{'to_mode'} =pop@{$res{'from_mode'}};2367$res{'from_id'} = [split(' ',$3) ];2368$res{'to_id'} =pop@{$res{'from_id'}};2369$res{'status'} = [split('',$4) ];2370$res{'to_file'} = unquote($5);2371}2372# 'c512b523472485aef4fff9e57b229d9d243c967f'2373elsif($line=~m/^([0-9a-fA-F]{40})$/) {2374$res{'commit'} =$1;2375}23762377returnwantarray?%res: \%res;2378}23792380# wrapper: return parsed line of git-diff-tree "raw" output2381# (the argument might be raw line, or parsed info)2382sub parsed_difftree_line {2383my$line_or_ref=shift;23842385if(ref($line_or_ref)eq"HASH") {2386# pre-parsed (or generated by hand)2387return$line_or_ref;2388}else{2389return parse_difftree_raw_line($line_or_ref);2390}2391}23922393# parse line of git-ls-tree output2394sub parse_ls_tree_line ($;%) {2395my$line=shift;2396my%opts=@_;2397my%res;23982399#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2400$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;24012402$res{'mode'} =$1;2403$res{'type'} =$2;2404$res{'hash'} =$3;2405if($opts{'-z'}) {2406$res{'name'} =$4;2407}else{2408$res{'name'} = unquote($4);2409}24102411returnwantarray?%res: \%res;2412}24132414# generates _two_ hashes, references to which are passed as 2 and 3 argument2415sub parse_from_to_diffinfo {2416my($diffinfo,$from,$to,@parents) =@_;24172418if($diffinfo->{'nparents'}) {2419# combined diff2420$from->{'file'} = [];2421$from->{'href'} = [];2422 fill_from_file_info($diffinfo,@parents)2423unlessexists$diffinfo->{'from_file'};2424for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2425$from->{'file'}[$i] =2426defined$diffinfo->{'from_file'}[$i] ?2427$diffinfo->{'from_file'}[$i] :2428$diffinfo->{'to_file'};2429if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2430$from->{'href'}[$i] = href(action=>"blob",2431 hash_base=>$parents[$i],2432 hash=>$diffinfo->{'from_id'}[$i],2433 file_name=>$from->{'file'}[$i]);2434}else{2435$from->{'href'}[$i] =undef;2436}2437}2438}else{2439# ordinary (not combined) diff2440$from->{'file'} =$diffinfo->{'from_file'};2441if($diffinfo->{'status'}ne"A") {# not new (added) file2442$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2443 hash=>$diffinfo->{'from_id'},2444 file_name=>$from->{'file'});2445}else{2446delete$from->{'href'};2447}2448}24492450$to->{'file'} =$diffinfo->{'to_file'};2451if(!is_deleted($diffinfo)) {# file exists in result2452$to->{'href'} = href(action=>"blob", hash_base=>$hash,2453 hash=>$diffinfo->{'to_id'},2454 file_name=>$to->{'file'});2455}else{2456delete$to->{'href'};2457}2458}24592460## ......................................................................2461## parse to array of hashes functions24622463sub git_get_heads_list {2464my$limit=shift;2465my@headslist;24662467open my$fd,'-|', git_cmd(),'for-each-ref',2468($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2469'--format=%(objectname) %(refname) %(subject)%00%(committer)',2470'refs/heads'2471orreturn;2472while(my$line= <$fd>) {2473my%ref_item;24742475chomp$line;2476my($refinfo,$committerinfo) =split(/\0/,$line);2477my($hash,$name,$title) =split(' ',$refinfo,3);2478my($committer,$epoch,$tz) =2479($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2480$ref_item{'fullname'} =$name;2481$name=~s!^refs/heads/!!;24822483$ref_item{'name'} =$name;2484$ref_item{'id'} =$hash;2485$ref_item{'title'} =$title||'(no commit message)';2486$ref_item{'epoch'} =$epoch;2487if($epoch) {2488$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2489}else{2490$ref_item{'age'} ="unknown";2491}24922493push@headslist, \%ref_item;2494}2495close$fd;24962497returnwantarray?@headslist: \@headslist;2498}24992500sub git_get_tags_list {2501my$limit=shift;2502my@tagslist;25032504open my$fd,'-|', git_cmd(),'for-each-ref',2505($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2506'--format=%(objectname) %(objecttype) %(refname) '.2507'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2508'refs/tags'2509orreturn;2510while(my$line= <$fd>) {2511my%ref_item;25122513chomp$line;2514my($refinfo,$creatorinfo) =split(/\0/,$line);2515my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2516my($creator,$epoch,$tz) =2517($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2518$ref_item{'fullname'} =$name;2519$name=~s!^refs/tags/!!;25202521$ref_item{'type'} =$type;2522$ref_item{'id'} =$id;2523$ref_item{'name'} =$name;2524if($typeeq"tag") {2525$ref_item{'subject'} =$title;2526$ref_item{'reftype'} =$reftype;2527$ref_item{'refid'} =$refid;2528}else{2529$ref_item{'reftype'} =$type;2530$ref_item{'refid'} =$id;2531}25322533if($typeeq"tag"||$typeeq"commit") {2534$ref_item{'epoch'} =$epoch;2535if($epoch) {2536$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2537}else{2538$ref_item{'age'} ="unknown";2539}2540}25412542push@tagslist, \%ref_item;2543}2544close$fd;25452546returnwantarray?@tagslist: \@tagslist;2547}25482549## ----------------------------------------------------------------------2550## filesystem-related functions25512552sub get_file_owner {2553my$path=shift;25542555my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2556my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2557if(!defined$gcos) {2558returnundef;2559}2560my$owner=$gcos;2561$owner=~s/[,;].*$//;2562return to_utf8($owner);2563}25642565## ......................................................................2566## mimetype related functions25672568sub mimetype_guess_file {2569my$filename=shift;2570my$mimemap=shift;2571-r $mimemaporreturnundef;25722573my%mimemap;2574open(MIME,$mimemap)orreturnundef;2575while(<MIME>) {2576next ifm/^#/;# skip comments2577my($mime,$exts) =split(/\t+/);2578if(defined$exts) {2579my@exts=split(/\s+/,$exts);2580foreachmy$ext(@exts) {2581$mimemap{$ext} =$mime;2582}2583}2584}2585close(MIME);25862587$filename=~/\.([^.]*)$/;2588return$mimemap{$1};2589}25902591sub mimetype_guess {2592my$filename=shift;2593my$mime;2594$filename=~/\./orreturnundef;25952596if($mimetypes_file) {2597my$file=$mimetypes_file;2598if($file!~m!^/!) {# if it is relative path2599# it is relative to project2600$file="$projectroot/$project/$file";2601}2602$mime= mimetype_guess_file($filename,$file);2603}2604$mime||= mimetype_guess_file($filename,'/etc/mime.types');2605return$mime;2606}26072608sub blob_mimetype {2609my$fd=shift;2610my$filename=shift;26112612if($filename) {2613my$mime= mimetype_guess($filename);2614$mimeandreturn$mime;2615}26162617# just in case2618return$default_blob_plain_mimetypeunless$fd;26192620if(-T $fd) {2621return'text/plain';2622}elsif(!$filename) {2623return'application/octet-stream';2624}elsif($filename=~m/\.png$/i) {2625return'image/png';2626}elsif($filename=~m/\.gif$/i) {2627return'image/gif';2628}elsif($filename=~m/\.jpe?g$/i) {2629return'image/jpeg';2630}else{2631return'application/octet-stream';2632}2633}26342635sub blob_contenttype {2636my($fd,$file_name,$type) =@_;26372638$type||= blob_mimetype($fd,$file_name);2639if($typeeq'text/plain'&&defined$default_text_plain_charset) {2640$type.="; charset=$default_text_plain_charset";2641}26422643return$type;2644}26452646## ======================================================================2647## functions printing HTML: header, footer, error page26482649sub git_header_html {2650my$status=shift||"200 OK";2651my$expires=shift;26522653my$title="$site_name";2654if(defined$project) {2655$title.=" - ". to_utf8($project);2656if(defined$action) {2657$title.="/$action";2658if(defined$file_name) {2659$title.=" - ". esc_path($file_name);2660if($actioneq"tree"&&$file_name!~ m|/$|) {2661$title.="/";2662}2663}2664}2665}2666my$content_type;2667# require explicit support from the UA if we are to send the page as2668# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2669# we have to do this because MSIE sometimes globs '*/*', pretending to2670# support xhtml+xml but choking when it gets what it asked for.2671if(defined$cgi->http('HTTP_ACCEPT') &&2672$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2673$cgi->Accept('application/xhtml+xml') !=0) {2674$content_type='application/xhtml+xml';2675}else{2676$content_type='text/html';2677}2678print$cgi->header(-type=>$content_type, -charset =>'utf-8',2679-status=>$status, -expires =>$expires);2680my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2681print<<EOF;2682<?xml version="1.0" encoding="utf-8"?>2683<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2684<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2685<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2686<!-- git core binaries version$git_version-->2687<head>2688<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2689<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2690<meta name="robots" content="index, nofollow"/>2691<title>$title</title>2692EOF2693# print out each stylesheet that exist2694if(defined$stylesheet) {2695#provides backwards capability for those people who define style sheet in a config file2696print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2697}else{2698foreachmy$stylesheet(@stylesheets) {2699next unless$stylesheet;2700print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2701}2702}2703if(defined$project) {2704my%href_params= get_feed_info();2705if(!exists$href_params{'-title'}) {2706$href_params{'-title'} ='log';2707}27082709foreachmy$formatqw(RSS Atom){2710my$type=lc($format);2711my%link_attr= (2712'-rel'=>'alternate',2713'-title'=>"$project-$href_params{'-title'} -$formatfeed",2714'-type'=>"application/$type+xml"2715);27162717$href_params{'action'} =$type;2718$link_attr{'-href'} = href(%href_params);2719print"<link ".2720"rel=\"$link_attr{'-rel'}\"".2721"title=\"$link_attr{'-title'}\"".2722"href=\"$link_attr{'-href'}\"".2723"type=\"$link_attr{'-type'}\"".2724"/>\n";27252726$href_params{'extra_options'} ='--no-merges';2727$link_attr{'-href'} = href(%href_params);2728$link_attr{'-title'} .=' (no merges)';2729print"<link ".2730"rel=\"$link_attr{'-rel'}\"".2731"title=\"$link_attr{'-title'}\"".2732"href=\"$link_attr{'-href'}\"".2733"type=\"$link_attr{'-type'}\"".2734"/>\n";2735}27362737}else{2738printf('<link rel="alternate" title="%sprojects list" '.2739'href="%s" type="text/plain; charset=utf-8" />'."\n",2740$site_name, href(project=>undef, action=>"project_index"));2741printf('<link rel="alternate" title="%sprojects feeds" '.2742'href="%s" type="text/x-opml" />'."\n",2743$site_name, href(project=>undef, action=>"opml"));2744}2745if(defined$favicon) {2746printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2747}27482749print"</head>\n".2750"<body>\n";27512752if(-f $site_header) {2753open(my$fd,$site_header);2754print<$fd>;2755close$fd;2756}27572758print"<div class=\"page_header\">\n".2759$cgi->a({-href => esc_url($logo_url),2760-title =>$logo_label},2761qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2762print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2763if(defined$project) {2764print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2765if(defined$action) {2766print" /$action";2767}2768print"\n";2769}2770print"</div>\n";27712772my($have_search) = gitweb_check_feature('search');2773if(defined$project&&$have_search) {2774if(!defined$searchtext) {2775$searchtext="";2776}2777my$search_hash;2778if(defined$hash_base) {2779$search_hash=$hash_base;2780}elsif(defined$hash) {2781$search_hash=$hash;2782}else{2783$search_hash="HEAD";2784}2785my$action=$my_uri;2786my($use_pathinfo) = gitweb_check_feature('pathinfo');2787if($use_pathinfo) {2788$action.="/".esc_url($project);2789}2790print$cgi->startform(-method=>"get", -action =>$action) .2791"<div class=\"search\">\n".2792(!$use_pathinfo&&2793$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .2794$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".2795$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".2796$cgi->popup_menu(-name =>'st', -default=>'commit',2797-values=> ['commit','grep','author','committer','pickaxe']) .2798$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .2799" search:\n",2800$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".2801"<span title=\"Extended regular expression\">".2802$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',2803-checked =>$search_use_regexp) .2804"</span>".2805"</div>".2806$cgi->end_form() ."\n";2807}2808}28092810sub git_footer_html {2811my$feed_class='rss_logo';28122813print"<div class=\"page_footer\">\n";2814if(defined$project) {2815my$descr= git_get_project_description($project);2816if(defined$descr) {2817print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";2818}28192820my%href_params= get_feed_info();2821if(!%href_params) {2822$feed_class.=' generic';2823}2824$href_params{'-title'} ||='log';28252826foreachmy$formatqw(RSS Atom){2827$href_params{'action'} =lc($format);2828print$cgi->a({-href => href(%href_params),2829-title =>"$href_params{'-title'}$formatfeed",2830-class=>$feed_class},$format)."\n";2831}28322833}else{2834print$cgi->a({-href => href(project=>undef, action=>"opml"),2835-class=>$feed_class},"OPML") ." ";2836print$cgi->a({-href => href(project=>undef, action=>"project_index"),2837-class=>$feed_class},"TXT") ."\n";2838}2839print"</div>\n";# class="page_footer"28402841if(-f $site_footer) {2842open(my$fd,$site_footer);2843print<$fd>;2844close$fd;2845}28462847print"</body>\n".2848"</html>";2849}28502851# die_error(<http_status_code>, <error_message>)2852# Example: die_error(404, 'Hash not found')2853# By convention, use the following status codes (as defined in RFC 2616):2854# 400: Invalid or missing CGI parameters, or2855# requested object exists but has wrong type.2856# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on2857# this server or project.2858# 404: Requested object/revision/project doesn't exist.2859# 500: The server isn't configured properly, or2860# an internal error occurred (e.g. failed assertions caused by bugs), or2861# an unknown error occurred (e.g. the git binary died unexpectedly).2862sub die_error {2863my$status=shift||500;2864my$error=shift||"Internal server error";28652866my%http_responses= (400=>'400 Bad Request',2867403=>'403 Forbidden',2868404=>'404 Not Found',2869500=>'500 Internal Server Error');2870 git_header_html($http_responses{$status});2871print<<EOF;2872<div class="page_body">2873<br /><br />2874$status-$error2875<br />2876</div>2877EOF2878 git_footer_html();2879exit;2880}28812882## ----------------------------------------------------------------------2883## functions printing or outputting HTML: navigation28842885sub git_print_page_nav {2886my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;2887$extra=''if!defined$extra;# pager or formats28882889my@navs=qw(summary shortlog log commit commitdiff tree);2890if($suppress) {2891@navs=grep{$_ne$suppress}@navs;2892}28932894my%arg=map{$_=> {action=>$_} }@navs;2895if(defined$head) {2896for(qw(commit commitdiff)) {2897$arg{$_}{'hash'} =$head;2898}2899if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {2900for(qw(shortlog log)) {2901$arg{$_}{'hash'} =$head;2902}2903}2904}29052906$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;2907$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;29082909my@actions= gitweb_check_feature('actions');2910while(@actions) {2911my($label,$link,$pos) = (shift(@actions),shift(@actions),shift(@actions));2912@navs=map{$_eq$pos? ($_,$label) :$_}@navs;2913# munch munch2914$link=~ s#%n#$project#g;2915$link=~ s#%f#$git_dir#g;2916$treehead?$link=~ s#%h#$treehead#g : $link =~ s#%h##g;2917$treebase?$link=~ s#%b#$treebase#g : $link =~ s#%b##g;2918$arg{$label}{'_href'} =$link;2919}29202921print"<div class=\"page_nav\">\n".2922(join" | ",2923map{$_eq$current?2924$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")2925}@navs);2926print"<br/>\n$extra<br/>\n".2927"</div>\n";2928}29292930sub format_paging_nav {2931my($action,$hash,$head,$page,$has_next_link) =@_;2932my$paging_nav;293329342935if($hashne$head||$page) {2936$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");2937}else{2938$paging_nav.="HEAD";2939}29402941if($page>0) {2942$paging_nav.=" ⋅ ".2943$cgi->a({-href => href(-replay=>1, page=>$page-1),2944-accesskey =>"p", -title =>"Alt-p"},"prev");2945}else{2946$paging_nav.=" ⋅ prev";2947}29482949if($has_next_link) {2950$paging_nav.=" ⋅ ".2951$cgi->a({-href => href(-replay=>1, page=>$page+1),2952-accesskey =>"n", -title =>"Alt-n"},"next");2953}else{2954$paging_nav.=" ⋅ next";2955}29562957return$paging_nav;2958}29592960## ......................................................................2961## functions printing or outputting HTML: div29622963sub git_print_header_div {2964my($action,$title,$hash,$hash_base) =@_;2965my%args= ();29662967$args{'action'} =$action;2968$args{'hash'} =$hashif$hash;2969$args{'hash_base'} =$hash_baseif$hash_base;29702971print"<div class=\"header\">\n".2972$cgi->a({-href => href(%args), -class=>"title"},2973$title?$title:$action) .2974"\n</div>\n";2975}29762977#sub git_print_authorship (\%) {2978sub git_print_authorship {2979my$co=shift;29802981my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});2982print"<div class=\"author_date\">".2983 esc_html($co->{'author_name'}) .2984" [$ad{'rfc2822'}";2985if($ad{'hour_local'} <6) {2986printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",2987$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});2988}else{2989printf(" (%02d:%02d%s)",2990$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});2991}2992print"]</div>\n";2993}29942995sub git_print_page_path {2996my$name=shift;2997my$type=shift;2998my$hb=shift;299930003001print"<div class=\"page_path\">";3002print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3003-title =>'tree root'}, to_utf8("[$project]"));3004print" / ";3005if(defined$name) {3006my@dirname=split'/',$name;3007my$basename=pop@dirname;3008my$fullname='';30093010foreachmy$dir(@dirname) {3011$fullname.= ($fullname?'/':'') .$dir;3012print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3013 hash_base=>$hb),3014-title =>$fullname}, esc_path($dir));3015print" / ";3016}3017if(defined$type&&$typeeq'blob') {3018print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3019 hash_base=>$hb),3020-title =>$name}, esc_path($basename));3021}elsif(defined$type&&$typeeq'tree') {3022print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3023 hash_base=>$hb),3024-title =>$name}, esc_path($basename));3025print" / ";3026}else{3027print esc_path($basename);3028}3029}3030print"<br/></div>\n";3031}30323033# sub git_print_log (\@;%) {3034sub git_print_log ($;%) {3035my$log=shift;3036my%opts=@_;30373038if($opts{'-remove_title'}) {3039# remove title, i.e. first line of log3040shift@$log;3041}3042# remove leading empty lines3043while(defined$log->[0] &&$log->[0]eq"") {3044shift@$log;3045}30463047# print log3048my$signoff=0;3049my$empty=0;3050foreachmy$line(@$log) {3051if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3052$signoff=1;3053$empty=0;3054if(!$opts{'-remove_signoff'}) {3055print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3056next;3057}else{3058# remove signoff lines3059next;3060}3061}else{3062$signoff=0;3063}30643065# print only one empty line3066# do not print empty line after signoff3067if($lineeq"") {3068next if($empty||$signoff);3069$empty=1;3070}else{3071$empty=0;3072}30733074print format_log_line_html($line) ."<br/>\n";3075}30763077if($opts{'-final_empty_line'}) {3078# end with single empty line3079print"<br/>\n"unless$empty;3080}3081}30823083# return link target (what link points to)3084sub git_get_link_target {3085my$hash=shift;3086my$link_target;30873088# read link3089open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3090orreturn;3091{3092local$/;3093$link_target= <$fd>;3094}3095close$fd3096orreturn;30973098return$link_target;3099}31003101# given link target, and the directory (basedir) the link is in,3102# return target of link relative to top directory (top tree);3103# return undef if it is not possible (including absolute links).3104sub normalize_link_target {3105my($link_target,$basedir,$hash_base) =@_;31063107# we can normalize symlink target only if $hash_base is provided3108return unless$hash_base;31093110# absolute symlinks (beginning with '/') cannot be normalized3111return if(substr($link_target,0,1)eq'/');31123113# normalize link target to path from top (root) tree (dir)3114my$path;3115if($basedir) {3116$path=$basedir.'/'.$link_target;3117}else{3118# we are in top (root) tree (dir)3119$path=$link_target;3120}31213122# remove //, /./, and /../3123my@path_parts;3124foreachmy$part(split('/',$path)) {3125# discard '.' and ''3126next if(!$part||$parteq'.');3127# handle '..'3128if($parteq'..') {3129if(@path_parts) {3130pop@path_parts;3131}else{3132# link leads outside repository (outside top dir)3133return;3134}3135}else{3136push@path_parts,$part;3137}3138}3139$path=join('/',@path_parts);31403141return$path;3142}31433144# print tree entry (row of git_tree), but without encompassing <tr> element3145sub git_print_tree_entry {3146my($t,$basedir,$hash_base,$have_blame) =@_;31473148my%base_key= ();3149$base_key{'hash_base'} =$hash_baseifdefined$hash_base;31503151# The format of a table row is: mode list link. Where mode is3152# the mode of the entry, list is the name of the entry, an href,3153# and link is the action links of the entry.31543155print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3156if($t->{'type'}eq"blob") {3157print"<td class=\"list\">".3158$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3159 file_name=>"$basedir$t->{'name'}",%base_key),3160-class=>"list"}, esc_path($t->{'name'}));3161if(S_ISLNK(oct$t->{'mode'})) {3162my$link_target= git_get_link_target($t->{'hash'});3163if($link_target) {3164my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3165if(defined$norm_target) {3166print" -> ".3167$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3168 file_name=>$norm_target),3169-title =>$norm_target}, esc_path($link_target));3170}else{3171print" -> ". esc_path($link_target);3172}3173}3174}3175print"</td>\n";3176print"<td class=\"link\">";3177print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3178 file_name=>"$basedir$t->{'name'}",%base_key)},3179"blob");3180if($have_blame) {3181print" | ".3182$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3183 file_name=>"$basedir$t->{'name'}",%base_key)},3184"blame");3185}3186if(defined$hash_base) {3187print" | ".3188$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3189 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3190"history");3191}3192print" | ".3193$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3194 file_name=>"$basedir$t->{'name'}")},3195"raw");3196print"</td>\n";31973198}elsif($t->{'type'}eq"tree") {3199print"<td class=\"list\">";3200print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3201 file_name=>"$basedir$t->{'name'}",%base_key)},3202 esc_path($t->{'name'}));3203print"</td>\n";3204print"<td class=\"link\">";3205print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3206 file_name=>"$basedir$t->{'name'}",%base_key)},3207"tree");3208if(defined$hash_base) {3209print" | ".3210$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3211 file_name=>"$basedir$t->{'name'}")},3212"history");3213}3214print"</td>\n";3215}else{3216# unknown object: we can only present history for it3217# (this includes 'commit' object, i.e. submodule support)3218print"<td class=\"list\">".3219 esc_path($t->{'name'}) .3220"</td>\n";3221print"<td class=\"link\">";3222if(defined$hash_base) {3223print$cgi->a({-href => href(action=>"history",3224 hash_base=>$hash_base,3225 file_name=>"$basedir$t->{'name'}")},3226"history");3227}3228print"</td>\n";3229}3230}32313232## ......................................................................3233## functions printing large fragments of HTML32343235# get pre-image filenames for merge (combined) diff3236sub fill_from_file_info {3237my($diff,@parents) =@_;32383239$diff->{'from_file'} = [ ];3240$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3241for(my$i=0;$i<$diff->{'nparents'};$i++) {3242if($diff->{'status'}[$i]eq'R'||3243$diff->{'status'}[$i]eq'C') {3244$diff->{'from_file'}[$i] =3245 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3246}3247}32483249return$diff;3250}32513252# is current raw difftree line of file deletion3253sub is_deleted {3254my$diffinfo=shift;32553256return$diffinfo->{'to_id'}eq('0' x 40);3257}32583259# does patch correspond to [previous] difftree raw line3260# $diffinfo - hashref of parsed raw diff format3261# $patchinfo - hashref of parsed patch diff format3262# (the same keys as in $diffinfo)3263sub is_patch_split {3264my($diffinfo,$patchinfo) =@_;32653266returndefined$diffinfo&&defined$patchinfo3267&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3268}326932703271sub git_difftree_body {3272my($difftree,$hash,@parents) =@_;3273my($parent) =$parents[0];3274my($have_blame) = gitweb_check_feature('blame');3275print"<div class=\"list_head\">\n";3276if($#{$difftree} >10) {3277print(($#{$difftree} +1) ." files changed:\n");3278}3279print"</div>\n";32803281print"<table class=\"".3282(@parents>1?"combined ":"") .3283"diff_tree\">\n";32843285# header only for combined diff in 'commitdiff' view3286my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3287if($has_header) {3288# table header3289print"<thead><tr>\n".3290"<th></th><th></th>\n";# filename, patchN link3291for(my$i=0;$i<@parents;$i++) {3292my$par=$parents[$i];3293print"<th>".3294$cgi->a({-href => href(action=>"commitdiff",3295 hash=>$hash, hash_parent=>$par),3296-title =>'commitdiff to parent number '.3297($i+1) .': '.substr($par,0,7)},3298$i+1) .3299" </th>\n";3300}3301print"</tr></thead>\n<tbody>\n";3302}33033304my$alternate=1;3305my$patchno=0;3306foreachmy$line(@{$difftree}) {3307my$diff= parsed_difftree_line($line);33083309if($alternate) {3310print"<tr class=\"dark\">\n";3311}else{3312print"<tr class=\"light\">\n";3313}3314$alternate^=1;33153316if(exists$diff->{'nparents'}) {# combined diff33173318 fill_from_file_info($diff,@parents)3319unlessexists$diff->{'from_file'};33203321if(!is_deleted($diff)) {3322# file exists in the result (child) commit3323print"<td>".3324$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3325 file_name=>$diff->{'to_file'},3326 hash_base=>$hash),3327-class=>"list"}, esc_path($diff->{'to_file'})) .3328"</td>\n";3329}else{3330print"<td>".3331 esc_path($diff->{'to_file'}) .3332"</td>\n";3333}33343335if($actioneq'commitdiff') {3336# link to patch3337$patchno++;3338print"<td class=\"link\">".3339$cgi->a({-href =>"#patch$patchno"},"patch") .3340" | ".3341"</td>\n";3342}33433344my$has_history=0;3345my$not_deleted=0;3346for(my$i=0;$i<$diff->{'nparents'};$i++) {3347my$hash_parent=$parents[$i];3348my$from_hash=$diff->{'from_id'}[$i];3349my$from_path=$diff->{'from_file'}[$i];3350my$status=$diff->{'status'}[$i];33513352$has_history||= ($statusne'A');3353$not_deleted||= ($statusne'D');33543355if($statuseq'A') {3356print"<td class=\"link\"align=\"right\"> | </td>\n";3357}elsif($statuseq'D') {3358print"<td class=\"link\">".3359$cgi->a({-href => href(action=>"blob",3360 hash_base=>$hash,3361 hash=>$from_hash,3362 file_name=>$from_path)},3363"blob". ($i+1)) .3364" | </td>\n";3365}else{3366if($diff->{'to_id'}eq$from_hash) {3367print"<td class=\"link nochange\">";3368}else{3369print"<td class=\"link\">";3370}3371print$cgi->a({-href => href(action=>"blobdiff",3372 hash=>$diff->{'to_id'},3373 hash_parent=>$from_hash,3374 hash_base=>$hash,3375 hash_parent_base=>$hash_parent,3376 file_name=>$diff->{'to_file'},3377 file_parent=>$from_path)},3378"diff". ($i+1)) .3379" | </td>\n";3380}3381}33823383print"<td class=\"link\">";3384if($not_deleted) {3385print$cgi->a({-href => href(action=>"blob",3386 hash=>$diff->{'to_id'},3387 file_name=>$diff->{'to_file'},3388 hash_base=>$hash)},3389"blob");3390print" | "if($has_history);3391}3392if($has_history) {3393print$cgi->a({-href => href(action=>"history",3394 file_name=>$diff->{'to_file'},3395 hash_base=>$hash)},3396"history");3397}3398print"</td>\n";33993400print"</tr>\n";3401next;# instead of 'else' clause, to avoid extra indent3402}3403# else ordinary diff34043405my($to_mode_oct,$to_mode_str,$to_file_type);3406my($from_mode_oct,$from_mode_str,$from_file_type);3407if($diff->{'to_mode'}ne('0' x 6)) {3408$to_mode_oct=oct$diff->{'to_mode'};3409if(S_ISREG($to_mode_oct)) {# only for regular file3410$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3411}3412$to_file_type= file_type($diff->{'to_mode'});3413}3414if($diff->{'from_mode'}ne('0' x 6)) {3415$from_mode_oct=oct$diff->{'from_mode'};3416if(S_ISREG($to_mode_oct)) {# only for regular file3417$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3418}3419$from_file_type= file_type($diff->{'from_mode'});3420}34213422if($diff->{'status'}eq"A") {# created3423my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3424$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3425$mode_chng.="]</span>";3426print"<td>";3427print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3428 hash_base=>$hash, file_name=>$diff->{'file'}),3429-class=>"list"}, esc_path($diff->{'file'}));3430print"</td>\n";3431print"<td>$mode_chng</td>\n";3432print"<td class=\"link\">";3433if($actioneq'commitdiff') {3434# link to patch3435$patchno++;3436print$cgi->a({-href =>"#patch$patchno"},"patch");3437print" | ";3438}3439print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3440 hash_base=>$hash, file_name=>$diff->{'file'})},3441"blob");3442print"</td>\n";34433444}elsif($diff->{'status'}eq"D") {# deleted3445my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3446print"<td>";3447print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3448 hash_base=>$parent, file_name=>$diff->{'file'}),3449-class=>"list"}, esc_path($diff->{'file'}));3450print"</td>\n";3451print"<td>$mode_chng</td>\n";3452print"<td class=\"link\">";3453if($actioneq'commitdiff') {3454# link to patch3455$patchno++;3456print$cgi->a({-href =>"#patch$patchno"},"patch");3457print" | ";3458}3459print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3460 hash_base=>$parent, file_name=>$diff->{'file'})},3461"blob") ." | ";3462if($have_blame) {3463print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3464 file_name=>$diff->{'file'})},3465"blame") ." | ";3466}3467print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3468 file_name=>$diff->{'file'})},3469"history");3470print"</td>\n";34713472}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3473my$mode_chnge="";3474if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3475$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3476if($from_file_typene$to_file_type) {3477$mode_chnge.=" from$from_file_typeto$to_file_type";3478}3479if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3480if($from_mode_str&&$to_mode_str) {3481$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3482}elsif($to_mode_str) {3483$mode_chnge.=" mode:$to_mode_str";3484}3485}3486$mode_chnge.="]</span>\n";3487}3488print"<td>";3489print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3490 hash_base=>$hash, file_name=>$diff->{'file'}),3491-class=>"list"}, esc_path($diff->{'file'}));3492print"</td>\n";3493print"<td>$mode_chnge</td>\n";3494print"<td class=\"link\">";3495if($actioneq'commitdiff') {3496# link to patch3497$patchno++;3498print$cgi->a({-href =>"#patch$patchno"},"patch") .3499" | ";3500}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3501# "commit" view and modified file (not onlu mode changed)3502print$cgi->a({-href => href(action=>"blobdiff",3503 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3504 hash_base=>$hash, hash_parent_base=>$parent,3505 file_name=>$diff->{'file'})},3506"diff") .3507" | ";3508}3509print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3510 hash_base=>$hash, file_name=>$diff->{'file'})},3511"blob") ." | ";3512if($have_blame) {3513print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3514 file_name=>$diff->{'file'})},3515"blame") ." | ";3516}3517print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3518 file_name=>$diff->{'file'})},3519"history");3520print"</td>\n";35213522}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3523my%status_name= ('R'=>'moved','C'=>'copied');3524my$nstatus=$status_name{$diff->{'status'}};3525my$mode_chng="";3526if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3527# mode also for directories, so we cannot use $to_mode_str3528$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3529}3530print"<td>".3531$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3532 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3533-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3534"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3535$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3536 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3537-class=>"list"}, esc_path($diff->{'from_file'})) .3538" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3539"<td class=\"link\">";3540if($actioneq'commitdiff') {3541# link to patch3542$patchno++;3543print$cgi->a({-href =>"#patch$patchno"},"patch") .3544" | ";3545}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3546# "commit" view and modified file (not only pure rename or copy)3547print$cgi->a({-href => href(action=>"blobdiff",3548 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3549 hash_base=>$hash, hash_parent_base=>$parent,3550 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3551"diff") .3552" | ";3553}3554print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3555 hash_base=>$parent, file_name=>$diff->{'to_file'})},3556"blob") ." | ";3557if($have_blame) {3558print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3559 file_name=>$diff->{'to_file'})},3560"blame") ." | ";3561}3562print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3563 file_name=>$diff->{'to_file'})},3564"history");3565print"</td>\n";35663567}# we should not encounter Unmerged (U) or Unknown (X) status3568print"</tr>\n";3569}3570print"</tbody>"if$has_header;3571print"</table>\n";3572}35733574sub git_patchset_body {3575my($fd,$difftree,$hash,@hash_parents) =@_;3576my($hash_parent) =$hash_parents[0];35773578my$is_combined= (@hash_parents>1);3579my$patch_idx=0;3580my$patch_number=0;3581my$patch_line;3582my$diffinfo;3583my$to_name;3584my(%from,%to);35853586print"<div class=\"patchset\">\n";35873588# skip to first patch3589while($patch_line= <$fd>) {3590chomp$patch_line;35913592last if($patch_line=~m/^diff /);3593}35943595 PATCH:3596while($patch_line) {35973598# parse "git diff" header line3599if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3600# $1 is from_name, which we do not use3601$to_name= unquote($2);3602$to_name=~s!^b/!!;3603}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3604# $1 is 'cc' or 'combined', which we do not use3605$to_name= unquote($2);3606}else{3607$to_name=undef;3608}36093610# check if current patch belong to current raw line3611# and parse raw git-diff line if needed3612if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3613# this is continuation of a split patch3614print"<div class=\"patch cont\">\n";3615}else{3616# advance raw git-diff output if needed3617$patch_idx++ifdefined$diffinfo;36183619# read and prepare patch information3620$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);36213622# compact combined diff output can have some patches skipped3623# find which patch (using pathname of result) we are at now;3624if($is_combined) {3625while($to_namene$diffinfo->{'to_file'}) {3626print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3627 format_diff_cc_simplified($diffinfo,@hash_parents) .3628"</div>\n";# class="patch"36293630$patch_idx++;3631$patch_number++;36323633last if$patch_idx>$#$difftree;3634$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3635}3636}36373638# modifies %from, %to hashes3639 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);36403641# this is first patch for raw difftree line with $patch_idx index3642# we index @$difftree array from 0, but number patches from 13643print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3644}36453646# git diff header3647#assert($patch_line =~ m/^diff /) if DEBUG;3648#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3649$patch_number++;3650# print "git diff" header3651print format_git_diff_header_line($patch_line,$diffinfo,3652 \%from, \%to);36533654# print extended diff header3655print"<div class=\"diff extended_header\">\n";3656 EXTENDED_HEADER:3657while($patch_line= <$fd>) {3658chomp$patch_line;36593660last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);36613662print format_extended_diff_header_line($patch_line,$diffinfo,3663 \%from, \%to);3664}3665print"</div>\n";# class="diff extended_header"36663667# from-file/to-file diff header3668if(!$patch_line) {3669print"</div>\n";# class="patch"3670last PATCH;3671}3672next PATCH if($patch_line=~m/^diff /);3673#assert($patch_line =~ m/^---/) if DEBUG;36743675my$last_patch_line=$patch_line;3676$patch_line= <$fd>;3677chomp$patch_line;3678#assert($patch_line =~ m/^\+\+\+/) if DEBUG;36793680print format_diff_from_to_header($last_patch_line,$patch_line,3681$diffinfo, \%from, \%to,3682@hash_parents);36833684# the patch itself3685 LINE:3686while($patch_line= <$fd>) {3687chomp$patch_line;36883689next PATCH if($patch_line=~m/^diff /);36903691print format_diff_line($patch_line, \%from, \%to);3692}36933694}continue{3695print"</div>\n";# class="patch"3696}36973698# for compact combined (--cc) format, with chunk and patch simpliciaction3699# patchset might be empty, but there might be unprocessed raw lines3700for(++$patch_idxif$patch_number>0;3701$patch_idx<@$difftree;3702++$patch_idx) {3703# read and prepare patch information3704$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);37053706# generate anchor for "patch" links in difftree / whatchanged part3707print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3708 format_diff_cc_simplified($diffinfo,@hash_parents) .3709"</div>\n";# class="patch"37103711$patch_number++;3712}37133714if($patch_number==0) {3715if(@hash_parents>1) {3716print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3717}else{3718print"<div class=\"diff nodifferences\">No differences found</div>\n";3719}3720}37213722print"</div>\n";# class="patchset"3723}37243725# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37263727# fills project list info (age, description, owner, forks) for each3728# project in the list, removing invalid projects from returned list3729# NOTE: modifies $projlist, but does not remove entries from it3730sub fill_project_list_info {3731my($projlist,$check_forks) =@_;3732my@projects;37333734my$show_ctags= gitweb_check_feature('ctags');3735 PROJECT:3736foreachmy$pr(@$projlist) {3737my(@activity) = git_get_last_activity($pr->{'path'});3738unless(@activity) {3739next PROJECT;3740}3741($pr->{'age'},$pr->{'age_string'}) =@activity;3742if(!defined$pr->{'descr'}) {3743my$descr= git_get_project_description($pr->{'path'}) ||"";3744$descr= to_utf8($descr);3745$pr->{'descr_long'} =$descr;3746$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3747}3748if(!defined$pr->{'owner'}) {3749$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3750}3751if($check_forks) {3752my$pname=$pr->{'path'};3753if(($pname=~s/\.git$//) &&3754($pname!~/\/$/) &&3755(-d "$projectroot/$pname")) {3756$pr->{'forks'} ="-d$projectroot/$pname";3757}else{3758$pr->{'forks'} =0;3759}3760}3761$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3762push@projects,$pr;3763}37643765return@projects;3766}37673768# print 'sort by' <th> element, generating 'sort by $name' replay link3769# if that order is not selected3770sub print_sort_th {3771my($name,$order,$header) =@_;3772$header||=ucfirst($name);37733774if($ordereq$name) {3775print"<th>$header</th>\n";3776}else{3777print"<th>".3778$cgi->a({-href => href(-replay=>1, order=>$name),3779-class=>"header"},$header) .3780"</th>\n";3781}3782}37833784sub git_project_list_body {3785# actually uses global variable $project3786my($projlist,$order,$from,$to,$extra,$no_header) =@_;37873788my($check_forks) = gitweb_check_feature('forks');3789my@projects= fill_project_list_info($projlist,$check_forks);37903791$order||=$default_projects_order;3792$from=0unlessdefined$from;3793$to=$#projectsif(!defined$to||$#projects<$to);37943795my%order_info= (3796 project => { key =>'path', type =>'str'},3797 descr => { key =>'descr_long', type =>'str'},3798 owner => { key =>'owner', type =>'str'},3799 age => { key =>'age', type =>'num'}3800);3801my$oi=$order_info{$order};3802if($oi->{'type'}eq'str') {3803@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;3804}else{3805@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;3806}38073808my$show_ctags= gitweb_check_feature('ctags');3809if($show_ctags) {3810my%ctags;3811foreachmy$p(@projects) {3812foreachmy$ct(keys%{$p->{'ctags'}}) {3813$ctags{$ct} +=$p->{'ctags'}->{$ct};3814}3815}3816my$cloud= git_populate_project_tagcloud(\%ctags);3817print git_show_project_tagcloud($cloud,64);3818}38193820print"<table class=\"project_list\">\n";3821unless($no_header) {3822print"<tr>\n";3823if($check_forks) {3824print"<th></th>\n";3825}3826 print_sort_th('project',$order,'Project');3827 print_sort_th('descr',$order,'Description');3828 print_sort_th('owner',$order,'Owner');3829 print_sort_th('age',$order,'Last Change');3830print"<th></th>\n".# for links3831"</tr>\n";3832}3833my$alternate=1;3834my$tagfilter=$cgi->param('by_tag');3835for(my$i=$from;$i<=$to;$i++) {3836my$pr=$projects[$i];38373838next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};3839next if$searchtextand not$pr->{'path'} =~/$searchtext/3840and not$pr->{'descr_long'} =~/$searchtext/;3841# Weed out forks or non-matching entries of search3842if($check_forks) {3843my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;3844$forkbase="^$forkbase"if$forkbase;3845next ifnot$searchtextand not$tagfilterand$show_ctags3846and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe3847}38483849if($alternate) {3850print"<tr class=\"dark\">\n";3851}else{3852print"<tr class=\"light\">\n";3853}3854$alternate^=1;3855if($check_forks) {3856print"<td>";3857if($pr->{'forks'}) {3858print"<!--$pr->{'forks'} -->\n";3859print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");3860}3861print"</td>\n";3862}3863print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3864-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".3865"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3866-class=>"list", -title =>$pr->{'descr_long'}},3867 esc_html($pr->{'descr'})) ."</td>\n".3868"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";3869print"<td class=\"". age_class($pr->{'age'}) ."\">".3870(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".3871"<td class=\"link\">".3872$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".3873$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".3874$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".3875$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .3876($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .3877"</td>\n".3878"</tr>\n";3879}3880if(defined$extra) {3881print"<tr>\n";3882if($check_forks) {3883print"<td></td>\n";3884}3885print"<td colspan=\"5\">$extra</td>\n".3886"</tr>\n";3887}3888print"</table>\n";3889}38903891sub git_shortlog_body {3892# uses global variable $project3893my($commitlist,$from,$to,$refs,$extra) =@_;38943895$from=0unlessdefined$from;3896$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);38973898print"<table class=\"shortlog\">\n";3899my$alternate=1;3900for(my$i=$from;$i<=$to;$i++) {3901my%co= %{$commitlist->[$i]};3902my$commit=$co{'id'};3903my$ref= format_ref_marker($refs,$commit);3904if($alternate) {3905print"<tr class=\"dark\">\n";3906}else{3907print"<tr class=\"light\">\n";3908}3909$alternate^=1;3910my$author= chop_and_escape_str($co{'author_name'},10);3911# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .3912print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".3913"<td><i>".$author."</i></td>\n".3914"<td>";3915print format_subject_html($co{'title'},$co{'title_short'},3916 href(action=>"commit", hash=>$commit),$ref);3917print"</td>\n".3918"<td class=\"link\">".3919$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".3920$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".3921$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");3922my$snapshot_links= format_snapshot_links($commit);3923if(defined$snapshot_links) {3924print" | ".$snapshot_links;3925}3926print"</td>\n".3927"</tr>\n";3928}3929if(defined$extra) {3930print"<tr>\n".3931"<td colspan=\"4\">$extra</td>\n".3932"</tr>\n";3933}3934print"</table>\n";3935}39363937sub git_history_body {3938# Warning: assumes constant type (blob or tree) during history3939my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;39403941$from=0unlessdefined$from;3942$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});39433944print"<table class=\"history\">\n";3945my$alternate=1;3946for(my$i=$from;$i<=$to;$i++) {3947my%co= %{$commitlist->[$i]};3948if(!%co) {3949next;3950}3951my$commit=$co{'id'};39523953my$ref= format_ref_marker($refs,$commit);39543955if($alternate) {3956print"<tr class=\"dark\">\n";3957}else{3958print"<tr class=\"light\">\n";3959}3960$alternate^=1;3961# shortlog uses chop_str($co{'author_name'}, 10)3962my$author= chop_and_escape_str($co{'author_name'},15,3);3963print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".3964"<td><i>".$author."</i></td>\n".3965"<td>";3966# originally git_history used chop_str($co{'title'}, 50)3967print format_subject_html($co{'title'},$co{'title_short'},3968 href(action=>"commit", hash=>$commit),$ref);3969print"</td>\n".3970"<td class=\"link\">".3971$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".3972$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");39733974if($ftypeeq'blob') {3975my$blob_current= git_get_hash_by_path($hash_base,$file_name);3976my$blob_parent= git_get_hash_by_path($commit,$file_name);3977if(defined$blob_current&&defined$blob_parent&&3978$blob_currentne$blob_parent) {3979print" | ".3980$cgi->a({-href => href(action=>"blobdiff",3981 hash=>$blob_current, hash_parent=>$blob_parent,3982 hash_base=>$hash_base, hash_parent_base=>$commit,3983 file_name=>$file_name)},3984"diff to current");3985}3986}3987print"</td>\n".3988"</tr>\n";3989}3990if(defined$extra) {3991print"<tr>\n".3992"<td colspan=\"4\">$extra</td>\n".3993"</tr>\n";3994}3995print"</table>\n";3996}39973998sub git_tags_body {3999# uses global variable $project4000my($taglist,$from,$to,$extra) =@_;4001$from=0unlessdefined$from;4002$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);40034004print"<table class=\"tags\">\n";4005my$alternate=1;4006for(my$i=$from;$i<=$to;$i++) {4007my$entry=$taglist->[$i];4008my%tag=%$entry;4009my$comment=$tag{'subject'};4010my$comment_short;4011if(defined$comment) {4012$comment_short= chop_str($comment,30,5);4013}4014if($alternate) {4015print"<tr class=\"dark\">\n";4016}else{4017print"<tr class=\"light\">\n";4018}4019$alternate^=1;4020if(defined$tag{'age'}) {4021print"<td><i>$tag{'age'}</i></td>\n";4022}else{4023print"<td></td>\n";4024}4025print"<td>".4026$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4027-class=>"list name"}, esc_html($tag{'name'})) .4028"</td>\n".4029"<td>";4030if(defined$comment) {4031print format_subject_html($comment,$comment_short,4032 href(action=>"tag", hash=>$tag{'id'}));4033}4034print"</td>\n".4035"<td class=\"selflink\">";4036if($tag{'type'}eq"tag") {4037print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4038}else{4039print" ";4040}4041print"</td>\n".4042"<td class=\"link\">"." | ".4043$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4044if($tag{'reftype'}eq"commit") {4045print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4046" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4047}elsif($tag{'reftype'}eq"blob") {4048print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4049}4050print"</td>\n".4051"</tr>";4052}4053if(defined$extra) {4054print"<tr>\n".4055"<td colspan=\"5\">$extra</td>\n".4056"</tr>\n";4057}4058print"</table>\n";4059}40604061sub git_heads_body {4062# uses global variable $project4063my($headlist,$head,$from,$to,$extra) =@_;4064$from=0unlessdefined$from;4065$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);40664067print"<table class=\"heads\">\n";4068my$alternate=1;4069for(my$i=$from;$i<=$to;$i++) {4070my$entry=$headlist->[$i];4071my%ref=%$entry;4072my$curr=$ref{'id'}eq$head;4073if($alternate) {4074print"<tr class=\"dark\">\n";4075}else{4076print"<tr class=\"light\">\n";4077}4078$alternate^=1;4079print"<td><i>$ref{'age'}</i></td>\n".4080($curr?"<td class=\"current_head\">":"<td>") .4081$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4082-class=>"list name"},esc_html($ref{'name'})) .4083"</td>\n".4084"<td class=\"link\">".4085$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4086$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4087$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4088"</td>\n".4089"</tr>";4090}4091if(defined$extra) {4092print"<tr>\n".4093"<td colspan=\"3\">$extra</td>\n".4094"</tr>\n";4095}4096print"</table>\n";4097}40984099sub git_search_grep_body {4100my($commitlist,$from,$to,$extra) =@_;4101$from=0unlessdefined$from;4102$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41034104print"<table class=\"commit_search\">\n";4105my$alternate=1;4106for(my$i=$from;$i<=$to;$i++) {4107my%co= %{$commitlist->[$i]};4108if(!%co) {4109next;4110}4111my$commit=$co{'id'};4112if($alternate) {4113print"<tr class=\"dark\">\n";4114}else{4115print"<tr class=\"light\">\n";4116}4117$alternate^=1;4118my$author= chop_and_escape_str($co{'author_name'},15,5);4119print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4120"<td><i>".$author."</i></td>\n".4121"<td>".4122$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4123-class=>"list subject"},4124 chop_and_escape_str($co{'title'},50) ."<br/>");4125my$comment=$co{'comment'};4126foreachmy$line(@$comment) {4127if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4128my($lead,$match,$trail) = ($1,$2,$3);4129$match= chop_str($match,70,5,'center');4130my$contextlen=int((80-length($match))/2);4131$contextlen=30if($contextlen>30);4132$lead= chop_str($lead,$contextlen,10,'left');4133$trail= chop_str($trail,$contextlen,10,'right');41344135$lead= esc_html($lead);4136$match= esc_html($match);4137$trail= esc_html($trail);41384139print"$lead<span class=\"match\">$match</span>$trail<br />";4140}4141}4142print"</td>\n".4143"<td class=\"link\">".4144$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4145" | ".4146$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4147" | ".4148$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4149print"</td>\n".4150"</tr>\n";4151}4152if(defined$extra) {4153print"<tr>\n".4154"<td colspan=\"3\">$extra</td>\n".4155"</tr>\n";4156}4157print"</table>\n";4158}41594160## ======================================================================4161## ======================================================================4162## actions41634164sub git_project_list {4165my$order=$input_params{'order'};4166if(defined$order&&$order!~m/none|project|descr|owner|age/) {4167 die_error(400,"Unknown order parameter");4168}41694170my@list= git_get_projects_list();4171if(!@list) {4172 die_error(404,"No projects found");4173}41744175 git_header_html();4176if(-f $home_text) {4177print"<div class=\"index_include\">\n";4178open(my$fd,$home_text);4179print<$fd>;4180close$fd;4181print"</div>\n";4182}4183print$cgi->startform(-method=>"get") .4184"<p class=\"projsearch\">Search:\n".4185$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4186"</p>".4187$cgi->end_form() ."\n";4188 git_project_list_body(\@list,$order);4189 git_footer_html();4190}41914192sub git_forks {4193my$order=$input_params{'order'};4194if(defined$order&&$order!~m/none|project|descr|owner|age/) {4195 die_error(400,"Unknown order parameter");4196}41974198my@list= git_get_projects_list($project);4199if(!@list) {4200 die_error(404,"No forks found");4201}42024203 git_header_html();4204 git_print_page_nav('','');4205 git_print_header_div('summary',"$projectforks");4206 git_project_list_body(\@list,$order);4207 git_footer_html();4208}42094210sub git_project_index {4211my@projects= git_get_projects_list($project);42124213print$cgi->header(4214-type =>'text/plain',4215-charset =>'utf-8',4216-content_disposition =>'inline; filename="index.aux"');42174218foreachmy$pr(@projects) {4219if(!exists$pr->{'owner'}) {4220$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4221}42224223my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4224# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4225$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4226$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4227$path=~s/ /\+/g;4228$owner=~s/ /\+/g;42294230print"$path$owner\n";4231}4232}42334234sub git_summary {4235my$descr= git_get_project_description($project) ||"none";4236my%co= parse_commit("HEAD");4237my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4238my$head=$co{'id'};42394240my$owner= git_get_project_owner($project);42414242my$refs= git_get_references();4243# These get_*_list functions return one more to allow us to see if4244# there are more ...4245my@taglist= git_get_tags_list(16);4246my@headlist= git_get_heads_list(16);4247my@forklist;4248my($check_forks) = gitweb_check_feature('forks');42494250if($check_forks) {4251@forklist= git_get_projects_list($project);4252}42534254 git_header_html();4255 git_print_page_nav('summary','',$head);42564257print"<div class=\"title\"> </div>\n";4258print"<table class=\"projects_list\">\n".4259"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4260"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4261if(defined$cd{'rfc2822'}) {4262print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4263}42644265# use per project git URL list in $projectroot/$project/cloneurl4266# or make project git URL from git base URL and project name4267my$url_tag="URL";4268my@url_list= git_get_project_url_list($project);4269@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4270foreachmy$git_url(@url_list) {4271next unless$git_url;4272print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4273$url_tag="";4274}42754276# Tag cloud4277my$show_ctags= (gitweb_check_feature('ctags'))[0];4278if($show_ctags) {4279my$ctags= git_get_project_ctags($project);4280my$cloud= git_populate_project_tagcloud($ctags);4281print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4282print"</td>\n<td>"unless%$ctags;4283print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4284print"</td>\n<td>"if%$ctags;4285print git_show_project_tagcloud($cloud,48);4286print"</td></tr>";4287}42884289print"</table>\n";42904291if(-s "$projectroot/$project/README.html") {4292if(open my$fd,"$projectroot/$project/README.html") {4293print"<div class=\"title\">readme</div>\n".4294"<div class=\"readme\">\n";4295print$_while(<$fd>);4296print"\n</div>\n";# class="readme"4297close$fd;4298}4299}43004301# we need to request one more than 16 (0..15) to check if4302# those 16 are all4303my@commitlist=$head? parse_commits($head,17) : ();4304if(@commitlist) {4305 git_print_header_div('shortlog');4306 git_shortlog_body(\@commitlist,0,15,$refs,4307$#commitlist<=15?undef:4308$cgi->a({-href => href(action=>"shortlog")},"..."));4309}43104311if(@taglist) {4312 git_print_header_div('tags');4313 git_tags_body(\@taglist,0,15,4314$#taglist<=15?undef:4315$cgi->a({-href => href(action=>"tags")},"..."));4316}43174318if(@headlist) {4319 git_print_header_div('heads');4320 git_heads_body(\@headlist,$head,0,15,4321$#headlist<=15?undef:4322$cgi->a({-href => href(action=>"heads")},"..."));4323}43244325if(@forklist) {4326 git_print_header_div('forks');4327 git_project_list_body(\@forklist,'age',0,15,4328$#forklist<=15?undef:4329$cgi->a({-href => href(action=>"forks")},"..."),4330'no_header');4331}43324333 git_footer_html();4334}43354336sub git_tag {4337my$head= git_get_head_hash($project);4338 git_header_html();4339 git_print_page_nav('','',$head,undef,$head);4340my%tag= parse_tag($hash);43414342if(!%tag) {4343 die_error(404,"Unknown tag object");4344}43454346 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4347print"<div class=\"title_text\">\n".4348"<table class=\"object_header\">\n".4349"<tr>\n".4350"<td>object</td>\n".4351"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4352$tag{'object'}) ."</td>\n".4353"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4354$tag{'type'}) ."</td>\n".4355"</tr>\n";4356if(defined($tag{'author'})) {4357my%ad= parse_date($tag{'epoch'},$tag{'tz'});4358print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4359print"<tr><td></td><td>".$ad{'rfc2822'} .4360sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4361"</td></tr>\n";4362}4363print"</table>\n\n".4364"</div>\n";4365print"<div class=\"page_body\">";4366my$comment=$tag{'comment'};4367foreachmy$line(@$comment) {4368chomp$line;4369print esc_html($line, -nbsp=>1) ."<br/>\n";4370}4371print"</div>\n";4372 git_footer_html();4373}43744375sub git_blame {4376my$fd;4377my$ftype;43784379 gitweb_check_feature('blame')4380or die_error(403,"Blame view not allowed");43814382 die_error(400,"No file name given")unless$file_name;4383$hash_base||= git_get_head_hash($project);4384 die_error(404,"Couldn't find base commit")unless($hash_base);4385my%co= parse_commit($hash_base)4386or die_error(404,"Commit not found");4387if(!defined$hash) {4388$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4389or die_error(404,"Error looking up file");4390}4391$ftype= git_get_type($hash);4392if($ftype!~"blob") {4393 die_error(400,"Object is not a blob");4394}4395open($fd,"-|", git_cmd(),"blame",'-p','--',4396$file_name,$hash_base)4397or die_error(500,"Open git-blame failed");4398 git_header_html();4399my$formats_nav=4400$cgi->a({-href => href(action=>"blob", -replay=>1)},4401"blob") .4402" | ".4403$cgi->a({-href => href(action=>"history", -replay=>1)},4404"history") .4405" | ".4406$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4407"HEAD");4408 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4409 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4410 git_print_page_path($file_name,$ftype,$hash_base);4411my@rev_color= (qw(light2 dark2));4412my$num_colors=scalar(@rev_color);4413my$current_color=0;4414my$last_rev;4415print<<HTML;4416<div class="page_body">4417<table class="blame">4418<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4419HTML4420my%metainfo= ();4421while(1) {4422$_= <$fd>;4423last unlessdefined$_;4424my($full_rev,$orig_lineno,$lineno,$group_size) =4425/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;4426if(!exists$metainfo{$full_rev}) {4427$metainfo{$full_rev} = {};4428}4429my$meta=$metainfo{$full_rev};4430while(<$fd>) {4431last if(s/^\t//);4432if(/^(\S+) (.*)$/) {4433$meta->{$1} =$2;4434}4435}4436my$data=$_;4437chomp$data;4438my$rev=substr($full_rev,0,8);4439my$author=$meta->{'author'};4440my%date= parse_date($meta->{'author-time'},4441$meta->{'author-tz'});4442my$date=$date{'iso-tz'};4443if($group_size) {4444$current_color= ++$current_color%$num_colors;4445}4446print"<tr class=\"$rev_color[$current_color]\">\n";4447if($group_size) {4448print"<td class=\"sha1\"";4449print" title=\"". esc_html($author) .",$date\"";4450print" rowspan=\"$group_size\""if($group_size>1);4451print">";4452print$cgi->a({-href => href(action=>"commit",4453 hash=>$full_rev,4454 file_name=>$file_name)},4455 esc_html($rev));4456print"</td>\n";4457}4458open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4459or die_error(500,"Open git-rev-parse failed");4460my$parent_commit= <$dd>;4461close$dd;4462chomp($parent_commit);4463my$blamed= href(action =>'blame',4464 file_name =>$meta->{'filename'},4465 hash_base =>$parent_commit);4466print"<td class=\"linenr\">";4467print$cgi->a({ -href =>"$blamed#l$orig_lineno",4468-id =>"l$lineno",4469-class=>"linenr"},4470 esc_html($lineno));4471print"</td>";4472print"<td class=\"pre\">". esc_html($data) ."</td>\n";4473print"</tr>\n";4474}4475print"</table>\n";4476print"</div>";4477close$fd4478or print"Reading blob failed\n";4479 git_footer_html();4480}44814482sub git_tags {4483my$head= git_get_head_hash($project);4484 git_header_html();4485 git_print_page_nav('','',$head,undef,$head);4486 git_print_header_div('summary',$project);44874488my@tagslist= git_get_tags_list();4489if(@tagslist) {4490 git_tags_body(\@tagslist);4491}4492 git_footer_html();4493}44944495sub git_heads {4496my$head= git_get_head_hash($project);4497 git_header_html();4498 git_print_page_nav('','',$head,undef,$head);4499 git_print_header_div('summary',$project);45004501my@headslist= git_get_heads_list();4502if(@headslist) {4503 git_heads_body(\@headslist,$head);4504}4505 git_footer_html();4506}45074508sub git_blob_plain {4509my$type=shift;4510my$expires;45114512if(!defined$hash) {4513if(defined$file_name) {4514my$base=$hash_base|| git_get_head_hash($project);4515$hash= git_get_hash_by_path($base,$file_name,"blob")4516or die_error(404,"Cannot find file");4517}else{4518 die_error(400,"No file name defined");4519}4520}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4521# blobs defined by non-textual hash id's can be cached4522$expires="+1d";4523}45244525open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4526or die_error(500,"Open git-cat-file blob '$hash' failed");45274528# content-type (can include charset)4529$type= blob_contenttype($fd,$file_name,$type);45304531# "save as" filename, even when no $file_name is given4532my$save_as="$hash";4533if(defined$file_name) {4534$save_as=$file_name;4535}elsif($type=~m/^text\//) {4536$save_as.='.txt';4537}45384539print$cgi->header(4540-type =>$type,4541-expires =>$expires,4542-content_disposition =>'inline; filename="'.$save_as.'"');4543undef$/;4544binmode STDOUT,':raw';4545print<$fd>;4546binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4547$/="\n";4548close$fd;4549}45504551sub git_blob {4552my$expires;45534554if(!defined$hash) {4555if(defined$file_name) {4556my$base=$hash_base|| git_get_head_hash($project);4557$hash= git_get_hash_by_path($base,$file_name,"blob")4558or die_error(404,"Cannot find file");4559}else{4560 die_error(400,"No file name defined");4561}4562}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4563# blobs defined by non-textual hash id's can be cached4564$expires="+1d";4565}45664567my($have_blame) = gitweb_check_feature('blame');4568open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4569or die_error(500,"Couldn't cat$file_name,$hash");4570my$mimetype= blob_mimetype($fd,$file_name);4571if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4572close$fd;4573return git_blob_plain($mimetype);4574}4575# we can have blame only for text/* mimetype4576$have_blame&&= ($mimetype=~m!^text/!);45774578 git_header_html(undef,$expires);4579my$formats_nav='';4580if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4581if(defined$file_name) {4582if($have_blame) {4583$formats_nav.=4584$cgi->a({-href => href(action=>"blame", -replay=>1)},4585"blame") .4586" | ";4587}4588$formats_nav.=4589$cgi->a({-href => href(action=>"history", -replay=>1)},4590"history") .4591" | ".4592$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4593"raw") .4594" | ".4595$cgi->a({-href => href(action=>"blob",4596 hash_base=>"HEAD", file_name=>$file_name)},4597"HEAD");4598}else{4599$formats_nav.=4600$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4601"raw");4602}4603 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4604 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4605}else{4606print"<div class=\"page_nav\">\n".4607"<br/><br/></div>\n".4608"<div class=\"title\">$hash</div>\n";4609}4610 git_print_page_path($file_name,"blob",$hash_base);4611print"<div class=\"page_body\">\n";4612if($mimetype=~m!^image/!) {4613print qq!<img type="$mimetype"!;4614if($file_name) {4615print qq! alt="$file_name" title="$file_name"!;4616}4617print qq! src="! .4618 href(action=>"blob_plain", hash=>$hash,4619 hash_base=>$hash_base, file_name=>$file_name) .4620 qq!"/>\n!;4621}else{4622my$nr;4623while(my$line= <$fd>) {4624chomp$line;4625$nr++;4626$line= untabify($line);4627printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4628$nr,$nr,$nr, esc_html($line, -nbsp=>1);4629}4630}4631close$fd4632or print"Reading blob failed.\n";4633print"</div>";4634 git_footer_html();4635}46364637sub git_tree {4638if(!defined$hash_base) {4639$hash_base="HEAD";4640}4641if(!defined$hash) {4642if(defined$file_name) {4643$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4644}else{4645$hash=$hash_base;4646}4647}4648 die_error(404,"No such tree")unlessdefined($hash);4649$/="\0";4650open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4651or die_error(500,"Open git-ls-tree failed");4652my@entries=map{chomp;$_} <$fd>;4653close$fdor die_error(404,"Reading tree failed");4654$/="\n";46554656my$refs= git_get_references();4657my$ref= format_ref_marker($refs,$hash_base);4658 git_header_html();4659my$basedir='';4660my($have_blame) = gitweb_check_feature('blame');4661if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4662my@views_nav= ();4663if(defined$file_name) {4664push@views_nav,4665$cgi->a({-href => href(action=>"history", -replay=>1)},4666"history"),4667$cgi->a({-href => href(action=>"tree",4668 hash_base=>"HEAD", file_name=>$file_name)},4669"HEAD"),4670}4671my$snapshot_links= format_snapshot_links($hash);4672if(defined$snapshot_links) {4673# FIXME: Should be available when we have no hash base as well.4674push@views_nav,$snapshot_links;4675}4676 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4677 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4678}else{4679undef$hash_base;4680print"<div class=\"page_nav\">\n";4681print"<br/><br/></div>\n";4682print"<div class=\"title\">$hash</div>\n";4683}4684if(defined$file_name) {4685$basedir=$file_name;4686if($basedirne''&&substr($basedir, -1)ne'/') {4687$basedir.='/';4688}4689 git_print_page_path($file_name,'tree',$hash_base);4690}4691print"<div class=\"page_body\">\n";4692print"<table class=\"tree\">\n";4693my$alternate=1;4694# '..' (top directory) link if possible4695if(defined$hash_base&&4696defined$file_name&&$file_name=~m![^/]+$!) {4697if($alternate) {4698print"<tr class=\"dark\">\n";4699}else{4700print"<tr class=\"light\">\n";4701}4702$alternate^=1;47034704my$up=$file_name;4705$up=~s!/?[^/]+$!!;4706undef$upunless$up;4707# based on git_print_tree_entry4708print'<td class="mode">'. mode_str('040000') ."</td>\n";4709print'<td class="list">';4710print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4711 file_name=>$up)},4712"..");4713print"</td>\n";4714print"<td class=\"link\"></td>\n";47154716print"</tr>\n";4717}4718foreachmy$line(@entries) {4719my%t= parse_ls_tree_line($line, -z =>1);47204721if($alternate) {4722print"<tr class=\"dark\">\n";4723}else{4724print"<tr class=\"light\">\n";4725}4726$alternate^=1;47274728 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);47294730print"</tr>\n";4731}4732print"</table>\n".4733"</div>";4734 git_footer_html();4735}47364737sub git_snapshot {4738my@supported_fmts= gitweb_check_feature('snapshot');4739@supported_fmts= filter_snapshot_fmts(@supported_fmts);47404741my$format=$input_params{'snapshot_format'};4742if(!@supported_fmts) {4743 die_error(403,"Snapshots not allowed");4744}4745# default to first supported snapshot format4746$format||=$supported_fmts[0];4747if($format!~m/^[a-z0-9]+$/) {4748 die_error(400,"Invalid snapshot format parameter");4749}elsif(!exists($known_snapshot_formats{$format})) {4750 die_error(400,"Unknown snapshot format");4751}elsif(!grep($_eq$format,@supported_fmts)) {4752 die_error(403,"Unsupported snapshot format");4753}47544755if(!defined$hash) {4756$hash= git_get_head_hash($project);4757}47584759my$name=$project;4760$name=~ s,([^/])/*\.git$,$1,;4761$name= basename($name);4762my$filename= to_utf8($name);4763$name=~s/\047/\047\\\047\047/g;4764my$cmd;4765$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4766$cmd= quote_command(4767 git_cmd(),'archive',4768"--format=$known_snapshot_formats{$format}{'format'}",4769"--prefix=$name/",$hash);4770if(exists$known_snapshot_formats{$format}{'compressor'}) {4771$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4772}47734774print$cgi->header(4775-type =>$known_snapshot_formats{$format}{'type'},4776-content_disposition =>'inline; filename="'."$filename".'"',4777-status =>'200 OK');47784779open my$fd,"-|",$cmd4780or die_error(500,"Execute git-archive failed");4781binmode STDOUT,':raw';4782print<$fd>;4783binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4784close$fd;4785}47864787sub git_log {4788my$head= git_get_head_hash($project);4789if(!defined$hash) {4790$hash=$head;4791}4792if(!defined$page) {4793$page=0;4794}4795my$refs= git_get_references();47964797my@commitlist= parse_commits($hash,101, (100*$page));47984799my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);48004801 git_header_html();4802 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);48034804if(!@commitlist) {4805my%co= parse_commit($hash);48064807 git_print_header_div('summary',$project);4808print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";4809}4810my$to= ($#commitlist>=99) ? (99) : ($#commitlist);4811for(my$i=0;$i<=$to;$i++) {4812my%co= %{$commitlist[$i]};4813next if!%co;4814my$commit=$co{'id'};4815my$ref= format_ref_marker($refs,$commit);4816my%ad= parse_date($co{'author_epoch'});4817 git_print_header_div('commit',4818"<span class=\"age\">$co{'age_string'}</span>".4819 esc_html($co{'title'}) .$ref,4820$commit);4821print"<div class=\"title_text\">\n".4822"<div class=\"log_link\">\n".4823$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4824" | ".4825$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4826" | ".4827$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4828"<br/>\n".4829"</div>\n".4830"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".4831"</div>\n";48324833print"<div class=\"log_body\">\n";4834 git_print_log($co{'comment'}, -final_empty_line=>1);4835print"</div>\n";4836}4837if($#commitlist>=100) {4838print"<div class=\"page_nav\">\n";4839print$cgi->a({-href => href(-replay=>1, page=>$page+1),4840-accesskey =>"n", -title =>"Alt-n"},"next");4841print"</div>\n";4842}4843 git_footer_html();4844}48454846sub git_commit {4847$hash||=$hash_base||"HEAD";4848my%co= parse_commit($hash)4849or die_error(404,"Unknown commit object");4850my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});4851my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});48524853my$parent=$co{'parent'};4854my$parents=$co{'parents'};# listref48554856# we need to prepare $formats_nav before any parameter munging4857my$formats_nav;4858if(!defined$parent) {4859# --root commitdiff4860$formats_nav.='(initial)';4861}elsif(@$parents==1) {4862# single parent commit4863$formats_nav.=4864'(parent: '.4865$cgi->a({-href => href(action=>"commit",4866 hash=>$parent)},4867 esc_html(substr($parent,0,7))) .4868')';4869}else{4870# merge commit4871$formats_nav.=4872'(merge: '.4873join(' ',map{4874$cgi->a({-href => href(action=>"commit",4875 hash=>$_)},4876 esc_html(substr($_,0,7)));4877}@$parents) .4878')';4879}48804881if(!defined$parent) {4882$parent="--root";4883}4884my@difftree;4885open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",4886@diff_opts,4887(@$parents<=1?$parent:'-c'),4888$hash,"--"4889or die_error(500,"Open git-diff-tree failed");4890@difftree=map{chomp;$_} <$fd>;4891close$fdor die_error(404,"Reading git-diff-tree failed");48924893# non-textual hash id's can be cached4894my$expires;4895if($hash=~m/^[0-9a-fA-F]{40}$/) {4896$expires="+1d";4897}4898my$refs= git_get_references();4899my$ref= format_ref_marker($refs,$co{'id'});49004901 git_header_html(undef,$expires);4902 git_print_page_nav('commit','',4903$hash,$co{'tree'},$hash,4904$formats_nav);49054906if(defined$co{'parent'}) {4907 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);4908}else{4909 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);4910}4911print"<div class=\"title_text\">\n".4912"<table class=\"object_header\">\n";4913print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".4914"<tr>".4915"<td></td><td>$ad{'rfc2822'}";4916if($ad{'hour_local'} <6) {4917printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",4918$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});4919}else{4920printf(" (%02d:%02d%s)",4921$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});4922}4923print"</td>".4924"</tr>\n";4925print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";4926print"<tr><td></td><td>$cd{'rfc2822'}".4927sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .4928"</td></tr>\n";4929print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";4930print"<tr>".4931"<td>tree</td>".4932"<td class=\"sha1\">".4933$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),4934class=>"list"},$co{'tree'}) .4935"</td>".4936"<td class=\"link\">".4937$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},4938"tree");4939my$snapshot_links= format_snapshot_links($hash);4940if(defined$snapshot_links) {4941print" | ".$snapshot_links;4942}4943print"</td>".4944"</tr>\n";49454946foreachmy$par(@$parents) {4947print"<tr>".4948"<td>parent</td>".4949"<td class=\"sha1\">".4950$cgi->a({-href => href(action=>"commit", hash=>$par),4951class=>"list"},$par) .4952"</td>".4953"<td class=\"link\">".4954$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .4955" | ".4956$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .4957"</td>".4958"</tr>\n";4959}4960print"</table>".4961"</div>\n";49624963print"<div class=\"page_body\">\n";4964 git_print_log($co{'comment'});4965print"</div>\n";49664967 git_difftree_body(\@difftree,$hash,@$parents);49684969 git_footer_html();4970}49714972sub git_object {4973# object is defined by:4974# - hash or hash_base alone4975# - hash_base and file_name4976my$type;49774978# - hash or hash_base alone4979if($hash|| ($hash_base&& !defined$file_name)) {4980my$object_id=$hash||$hash_base;49814982open my$fd,"-|", quote_command(4983 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'4984or die_error(404,"Object does not exist");4985$type= <$fd>;4986chomp$type;4987close$fd4988or die_error(404,"Object does not exist");49894990# - hash_base and file_name4991}elsif($hash_base&&defined$file_name) {4992$file_name=~ s,/+$,,;49934994system(git_cmd(),"cat-file",'-e',$hash_base) ==04995or die_error(404,"Base object does not exist");49964997# here errors should not hapen4998open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name4999or die_error(500,"Open git-ls-tree failed");5000my$line= <$fd>;5001close$fd;50025003#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5004unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5005 die_error(404,"File or directory for given base does not exist");5006}5007$type=$2;5008$hash=$3;5009}else{5010 die_error(400,"Not enough information to find object");5011}50125013print$cgi->redirect(-uri => href(action=>$type, -full=>1,5014 hash=>$hash, hash_base=>$hash_base,5015 file_name=>$file_name),5016-status =>'302 Found');5017}50185019sub git_blobdiff {5020my$format=shift||'html';50215022my$fd;5023my@difftree;5024my%diffinfo;5025my$expires;50265027# preparing $fd and %diffinfo for git_patchset_body5028# new style URI5029if(defined$hash_base&&defined$hash_parent_base) {5030if(defined$file_name) {5031# read raw output5032open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5033$hash_parent_base,$hash_base,5034"--", (defined$file_parent?$file_parent: ()),$file_name5035or die_error(500,"Open git-diff-tree failed");5036@difftree=map{chomp;$_} <$fd>;5037close$fd5038or die_error(404,"Reading git-diff-tree failed");5039@difftree5040or die_error(404,"Blob diff not found");50415042}elsif(defined$hash&&5043$hash=~/[0-9a-fA-F]{40}/) {5044# try to find filename from $hash50455046# read filtered raw output5047open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5048$hash_parent_base,$hash_base,"--"5049or die_error(500,"Open git-diff-tree failed");5050@difftree=5051# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5052# $hash == to_id5053grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5054map{chomp;$_} <$fd>;5055close$fd5056or die_error(404,"Reading git-diff-tree failed");5057@difftree5058or die_error(404,"Blob diff not found");50595060}else{5061 die_error(400,"Missing one of the blob diff parameters");5062}50635064if(@difftree>1) {5065 die_error(400,"Ambiguous blob diff specification");5066}50675068%diffinfo= parse_difftree_raw_line($difftree[0]);5069$file_parent||=$diffinfo{'from_file'} ||$file_name;5070$file_name||=$diffinfo{'to_file'};50715072$hash_parent||=$diffinfo{'from_id'};5073$hash||=$diffinfo{'to_id'};50745075# non-textual hash id's can be cached5076if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5077$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5078$expires='+1d';5079}50805081# open patch output5082open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5083'-p', ($formateq'html'?"--full-index": ()),5084$hash_parent_base,$hash_base,5085"--", (defined$file_parent?$file_parent: ()),$file_name5086or die_error(500,"Open git-diff-tree failed");5087}50885089# old/legacy style URI5090if(!%diffinfo&&# if new style URI failed5091defined$hash&&defined$hash_parent) {5092# fake git-diff-tree raw output5093$diffinfo{'from_mode'} =$diffinfo{'to_mode'} ="blob";5094$diffinfo{'from_id'} =$hash_parent;5095$diffinfo{'to_id'} =$hash;5096if(defined$file_name) {5097if(defined$file_parent) {5098$diffinfo{'status'} ='2';5099$diffinfo{'from_file'} =$file_parent;5100$diffinfo{'to_file'} =$file_name;5101}else{# assume not renamed5102$diffinfo{'status'} ='1';5103$diffinfo{'from_file'} =$file_name;5104$diffinfo{'to_file'} =$file_name;5105}5106}else{# no filename given5107$diffinfo{'status'} ='2';5108$diffinfo{'from_file'} =$hash_parent;5109$diffinfo{'to_file'} =$hash;5110}51115112# non-textual hash id's can be cached5113if($hash=~m/^[0-9a-fA-F]{40}$/&&5114$hash_parent=~m/^[0-9a-fA-F]{40}$/) {5115$expires='+1d';5116}51175118# open patch output5119open$fd,"-|", git_cmd(),"diff",@diff_opts,5120'-p', ($formateq'html'?"--full-index": ()),5121$hash_parent,$hash,"--"5122or die_error(500,"Open git-diff failed");5123}else{5124 die_error(400,"Missing one of the blob diff parameters")5125unless%diffinfo;5126}51275128# header5129if($formateq'html') {5130my$formats_nav=5131$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5132"raw");5133 git_header_html(undef,$expires);5134if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5135 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5136 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5137}else{5138print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5139print"<div class=\"title\">$hashvs$hash_parent</div>\n";5140}5141if(defined$file_name) {5142 git_print_page_path($file_name,"blob",$hash_base);5143}else{5144print"<div class=\"page_path\"></div>\n";5145}51465147}elsif($formateq'plain') {5148print$cgi->header(5149-type =>'text/plain',5150-charset =>'utf-8',5151-expires =>$expires,5152-content_disposition =>'inline; filename="'."$file_name".'.patch"');51535154print"X-Git-Url: ".$cgi->self_url() ."\n\n";51555156}else{5157 die_error(400,"Unknown blobdiff format");5158}51595160# patch5161if($formateq'html') {5162print"<div class=\"page_body\">\n";51635164 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5165close$fd;51665167print"</div>\n";# class="page_body"5168 git_footer_html();51695170}else{5171while(my$line= <$fd>) {5172$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5173$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;51745175print$line;51765177last if$line=~m!^\+\+\+!;5178}5179local$/=undef;5180print<$fd>;5181close$fd;5182}5183}51845185sub git_blobdiff_plain {5186 git_blobdiff('plain');5187}51885189sub git_commitdiff {5190my$format=shift||'html';5191$hash||=$hash_base||"HEAD";5192my%co= parse_commit($hash)5193or die_error(404,"Unknown commit object");51945195# choose format for commitdiff for merge5196if(!defined$hash_parent&& @{$co{'parents'}} >1) {5197$hash_parent='--cc';5198}5199# we need to prepare $formats_nav before almost any parameter munging5200my$formats_nav;5201if($formateq'html') {5202$formats_nav=5203$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5204"raw");52055206if(defined$hash_parent&&5207$hash_parentne'-c'&&$hash_parentne'--cc') {5208# commitdiff with two commits given5209my$hash_parent_short=$hash_parent;5210if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5211$hash_parent_short=substr($hash_parent,0,7);5212}5213$formats_nav.=5214' (from';5215for(my$i=0;$i< @{$co{'parents'}};$i++) {5216if($co{'parents'}[$i]eq$hash_parent) {5217$formats_nav.=' parent '. ($i+1);5218last;5219}5220}5221$formats_nav.=': '.5222$cgi->a({-href => href(action=>"commitdiff",5223 hash=>$hash_parent)},5224 esc_html($hash_parent_short)) .5225')';5226}elsif(!$co{'parent'}) {5227# --root commitdiff5228$formats_nav.=' (initial)';5229}elsif(scalar@{$co{'parents'}} ==1) {5230# single parent commit5231$formats_nav.=5232' (parent: '.5233$cgi->a({-href => href(action=>"commitdiff",5234 hash=>$co{'parent'})},5235 esc_html(substr($co{'parent'},0,7))) .5236')';5237}else{5238# merge commit5239if($hash_parenteq'--cc') {5240$formats_nav.=' | '.5241$cgi->a({-href => href(action=>"commitdiff",5242 hash=>$hash, hash_parent=>'-c')},5243'combined');5244}else{# $hash_parent eq '-c'5245$formats_nav.=' | '.5246$cgi->a({-href => href(action=>"commitdiff",5247 hash=>$hash, hash_parent=>'--cc')},5248'compact');5249}5250$formats_nav.=5251' (merge: '.5252join(' ',map{5253$cgi->a({-href => href(action=>"commitdiff",5254 hash=>$_)},5255 esc_html(substr($_,0,7)));5256} @{$co{'parents'}} ) .5257')';5258}5259}52605261my$hash_parent_param=$hash_parent;5262if(!defined$hash_parent_param) {5263# --cc for multiple parents, --root for parentless5264$hash_parent_param=5265@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5266}52675268# read commitdiff5269my$fd;5270my@difftree;5271if($formateq'html') {5272open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5273"--no-commit-id","--patch-with-raw","--full-index",5274$hash_parent_param,$hash,"--"5275or die_error(500,"Open git-diff-tree failed");52765277while(my$line= <$fd>) {5278chomp$line;5279# empty line ends raw part of diff-tree output5280last unless$line;5281push@difftree,scalar parse_difftree_raw_line($line);5282}52835284}elsif($formateq'plain') {5285open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5286'-p',$hash_parent_param,$hash,"--"5287or die_error(500,"Open git-diff-tree failed");52885289}else{5290 die_error(400,"Unknown commitdiff format");5291}52925293# non-textual hash id's can be cached5294my$expires;5295if($hash=~m/^[0-9a-fA-F]{40}$/) {5296$expires="+1d";5297}52985299# write commit message5300if($formateq'html') {5301my$refs= git_get_references();5302my$ref= format_ref_marker($refs,$co{'id'});53035304 git_header_html(undef,$expires);5305 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5306 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5307 git_print_authorship(\%co);5308print"<div class=\"page_body\">\n";5309if(@{$co{'comment'}} >1) {5310print"<div class=\"log\">\n";5311 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5312print"</div>\n";# class="log"5313}53145315}elsif($formateq'plain') {5316my$refs= git_get_references("tags");5317my$tagname= git_get_rev_name_tags($hash);5318my$filename= basename($project) ."-$hash.patch";53195320print$cgi->header(5321-type =>'text/plain',5322-charset =>'utf-8',5323-expires =>$expires,5324-content_disposition =>'inline; filename="'."$filename".'"');5325my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5326print"From: ". to_utf8($co{'author'}) ."\n";5327print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5328print"Subject: ". to_utf8($co{'title'}) ."\n";53295330print"X-Git-Tag:$tagname\n"if$tagname;5331print"X-Git-Url: ".$cgi->self_url() ."\n\n";53325333foreachmy$line(@{$co{'comment'}}) {5334print to_utf8($line) ."\n";5335}5336print"---\n\n";5337}53385339# write patch5340if($formateq'html') {5341my$use_parents= !defined$hash_parent||5342$hash_parenteq'-c'||$hash_parenteq'--cc';5343 git_difftree_body(\@difftree,$hash,5344$use_parents? @{$co{'parents'}} :$hash_parent);5345print"<br/>\n";53465347 git_patchset_body($fd, \@difftree,$hash,5348$use_parents? @{$co{'parents'}} :$hash_parent);5349close$fd;5350print"</div>\n";# class="page_body"5351 git_footer_html();53525353}elsif($formateq'plain') {5354local$/=undef;5355print<$fd>;5356close$fd5357or print"Reading git-diff-tree failed\n";5358}5359}53605361sub git_commitdiff_plain {5362 git_commitdiff('plain');5363}53645365sub git_history {5366if(!defined$hash_base) {5367$hash_base= git_get_head_hash($project);5368}5369if(!defined$page) {5370$page=0;5371}5372my$ftype;5373my%co= parse_commit($hash_base)5374or die_error(404,"Unknown commit object");53755376my$refs= git_get_references();5377my$limit=sprintf("--max-count=%i", (100* ($page+1)));53785379my@commitlist= parse_commits($hash_base,101, (100*$page),5380$file_name,"--full-history")5381or die_error(404,"No such file or directory on given branch");53825383if(!defined$hash&&defined$file_name) {5384# some commits could have deleted file in question,5385# and not have it in tree, but one of them has to have it5386for(my$i=0;$i<=@commitlist;$i++) {5387$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5388last ifdefined$hash;5389}5390}5391if(defined$hash) {5392$ftype= git_get_type($hash);5393}5394if(!defined$ftype) {5395 die_error(500,"Unknown type of object");5396}53975398my$paging_nav='';5399if($page>0) {5400$paging_nav.=5401$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5402 file_name=>$file_name)},5403"first");5404$paging_nav.=" ⋅ ".5405$cgi->a({-href => href(-replay=>1, page=>$page-1),5406-accesskey =>"p", -title =>"Alt-p"},"prev");5407}else{5408$paging_nav.="first";5409$paging_nav.=" ⋅ prev";5410}5411my$next_link='';5412if($#commitlist>=100) {5413$next_link=5414$cgi->a({-href => href(-replay=>1, page=>$page+1),5415-accesskey =>"n", -title =>"Alt-n"},"next");5416$paging_nav.=" ⋅$next_link";5417}else{5418$paging_nav.=" ⋅ next";5419}54205421 git_header_html();5422 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5423 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5424 git_print_page_path($file_name,$ftype,$hash_base);54255426 git_history_body(\@commitlist,0,99,5427$refs,$hash_base,$ftype,$next_link);54285429 git_footer_html();5430}54315432sub git_search {5433 gitweb_check_feature('search')or die_error(403,"Search is disabled");5434if(!defined$searchtext) {5435 die_error(400,"Text field is empty");5436}5437if(!defined$hash) {5438$hash= git_get_head_hash($project);5439}5440my%co= parse_commit($hash);5441if(!%co) {5442 die_error(404,"Unknown commit object");5443}5444if(!defined$page) {5445$page=0;5446}54475448$searchtype||='commit';5449if($searchtypeeq'pickaxe') {5450# pickaxe may take all resources of your box and run for several minutes5451# with every query - so decide by yourself how public you make this feature5452 gitweb_check_feature('pickaxe')5453or die_error(403,"Pickaxe is disabled");5454}5455if($searchtypeeq'grep') {5456 gitweb_check_feature('grep')5457or die_error(403,"Grep is disabled");5458}54595460 git_header_html();54615462if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5463my$greptype;5464if($searchtypeeq'commit') {5465$greptype="--grep=";5466}elsif($searchtypeeq'author') {5467$greptype="--author=";5468}elsif($searchtypeeq'committer') {5469$greptype="--committer=";5470}5471$greptype.=$searchtext;5472my@commitlist= parse_commits($hash,101, (100*$page),undef,5473$greptype,'--regexp-ignore-case',5474$search_use_regexp?'--extended-regexp':'--fixed-strings');54755476my$paging_nav='';5477if($page>0) {5478$paging_nav.=5479$cgi->a({-href => href(action=>"search", hash=>$hash,5480 searchtext=>$searchtext,5481 searchtype=>$searchtype)},5482"first");5483$paging_nav.=" ⋅ ".5484$cgi->a({-href => href(-replay=>1, page=>$page-1),5485-accesskey =>"p", -title =>"Alt-p"},"prev");5486}else{5487$paging_nav.="first";5488$paging_nav.=" ⋅ prev";5489}5490my$next_link='';5491if($#commitlist>=100) {5492$next_link=5493$cgi->a({-href => href(-replay=>1, page=>$page+1),5494-accesskey =>"n", -title =>"Alt-n"},"next");5495$paging_nav.=" ⋅$next_link";5496}else{5497$paging_nav.=" ⋅ next";5498}54995500if($#commitlist>=100) {5501}55025503 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5504 git_print_header_div('commit', esc_html($co{'title'}),$hash);5505 git_search_grep_body(\@commitlist,0,99,$next_link);5506}55075508if($searchtypeeq'pickaxe') {5509 git_print_page_nav('','',$hash,$co{'tree'},$hash);5510 git_print_header_div('commit', esc_html($co{'title'}),$hash);55115512print"<table class=\"pickaxe search\">\n";5513my$alternate=1;5514$/="\n";5515open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5516'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5517($search_use_regexp?'--pickaxe-regex': ());5518undef%co;5519my@files;5520while(my$line= <$fd>) {5521chomp$line;5522next unless$line;55235524my%set= parse_difftree_raw_line($line);5525if(defined$set{'commit'}) {5526# finish previous commit5527if(%co) {5528print"</td>\n".5529"<td class=\"link\">".5530$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5531" | ".5532$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5533print"</td>\n".5534"</tr>\n";5535}55365537if($alternate) {5538print"<tr class=\"dark\">\n";5539}else{5540print"<tr class=\"light\">\n";5541}5542$alternate^=1;5543%co= parse_commit($set{'commit'});5544my$author= chop_and_escape_str($co{'author_name'},15,5);5545print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5546"<td><i>$author</i></td>\n".5547"<td>".5548$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5549-class=>"list subject"},5550 chop_and_escape_str($co{'title'},50) ."<br/>");5551}elsif(defined$set{'to_id'}) {5552next if($set{'to_id'} =~m/^0{40}$/);55535554print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5555 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5556-class=>"list"},5557"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5558"<br/>\n";5559}5560}5561close$fd;55625563# finish last commit (warning: repetition!)5564if(%co) {5565print"</td>\n".5566"<td class=\"link\">".5567$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5568" | ".5569$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5570print"</td>\n".5571"</tr>\n";5572}55735574print"</table>\n";5575}55765577if($searchtypeeq'grep') {5578 git_print_page_nav('','',$hash,$co{'tree'},$hash);5579 git_print_header_div('commit', esc_html($co{'title'}),$hash);55805581print"<table class=\"grep_search\">\n";5582my$alternate=1;5583my$matches=0;5584$/="\n";5585open my$fd,"-|", git_cmd(),'grep','-n',5586$search_use_regexp? ('-E','-i') :'-F',5587$searchtext,$co{'tree'};5588my$lastfile='';5589while(my$line= <$fd>) {5590chomp$line;5591my($file,$lno,$ltext,$binary);5592last if($matches++>1000);5593if($line=~/^Binary file (.+) matches$/) {5594$file=$1;5595$binary=1;5596}else{5597(undef,$file,$lno,$ltext) =split(/:/,$line,4);5598}5599if($filene$lastfile) {5600$lastfileand print"</td></tr>\n";5601if($alternate++) {5602print"<tr class=\"dark\">\n";5603}else{5604print"<tr class=\"light\">\n";5605}5606print"<td class=\"list\">".5607$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5608 file_name=>"$file"),5609-class=>"list"}, esc_path($file));5610print"</td><td>\n";5611$lastfile=$file;5612}5613if($binary) {5614print"<div class=\"binary\">Binary file</div>\n";5615}else{5616$ltext= untabify($ltext);5617if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5618$ltext= esc_html($1, -nbsp=>1);5619$ltext.='<span class="match">';5620$ltext.= esc_html($2, -nbsp=>1);5621$ltext.='</span>';5622$ltext.= esc_html($3, -nbsp=>1);5623}else{5624$ltext= esc_html($ltext, -nbsp=>1);5625}5626print"<div class=\"pre\">".5627$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5628 file_name=>"$file").'#l'.$lno,5629-class=>"linenr"},sprintf('%4i',$lno))5630.' '.$ltext."</div>\n";5631}5632}5633if($lastfile) {5634print"</td></tr>\n";5635if($matches>1000) {5636print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5637}5638}else{5639print"<div class=\"diff nodifferences\">No matches found</div>\n";5640}5641close$fd;56425643print"</table>\n";5644}5645 git_footer_html();5646}56475648sub git_search_help {5649 git_header_html();5650 git_print_page_nav('','',$hash,$hash,$hash);5651print<<EOT;5652<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5653regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5654the pattern entered is recognized as the POSIX extended5655<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5656insensitive).</p>5657<dl>5658<dt><b>commit</b></dt>5659<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5660EOT5661my($have_grep) = gitweb_check_feature('grep');5662if($have_grep) {5663print<<EOT;5664<dt><b>grep</b></dt>5665<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5666 a different one) are searched for the given pattern. On large trees, this search can take5667a while and put some strain on the server, so please use it with some consideration. Note that5668due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5669case-sensitive.</dd>5670EOT5671}5672print<<EOT;5673<dt><b>author</b></dt>5674<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5675<dt><b>committer</b></dt>5676<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5677EOT5678my($have_pickaxe) = gitweb_check_feature('pickaxe');5679if($have_pickaxe) {5680print<<EOT;5681<dt><b>pickaxe</b></dt>5682<dd>All commits that caused the string to appear or disappear from any file (changes that5683added, removed or "modified" the string) will be listed. This search can take a while and5684takes a lot of strain on the server, so please use it wisely. Note that since you may be5685interested even in changes just changing the case as well, this search is case sensitive.</dd>5686EOT5687}5688print"</dl>\n";5689 git_footer_html();5690}56915692sub git_shortlog {5693my$head= git_get_head_hash($project);5694if(!defined$hash) {5695$hash=$head;5696}5697if(!defined$page) {5698$page=0;5699}5700my$refs= git_get_references();57015702my$commit_hash=$hash;5703if(defined$hash_parent) {5704$commit_hash="$hash_parent..$hash";5705}5706my@commitlist= parse_commits($commit_hash,101, (100*$page));57075708my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5709my$next_link='';5710if($#commitlist>=100) {5711$next_link=5712$cgi->a({-href => href(-replay=>1, page=>$page+1),5713-accesskey =>"n", -title =>"Alt-n"},"next");5714}57155716 git_header_html();5717 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5718 git_print_header_div('summary',$project);57195720 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);57215722 git_footer_html();5723}57245725## ......................................................................5726## feeds (RSS, Atom; OPML)57275728sub git_feed {5729my$format=shift||'atom';5730my($have_blame) = gitweb_check_feature('blame');57315732# Atom: http://www.atomenabled.org/developers/syndication/5733# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ5734if($formatne'rss'&&$formatne'atom') {5735 die_error(400,"Unknown web feed format");5736}57375738# log/feed of current (HEAD) branch, log of given branch, history of file/directory5739my$head=$hash||'HEAD';5740my@commitlist= parse_commits($head,150,0,$file_name);57415742my%latest_commit;5743my%latest_date;5744my$content_type="application/$format+xml";5745if(defined$cgi->http('HTTP_ACCEPT') &&5746$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {5747# browser (feed reader) prefers text/xml5748$content_type='text/xml';5749}5750if(defined($commitlist[0])) {5751%latest_commit= %{$commitlist[0]};5752%latest_date= parse_date($latest_commit{'author_epoch'});5753print$cgi->header(5754-type =>$content_type,5755-charset =>'utf-8',5756-last_modified =>$latest_date{'rfc2822'});5757}else{5758print$cgi->header(5759-type =>$content_type,5760-charset =>'utf-8');5761}57625763# Optimization: skip generating the body if client asks only5764# for Last-Modified date.5765return if($cgi->request_method()eq'HEAD');57665767# header variables5768my$title="$site_name-$project/$action";5769my$feed_type='log';5770if(defined$hash) {5771$title.=" - '$hash'";5772$feed_type='branch log';5773if(defined$file_name) {5774$title.=" ::$file_name";5775$feed_type='history';5776}5777}elsif(defined$file_name) {5778$title.=" -$file_name";5779$feed_type='history';5780}5781$title.="$feed_type";5782my$descr= git_get_project_description($project);5783if(defined$descr) {5784$descr= esc_html($descr);5785}else{5786$descr="$project".5787($formateq'rss'?'RSS':'Atom') .5788" feed";5789}5790my$owner= git_get_project_owner($project);5791$owner= esc_html($owner);57925793#header5794my$alt_url;5795if(defined$file_name) {5796$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);5797}elsif(defined$hash) {5798$alt_url= href(-full=>1, action=>"log", hash=>$hash);5799}else{5800$alt_url= href(-full=>1, action=>"summary");5801}5802print qq!<?xml version="1.0" encoding="utf-8"?>\n!;5803if($formateq'rss') {5804print<<XML;5805<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">5806<channel>5807XML5808print"<title>$title</title>\n".5809"<link>$alt_url</link>\n".5810"<description>$descr</description>\n".5811"<language>en</language>\n";5812}elsif($formateq'atom') {5813print<<XML;5814<feed xmlns="http://www.w3.org/2005/Atom">5815XML5816print"<title>$title</title>\n".5817"<subtitle>$descr</subtitle>\n".5818'<link rel="alternate" type="text/html" href="'.5819$alt_url.'" />'."\n".5820'<link rel="self" type="'.$content_type.'" href="'.5821$cgi->self_url() .'" />'."\n".5822"<id>". href(-full=>1) ."</id>\n".5823# use project owner for feed author5824"<author><name>$owner</name></author>\n";5825if(defined$favicon) {5826print"<icon>". esc_url($favicon) ."</icon>\n";5827}5828if(defined$logo_url) {5829# not twice as wide as tall: 72 x 27 pixels5830print"<logo>". esc_url($logo) ."</logo>\n";5831}5832if(!%latest_date) {5833# dummy date to keep the feed valid until commits trickle in:5834print"<updated>1970-01-01T00:00:00Z</updated>\n";5835}else{5836print"<updated>$latest_date{'iso-8601'}</updated>\n";5837}5838}58395840# contents5841for(my$i=0;$i<=$#commitlist;$i++) {5842my%co= %{$commitlist[$i]};5843my$commit=$co{'id'};5844# we read 150, we always show 30 and the ones more recent than 48 hours5845if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {5846last;5847}5848my%cd= parse_date($co{'author_epoch'});58495850# get list of changed files5851open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5852$co{'parent'} ||"--root",5853$co{'id'},"--", (defined$file_name?$file_name: ())5854ornext;5855my@difftree=map{chomp;$_} <$fd>;5856close$fd5857ornext;58585859# print element (entry, item)5860my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);5861if($formateq'rss') {5862print"<item>\n".5863"<title>". esc_html($co{'title'}) ."</title>\n".5864"<author>". esc_html($co{'author'}) ."</author>\n".5865"<pubDate>$cd{'rfc2822'}</pubDate>\n".5866"<guid isPermaLink=\"true\">$co_url</guid>\n".5867"<link>$co_url</link>\n".5868"<description>". esc_html($co{'title'}) ."</description>\n".5869"<content:encoded>".5870"<![CDATA[\n";5871}elsif($formateq'atom') {5872print"<entry>\n".5873"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".5874"<updated>$cd{'iso-8601'}</updated>\n".5875"<author>\n".5876" <name>". esc_html($co{'author_name'}) ."</name>\n";5877if($co{'author_email'}) {5878print" <email>". esc_html($co{'author_email'}) ."</email>\n";5879}5880print"</author>\n".5881# use committer for contributor5882"<contributor>\n".5883" <name>". esc_html($co{'committer_name'}) ."</name>\n";5884if($co{'committer_email'}) {5885print" <email>". esc_html($co{'committer_email'}) ."</email>\n";5886}5887print"</contributor>\n".5888"<published>$cd{'iso-8601'}</published>\n".5889"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".5890"<id>$co_url</id>\n".5891"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".5892"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";5893}5894my$comment=$co{'comment'};5895print"<pre>\n";5896foreachmy$line(@$comment) {5897$line= esc_html($line);5898print"$line\n";5899}5900print"</pre><ul>\n";5901foreachmy$difftree_line(@difftree) {5902my%difftree= parse_difftree_raw_line($difftree_line);5903next if!$difftree{'from_id'};59045905my$file=$difftree{'file'} ||$difftree{'to_file'};59065907print"<li>".5908"[".5909$cgi->a({-href => href(-full=>1, action=>"blobdiff",5910 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},5911 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},5912 file_name=>$file, file_parent=>$difftree{'from_file'}),5913-title =>"diff"},'D');5914if($have_blame) {5915print$cgi->a({-href => href(-full=>1, action=>"blame",5916 file_name=>$file, hash_base=>$commit),5917-title =>"blame"},'B');5918}5919# if this is not a feed of a file history5920if(!defined$file_name||$file_namene$file) {5921print$cgi->a({-href => href(-full=>1, action=>"history",5922 file_name=>$file, hash=>$commit),5923-title =>"history"},'H');5924}5925$file= esc_path($file);5926print"] ".5927"$file</li>\n";5928}5929if($formateq'rss') {5930print"</ul>]]>\n".5931"</content:encoded>\n".5932"</item>\n";5933}elsif($formateq'atom') {5934print"</ul>\n</div>\n".5935"</content>\n".5936"</entry>\n";5937}5938}59395940# end of feed5941if($formateq'rss') {5942print"</channel>\n</rss>\n";5943}elsif($formateq'atom') {5944print"</feed>\n";5945}5946}59475948sub git_rss {5949 git_feed('rss');5950}59515952sub git_atom {5953 git_feed('atom');5954}59555956sub git_opml {5957my@list= git_get_projects_list();59585959print$cgi->header(-type =>'text/xml', -charset =>'utf-8');5960print<<XML;5961<?xml version="1.0" encoding="utf-8"?>5962<opml version="1.0">5963<head>5964 <title>$site_nameOPML Export</title>5965</head>5966<body>5967<outline text="git RSS feeds">5968XML59695970foreachmy$pr(@list) {5971my%proj=%$pr;5972my$head= git_get_head_hash($proj{'path'});5973if(!defined$head) {5974next;5975}5976$git_dir="$projectroot/$proj{'path'}";5977my%co= parse_commit($head);5978if(!%co) {5979next;5980}59815982my$path= esc_html(chop_str($proj{'path'},25,5));5983my$rss="$my_url?p=$proj{'path'};a=rss";5984my$html="$my_url?p=$proj{'path'};a=summary";5985print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";5986}5987print<<XML;5988</outline>5989</body>5990</opml>5991XML5992}