gitweb.perl 248 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484
  1. #!/usr/bin/perl
  2. # gitweb - simple web interface to track changes in git repositories
  3. #
  4. # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
  5. # (C) 2005, Christian Gierke
  6. #
  7. # This program is licensed under the GPLv2
  8. use 5.008;
  9. use strict;
  10. use warnings;
  11. # handle ACL in file access tests
  12. use filetest 'access';
  13. use CGI qw(:standard :escapeHTML -nosticky);
  14. use CGI::Util qw(unescape);
  15. use CGI::Carp qw(fatalsToBrowser set_message);
  16. use Encode;
  17. use Fcntl ':mode';
  18. use File::Find qw();
  19. use File::Basename qw(basename);
  20. use Time::HiRes qw(gettimeofday tv_interval);
  21. use Digest::MD5 qw(md5_hex);
  22. binmode STDOUT, ':utf8';
  23. if (!defined($CGI::VERSION) || $CGI::VERSION < 4.08) {
  24. eval 'sub CGI::multi_param { CGI::param(@_) }'
  25. }
  26. our $t0 = [ gettimeofday() ];
  27. our $number_of_git_cmds = 0;
  28. BEGIN {
  29. CGI->compile() if $ENV{'MOD_PERL'};
  30. }
  31. our $version = "++GIT_VERSION++";
  32. our ($my_url, $my_uri, $base_url, $path_info, $home_link);
  33. sub evaluate_uri {
  34. our $cgi;
  35. our $my_url = $cgi->url();
  36. our $my_uri = $cgi->url(-absolute => 1);
  37. # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
  38. # needed and used only for URLs with nonempty PATH_INFO
  39. our $base_url = $my_url;
  40. # When the script is used as DirectoryIndex, the URL does not contain the name
  41. # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
  42. # have to do it ourselves. We make $path_info global because it's also used
  43. # later on.
  44. #
  45. # Another issue with the script being the DirectoryIndex is that the resulting
  46. # $my_url data is not the full script URL: this is good, because we want
  47. # generated links to keep implying the script name if it wasn't explicitly
  48. # indicated in the URL we're handling, but it means that $my_url cannot be used
  49. # as base URL.
  50. # Therefore, if we needed to strip PATH_INFO, then we know that we have
  51. # to build the base URL ourselves:
  52. our $path_info = decode_utf8($ENV{"PATH_INFO"});
  53. if ($path_info) {
  54. # $path_info has already been URL-decoded by the web server, but
  55. # $my_url and $my_uri have not. URL-decode them so we can properly
  56. # strip $path_info.
  57. $my_url = unescape($my_url);
  58. $my_uri = unescape($my_uri);
  59. if ($my_url =~ s,\Q$path_info\E$,, &&
  60. $my_uri =~ s,\Q$path_info\E$,, &&
  61. defined $ENV{'SCRIPT_NAME'}) {
  62. $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
  63. }
  64. }
  65. # target of the home link on top of all pages
  66. our $home_link = $my_uri || "/";
  67. }
  68. # core git executable to use
  69. # this can just be "git" if your webserver has a sensible PATH
  70. our $GIT = "++GIT_BINDIR++/git";
  71. # absolute fs-path which will be prepended to the project path
  72. #our $projectroot = "/pub/scm";
  73. our $projectroot = "++GITWEB_PROJECTROOT++";
  74. # fs traversing limit for getting project list
  75. # the number is relative to the projectroot
  76. our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
  77. # string of the home link on top of all pages
  78. our $home_link_str = "++GITWEB_HOME_LINK_STR++";
  79. # extra breadcrumbs preceding the home link
  80. our @extra_breadcrumbs = ();
  81. # name of your site or organization to appear in page titles
  82. # replace this with something more descriptive for clearer bookmarks
  83. our $site_name = "++GITWEB_SITENAME++"
  84. || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
  85. # html snippet to include in the <head> section of each page
  86. our $site_html_head_string = "++GITWEB_SITE_HTML_HEAD_STRING++";
  87. # filename of html text to include at top of each page
  88. our $site_header = "++GITWEB_SITE_HEADER++";
  89. # html text to include at home page
  90. our $home_text = "++GITWEB_HOMETEXT++";
  91. # filename of html text to include at bottom of each page
  92. our $site_footer = "++GITWEB_SITE_FOOTER++";
  93. # URI of stylesheets
  94. our @stylesheets = ("++GITWEB_CSS++");
  95. # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
  96. our $stylesheet = undef;
  97. # URI of GIT logo (72x27 size)
  98. our $logo = "++GITWEB_LOGO++";
  99. # URI of GIT favicon, assumed to be image/png type
  100. our $favicon = "++GITWEB_FAVICON++";
  101. # URI of gitweb.js (JavaScript code for gitweb)
  102. our $javascript = "++GITWEB_JS++";
  103. # URI and label (title) of GIT logo link
  104. #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
  105. #our $logo_label = "git documentation";
  106. our $logo_url = "http://git-scm.com/";
  107. our $logo_label = "git homepage";
  108. # source of projects list
  109. our $projects_list = "++GITWEB_LIST++";
  110. # the width (in characters) of the projects list "Description" column
  111. our $projects_list_description_width = 25;
  112. # group projects by category on the projects list
  113. # (enabled if this variable evaluates to true)
  114. our $projects_list_group_categories = 0;
  115. # default category if none specified
  116. # (leave the empty string for no category)
  117. our $project_list_default_category = "";
  118. # default order of projects list
  119. # valid values are none, project, descr, owner, and age
  120. our $default_projects_order = "project";
  121. # show repository only if this file exists
  122. # (only effective if this variable evaluates to true)
  123. our $export_ok = "++GITWEB_EXPORT_OK++";
  124. # don't generate age column on the projects list page
  125. our $omit_age_column = 0;
  126. # don't generate information about owners of repositories
  127. our $omit_owner=0;
  128. # show repository only if this subroutine returns true
  129. # when given the path to the project, for example:
  130. # sub { return -e "$_[0]/git-daemon-export-ok"; }
  131. our $export_auth_hook = undef;
  132. # only allow viewing of repositories also shown on the overview page
  133. our $strict_export = "++GITWEB_STRICT_EXPORT++";
  134. # list of git base URLs used for URL to where fetch project from,
  135. # i.e. full URL is "$git_base_url/$project"
  136. our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
  137. # default blob_plain mimetype and default charset for text/plain blob
  138. our $default_blob_plain_mimetype = 'text/plain';
  139. our $default_text_plain_charset = undef;
  140. # file to use for guessing MIME types before trying /etc/mime.types
  141. # (relative to the current git repository)
  142. our $mimetypes_file = undef;
  143. # assume this charset if line contains non-UTF-8 characters;
  144. # it should be valid encoding (see Encoding::Supported(3pm) for list),
  145. # for which encoding all byte sequences are valid, for example
  146. # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
  147. # could be even 'utf-8' for the old behavior)
  148. our $fallback_encoding = 'latin1';
  149. # rename detection options for git-diff and git-diff-tree
  150. # - default is '-M', with the cost proportional to
  151. # (number of removed files) * (number of new files).
  152. # - more costly is '-C' (which implies '-M'), with the cost proportional to
  153. # (number of changed files + number of removed files) * (number of new files)
  154. # - even more costly is '-C', '--find-copies-harder' with cost
  155. # (number of files in the original tree) * (number of new files)
  156. # - one might want to include '-B' option, e.g. '-B', '-M'
  157. our @diff_opts = ('-M'); # taken from git_commit
  158. # Disables features that would allow repository owners to inject script into
  159. # the gitweb domain.
  160. our $prevent_xss = 0;
  161. # Path to the highlight executable to use (must be the one from
  162. # http://www.andre-simon.de due to assumptions about parameters and output).
  163. # Useful if highlight is not installed on your webserver's PATH.
  164. # [Default: highlight]
  165. our $highlight_bin = "++HIGHLIGHT_BIN++";
  166. # information about snapshot formats that gitweb is capable of serving
  167. our %known_snapshot_formats = (
  168. # name => {
  169. # 'display' => display name,
  170. # 'type' => mime type,
  171. # 'suffix' => filename suffix,
  172. # 'format' => --format for git-archive,
  173. # 'compressor' => [compressor command and arguments]
  174. # (array reference, optional)
  175. # 'disabled' => boolean (optional)}
  176. #
  177. 'tgz' => {
  178. 'display' => 'tar.gz',
  179. 'type' => 'application/x-gzip',
  180. 'suffix' => '.tar.gz',
  181. 'format' => 'tar',
  182. 'compressor' => ['gzip', '-n']},
  183. 'tbz2' => {
  184. 'display' => 'tar.bz2',
  185. 'type' => 'application/x-bzip2',
  186. 'suffix' => '.tar.bz2',
  187. 'format' => 'tar',
  188. 'compressor' => ['bzip2']},
  189. 'txz' => {
  190. 'display' => 'tar.xz',
  191. 'type' => 'application/x-xz',
  192. 'suffix' => '.tar.xz',
  193. 'format' => 'tar',
  194. 'compressor' => ['xz'],
  195. 'disabled' => 1},
  196. 'zip' => {
  197. 'display' => 'zip',
  198. 'type' => 'application/x-zip',
  199. 'suffix' => '.zip',
  200. 'format' => 'zip'},
  201. );
  202. # Aliases so we understand old gitweb.snapshot values in repository
  203. # configuration.
  204. our %known_snapshot_format_aliases = (
  205. 'gzip' => 'tgz',
  206. 'bzip2' => 'tbz2',
  207. 'xz' => 'txz',
  208. # backward compatibility: legacy gitweb config support
  209. 'x-gzip' => undef, 'gz' => undef,
  210. 'x-bzip2' => undef, 'bz2' => undef,
  211. 'x-zip' => undef, '' => undef,
  212. );
  213. # Pixel sizes for icons and avatars. If the default font sizes or lineheights
  214. # are changed, it may be appropriate to change these values too via
  215. # $GITWEB_CONFIG.
  216. our %avatar_size = (
  217. 'default' => 16,
  218. 'double' => 32
  219. );
  220. # Used to set the maximum load that we will still respond to gitweb queries.
  221. # If server load exceed this value then return "503 server busy" error.
  222. # If gitweb cannot determined server load, it is taken to be 0.
  223. # Leave it undefined (or set to 'undef') to turn off load checking.
  224. our $maxload = 300;
  225. # configuration for 'highlight' (http://www.andre-simon.de/)
  226. # match by basename
  227. our %highlight_basename = (
  228. #'Program' => 'py',
  229. #'Library' => 'py',
  230. 'SConstruct' => 'py', # SCons equivalent of Makefile
  231. 'Makefile' => 'make',
  232. );
  233. # match by extension
  234. our %highlight_ext = (
  235. # main extensions, defining name of syntax;
  236. # see files in /usr/share/highlight/langDefs/ directory
  237. (map { $_ => $_ } qw(py rb java css js tex bib xml awk bat ini spec tcl sql)),
  238. # alternate extensions, see /etc/highlight/filetypes.conf
  239. (map { $_ => 'c' } qw(c h)),
  240. (map { $_ => 'sh' } qw(sh bash zsh ksh)),
  241. (map { $_ => 'cpp' } qw(cpp cxx c++ cc)),
  242. (map { $_ => 'php' } qw(php php3 php4 php5 phps)),
  243. (map { $_ => 'pl' } qw(pl perl pm)), # perhaps also 'cgi'
  244. (map { $_ => 'make'} qw(make mak mk)),
  245. (map { $_ => 'xml' } qw(xml xhtml html htm)),
  246. );
  247. # You define site-wide feature defaults here; override them with
  248. # $GITWEB_CONFIG as necessary.
  249. our %feature = (
  250. # feature => {
  251. # 'sub' => feature-sub (subroutine),
  252. # 'override' => allow-override (boolean),
  253. # 'default' => [ default options...] (array reference)}
  254. #
  255. # if feature is overridable (it means that allow-override has true value),
  256. # then feature-sub will be called with default options as parameters;
  257. # return value of feature-sub indicates if to enable specified feature
  258. #
  259. # if there is no 'sub' key (no feature-sub), then feature cannot be
  260. # overridden
  261. #
  262. # use gitweb_get_feature(<feature>) to retrieve the <feature> value
  263. # (an array) or gitweb_check_feature(<feature>) to check if <feature>
  264. # is enabled
  265. # Enable the 'blame' blob view, showing the last commit that modified
  266. # each line in the file. This can be very CPU-intensive.
  267. # To enable system wide have in $GITWEB_CONFIG
  268. # $feature{'blame'}{'default'} = [1];
  269. # To have project specific config enable override in $GITWEB_CONFIG
  270. # $feature{'blame'}{'override'} = 1;
  271. # and in project config gitweb.blame = 0|1;
  272. 'blame' => {
  273. 'sub' => sub { feature_bool('blame', @_) },
  274. 'override' => 0,
  275. 'default' => [0]},
  276. # Enable the 'snapshot' link, providing a compressed archive of any
  277. # tree. This can potentially generate high traffic if you have large
  278. # project.
  279. # Value is a list of formats defined in %known_snapshot_formats that
  280. # you wish to offer.
  281. # To disable system wide have in $GITWEB_CONFIG
  282. # $feature{'snapshot'}{'default'} = [];
  283. # To have project specific config enable override in $GITWEB_CONFIG
  284. # $feature{'snapshot'}{'override'} = 1;
  285. # and in project config, a comma-separated list of formats or "none"
  286. # to disable. Example: gitweb.snapshot = tbz2,zip;
  287. 'snapshot' => {
  288. 'sub' => \&feature_snapshot,
  289. 'override' => 0,
  290. 'default' => ['tgz']},
  291. # Enable text search, which will list the commits which match author,
  292. # committer or commit text to a given string. Enabled by default.
  293. # Project specific override is not supported.
  294. #
  295. # Note that this controls all search features, which means that if
  296. # it is disabled, then 'grep' and 'pickaxe' search would also be
  297. # disabled.
  298. 'search' => {
  299. 'override' => 0,
  300. 'default' => [1]},
  301. # Enable grep search, which will list the files in currently selected
  302. # tree containing the given string. Enabled by default. This can be
  303. # potentially CPU-intensive, of course.
  304. # Note that you need to have 'search' feature enabled too.
  305. # To enable system wide have in $GITWEB_CONFIG
  306. # $feature{'grep'}{'default'} = [1];
  307. # To have project specific config enable override in $GITWEB_CONFIG
  308. # $feature{'grep'}{'override'} = 1;
  309. # and in project config gitweb.grep = 0|1;
  310. 'grep' => {
  311. 'sub' => sub { feature_bool('grep', @_) },
  312. 'override' => 0,
  313. 'default' => [1]},
  314. # Enable the pickaxe search, which will list the commits that modified
  315. # a given string in a file. This can be practical and quite faster
  316. # alternative to 'blame', but still potentially CPU-intensive.
  317. # Note that you need to have 'search' feature enabled too.
  318. # To enable system wide have in $GITWEB_CONFIG
  319. # $feature{'pickaxe'}{'default'} = [1];
  320. # To have project specific config enable override in $GITWEB_CONFIG
  321. # $feature{'pickaxe'}{'override'} = 1;
  322. # and in project config gitweb.pickaxe = 0|1;
  323. 'pickaxe' => {
  324. 'sub' => sub { feature_bool('pickaxe', @_) },
  325. 'override' => 0,
  326. 'default' => [1]},
  327. # Enable showing size of blobs in a 'tree' view, in a separate
  328. # column, similar to what 'ls -l' does. This cost a bit of IO.
  329. # To disable system wide have in $GITWEB_CONFIG
  330. # $feature{'show-sizes'}{'default'} = [0];
  331. # To have project specific config enable override in $GITWEB_CONFIG
  332. # $feature{'show-sizes'}{'override'} = 1;
  333. # and in project config gitweb.showsizes = 0|1;
  334. 'show-sizes' => {
  335. 'sub' => sub { feature_bool('showsizes', @_) },
  336. 'override' => 0,
  337. 'default' => [1]},
  338. # Make gitweb use an alternative format of the URLs which can be
  339. # more readable and natural-looking: project name is embedded
  340. # directly in the path and the query string contains other
  341. # auxiliary information. All gitweb installations recognize
  342. # URL in either format; this configures in which formats gitweb
  343. # generates links.
  344. # To enable system wide have in $GITWEB_CONFIG
  345. # $feature{'pathinfo'}{'default'} = [1];
  346. # Project specific override is not supported.
  347. # Note that you will need to change the default location of CSS,
  348. # favicon, logo and possibly other files to an absolute URL. Also,
  349. # if gitweb.cgi serves as your indexfile, you will need to force
  350. # $my_uri to contain the script name in your $GITWEB_CONFIG.
  351. 'pathinfo' => {
  352. 'override' => 0,
  353. 'default' => [0]},
  354. # Make gitweb consider projects in project root subdirectories
  355. # to be forks of existing projects. Given project $projname.git,
  356. # projects matching $projname/*.git will not be shown in the main
  357. # projects list, instead a '+' mark will be added to $projname
  358. # there and a 'forks' view will be enabled for the project, listing
  359. # all the forks. If project list is taken from a file, forks have
  360. # to be listed after the main project.
  361. # To enable system wide have in $GITWEB_CONFIG
  362. # $feature{'forks'}{'default'} = [1];
  363. # Project specific override is not supported.
  364. 'forks' => {
  365. 'override' => 0,
  366. 'default' => [0]},
  367. # Insert custom links to the action bar of all project pages.
  368. # This enables you mainly to link to third-party scripts integrating
  369. # into gitweb; e.g. git-browser for graphical history representation
  370. # or custom web-based repository administration interface.
  371. # The 'default' value consists of a list of triplets in the form
  372. # (label, link, position) where position is the label after which
  373. # to insert the link and link is a format string where %n expands
  374. # to the project name, %f to the project path within the filesystem,
  375. # %h to the current hash (h gitweb parameter) and %b to the current
  376. # hash base (hb gitweb parameter); %% expands to %.
  377. # To enable system wide have in $GITWEB_CONFIG e.g.
  378. # $feature{'actions'}{'default'} = [('graphiclog',
  379. # '/git-browser/by-commit.html?r=%n', 'summary')];
  380. # Project specific override is not supported.
  381. 'actions' => {
  382. 'override' => 0,
  383. 'default' => []},
  384. # Allow gitweb scan project content tags of project repository,
  385. # and display the popular Web 2.0-ish "tag cloud" near the projects
  386. # list. Note that this is something COMPLETELY different from the
  387. # normal Git tags.
  388. # gitweb by itself can show existing tags, but it does not handle
  389. # tagging itself; you need to do it externally, outside gitweb.
  390. # The format is described in git_get_project_ctags() subroutine.
  391. # You may want to install the HTML::TagCloud Perl module to get
  392. # a pretty tag cloud instead of just a list of tags.
  393. # To enable system wide have in $GITWEB_CONFIG
  394. # $feature{'ctags'}{'default'} = [1];
  395. # Project specific override is not supported.
  396. # In the future whether ctags editing is enabled might depend
  397. # on the value, but using 1 should always mean no editing of ctags.
  398. 'ctags' => {
  399. 'override' => 0,
  400. 'default' => [0]},
  401. # The maximum number of patches in a patchset generated in patch
  402. # view. Set this to 0 or undef to disable patch view, or to a
  403. # negative number to remove any limit.
  404. # To disable system wide have in $GITWEB_CONFIG
  405. # $feature{'patches'}{'default'} = [0];
  406. # To have project specific config enable override in $GITWEB_CONFIG
  407. # $feature{'patches'}{'override'} = 1;
  408. # and in project config gitweb.patches = 0|n;
  409. # where n is the maximum number of patches allowed in a patchset.
  410. 'patches' => {
  411. 'sub' => \&feature_patches,
  412. 'override' => 0,
  413. 'default' => [16]},
  414. # Avatar support. When this feature is enabled, views such as
  415. # shortlog or commit will display an avatar associated with
  416. # the email of the committer(s) and/or author(s).
  417. # Currently available providers are gravatar and picon.
  418. # If an unknown provider is specified, the feature is disabled.
  419. # Picon currently relies on the indiana.edu database.
  420. # To enable system wide have in $GITWEB_CONFIG
  421. # $feature{'avatar'}{'default'} = ['<provider>'];
  422. # where <provider> is either gravatar or picon.
  423. # To have project specific config enable override in $GITWEB_CONFIG
  424. # $feature{'avatar'}{'override'} = 1;
  425. # and in project config gitweb.avatar = <provider>;
  426. 'avatar' => {
  427. 'sub' => \&feature_avatar,
  428. 'override' => 0,
  429. 'default' => ['']},
  430. # Enable displaying how much time and how many git commands
  431. # it took to generate and display page. Disabled by default.
  432. # Project specific override is not supported.
  433. 'timed' => {
  434. 'override' => 0,
  435. 'default' => [0]},
  436. # Enable turning some links into links to actions which require
  437. # JavaScript to run (like 'blame_incremental'). Not enabled by
  438. # default. Project specific override is currently not supported.
  439. 'javascript-actions' => {
  440. 'override' => 0,
  441. 'default' => [0]},
  442. # Enable and configure ability to change common timezone for dates
  443. # in gitweb output via JavaScript. Enabled by default.
  444. # Project specific override is not supported.
  445. 'javascript-timezone' => {
  446. 'override' => 0,
  447. 'default' => [
  448. 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
  449. # or undef to turn off this feature
  450. 'gitweb_tz', # name of cookie where to store selected timezone
  451. 'datetime', # CSS class used to mark up dates for manipulation
  452. ]},
  453. # Syntax highlighting support. This is based on Daniel Svensson's
  454. # and Sham Chukoury's work in gitweb-xmms2.git.
  455. # It requires the 'highlight' program present in $PATH,
  456. # and therefore is disabled by default.
  457. # To enable system wide have in $GITWEB_CONFIG
  458. # $feature{'highlight'}{'default'} = [1];
  459. 'highlight' => {
  460. 'sub' => sub { feature_bool('highlight', @_) },
  461. 'override' => 0,
  462. 'default' => [0]},
  463. # Enable displaying of remote heads in the heads list
  464. # To enable system wide have in $GITWEB_CONFIG
  465. # $feature{'remote_heads'}{'default'} = [1];
  466. # To have project specific config enable override in $GITWEB_CONFIG
  467. # $feature{'remote_heads'}{'override'} = 1;
  468. # and in project config gitweb.remoteheads = 0|1;
  469. 'remote_heads' => {
  470. 'sub' => sub { feature_bool('remote_heads', @_) },
  471. 'override' => 0,
  472. 'default' => [0]},
  473. # Enable showing branches under other refs in addition to heads
  474. # To set system wide extra branch refs have in $GITWEB_CONFIG
  475. # $feature{'extra-branch-refs'}{'default'} = ['dirs', 'of', 'choice'];
  476. # To have project specific config enable override in $GITWEB_CONFIG
  477. # $feature{'extra-branch-refs'}{'override'} = 1;
  478. # and in project config gitweb.extrabranchrefs = dirs of choice
  479. # Every directory is separated with whitespace.
  480. 'extra-branch-refs' => {
  481. 'sub' => \&feature_extra_branch_refs,
  482. 'override' => 0,
  483. 'default' => []},
  484. );
  485. sub gitweb_get_feature {
  486. my ($name) = @_;
  487. return unless exists $feature{$name};
  488. my ($sub, $override, @defaults) = (
  489. $feature{$name}{'sub'},
  490. $feature{$name}{'override'},
  491. @{$feature{$name}{'default'}});
  492. # project specific override is possible only if we have project
  493. our $git_dir; # global variable, declared later
  494. if (!$override || !defined $git_dir) {
  495. return @defaults;
  496. }
  497. if (!defined $sub) {
  498. warn "feature $name is not overridable";
  499. return @defaults;
  500. }
  501. return $sub->(@defaults);
  502. }
  503. # A wrapper to check if a given feature is enabled.
  504. # With this, you can say
  505. #
  506. # my $bool_feat = gitweb_check_feature('bool_feat');
  507. # gitweb_check_feature('bool_feat') or somecode;
  508. #
  509. # instead of
  510. #
  511. # my ($bool_feat) = gitweb_get_feature('bool_feat');
  512. # (gitweb_get_feature('bool_feat'))[0] or somecode;
  513. #
  514. sub gitweb_check_feature {
  515. return (gitweb_get_feature(@_))[0];
  516. }
  517. sub feature_bool {
  518. my $key = shift;
  519. my ($val) = git_get_project_config($key, '--bool');
  520. if (!defined $val) {
  521. return ($_[0]);
  522. } elsif ($val eq 'true') {
  523. return (1);
  524. } elsif ($val eq 'false') {
  525. return (0);
  526. }
  527. }
  528. sub feature_snapshot {
  529. my (@fmts) = @_;
  530. my ($val) = git_get_project_config('snapshot');
  531. if ($val) {
  532. @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
  533. }
  534. return @fmts;
  535. }
  536. sub feature_patches {
  537. my @val = (git_get_project_config('patches', '--int'));
  538. if (@val) {
  539. return @val;
  540. }
  541. return ($_[0]);
  542. }
  543. sub feature_avatar {
  544. my @val = (git_get_project_config('avatar'));
  545. return @val ? @val : @_;
  546. }
  547. sub feature_extra_branch_refs {
  548. my (@branch_refs) = @_;
  549. my $values = git_get_project_config('extrabranchrefs');
  550. if ($values) {
  551. $values = config_to_multi ($values);
  552. @branch_refs = ();
  553. foreach my $value (@{$values}) {
  554. push @branch_refs, split /\s+/, $value;
  555. }
  556. }
  557. return @branch_refs;
  558. }
  559. # checking HEAD file with -e is fragile if the repository was
  560. # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
  561. # and then pruned.
  562. sub check_head_link {
  563. my ($dir) = @_;
  564. my $headfile = "$dir/HEAD";
  565. return ((-e $headfile) ||
  566. (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
  567. }
  568. sub check_export_ok {
  569. my ($dir) = @_;
  570. return (check_head_link($dir) &&
  571. (!$export_ok || -e "$dir/$export_ok") &&
  572. (!$export_auth_hook || $export_auth_hook->($dir)));
  573. }
  574. # process alternate names for backward compatibility
  575. # filter out unsupported (unknown) snapshot formats
  576. sub filter_snapshot_fmts {
  577. my @fmts = @_;
  578. @fmts = map {
  579. exists $known_snapshot_format_aliases{$_} ?
  580. $known_snapshot_format_aliases{$_} : $_} @fmts;
  581. @fmts = grep {
  582. exists $known_snapshot_formats{$_} &&
  583. !$known_snapshot_formats{$_}{'disabled'}} @fmts;
  584. }
  585. sub filter_and_validate_refs {
  586. my @refs = @_;
  587. my %unique_refs = ();
  588. foreach my $ref (@refs) {
  589. die_error(500, "Invalid ref '$ref' in 'extra-branch-refs' feature") unless (is_valid_ref_format($ref));
  590. # 'heads' are added implicitly in get_branch_refs().
  591. $unique_refs{$ref} = 1 if ($ref ne 'heads');
  592. }
  593. return sort keys %unique_refs;
  594. }
  595. # If it is set to code reference, it is code that it is to be run once per
  596. # request, allowing updating configurations that change with each request,
  597. # while running other code in config file only once.
  598. #
  599. # Otherwise, if it is false then gitweb would process config file only once;
  600. # if it is true then gitweb config would be run for each request.
  601. our $per_request_config = 1;
  602. # read and parse gitweb config file given by its parameter.
  603. # returns true on success, false on recoverable error, allowing
  604. # to chain this subroutine, using first file that exists.
  605. # dies on errors during parsing config file, as it is unrecoverable.
  606. sub read_config_file {
  607. my $filename = shift;
  608. return unless defined $filename;
  609. # die if there are errors parsing config file
  610. if (-e $filename) {
  611. do $filename;
  612. die $@ if $@;
  613. return 1;
  614. }
  615. return;
  616. }
  617. our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM, $GITWEB_CONFIG_COMMON);
  618. sub evaluate_gitweb_config {
  619. our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
  620. our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
  621. our $GITWEB_CONFIG_COMMON = $ENV{'GITWEB_CONFIG_COMMON'} || "++GITWEB_CONFIG_COMMON++";
  622. # Protect against duplications of file names, to not read config twice.
  623. # Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so
  624. # there possibility of duplication of filename there doesn't matter.
  625. $GITWEB_CONFIG = "" if ($GITWEB_CONFIG eq $GITWEB_CONFIG_COMMON);
  626. $GITWEB_CONFIG_SYSTEM = "" if ($GITWEB_CONFIG_SYSTEM eq $GITWEB_CONFIG_COMMON);
  627. # Common system-wide settings for convenience.
  628. # Those settings can be overridden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM.
  629. read_config_file($GITWEB_CONFIG_COMMON);
  630. # Use first config file that exists. This means use the per-instance
  631. # GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG.
  632. read_config_file($GITWEB_CONFIG) and return;
  633. read_config_file($GITWEB_CONFIG_SYSTEM);
  634. }
  635. # Get loadavg of system, to compare against $maxload.
  636. # Currently it requires '/proc/loadavg' present to get loadavg;
  637. # if it is not present it returns 0, which means no load checking.
  638. sub get_loadavg {
  639. if( -e '/proc/loadavg' ){
  640. open my $fd, '<', '/proc/loadavg'
  641. or return 0;
  642. my @load = split(/\s+/, scalar <$fd>);
  643. close $fd;
  644. # The first three columns measure CPU and IO utilization of the last one,
  645. # five, and 10 minute periods. The fourth column shows the number of
  646. # currently running processes and the total number of processes in the m/n
  647. # format. The last column displays the last process ID used.
  648. return $load[0] || 0;
  649. }
  650. # additional checks for load average should go here for things that don't export
  651. # /proc/loadavg
  652. return 0;
  653. }
  654. # version of the core git binary
  655. our $git_version;
  656. sub evaluate_git_version {
  657. our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
  658. $number_of_git_cmds++;
  659. }
  660. sub check_loadavg {
  661. if (defined $maxload && get_loadavg() > $maxload) {
  662. die_error(503, "The load average on the server is too high");
  663. }
  664. }
  665. # ======================================================================
  666. # input validation and dispatch
  667. # Various hash size-related values.
  668. my $sha1_len = 40;
  669. my $sha256_extra_len = 24;
  670. my $sha256_len = $sha1_len + $sha256_extra_len;
  671. # A regex matching $len hex characters. $len may be a range (e.g. 7,64).
  672. sub oid_nlen_regex {
  673. my $len = shift;
  674. my $hchr = qr/[0-9a-fA-F]/;
  675. return qr/(?:(?:$hchr){$len})/;
  676. }
  677. # A regex matching two sets of $nlen hex characters, prefixed by the literal
  678. # string $prefix and with the literal string $infix between them.
  679. sub oid_nlen_prefix_infix_regex {
  680. my $nlen = shift;
  681. my $prefix = shift;
  682. my $infix = shift;
  683. my $rx = oid_nlen_regex($nlen);
  684. return qr/^\Q$prefix\E$rx\Q$infix\E$rx$/;
  685. }
  686. # A regex matching a valid object ID.
  687. our $oid_regex;
  688. {
  689. my $x = oid_nlen_regex($sha1_len);
  690. my $y = oid_nlen_regex($sha256_extra_len);
  691. $oid_regex = qr/(?:$x(?:$y)?)/;
  692. }
  693. # input parameters can be collected from a variety of sources (presently, CGI
  694. # and PATH_INFO), so we define an %input_params hash that collects them all
  695. # together during validation: this allows subsequent uses (e.g. href()) to be
  696. # agnostic of the parameter origin
  697. our %input_params = ();
  698. # input parameters are stored with the long parameter name as key. This will
  699. # also be used in the href subroutine to convert parameters to their CGI
  700. # equivalent, and since the href() usage is the most frequent one, we store
  701. # the name -> CGI key mapping here, instead of the reverse.
  702. #
  703. # XXX: Warning: If you touch this, check the search form for updating,
  704. # too.
  705. our @cgi_param_mapping = (
  706. project => "p",
  707. action => "a",
  708. file_name => "f",
  709. file_parent => "fp",
  710. hash => "h",
  711. hash_parent => "hp",
  712. hash_base => "hb",
  713. hash_parent_base => "hpb",
  714. page => "pg",
  715. order => "o",
  716. searchtext => "s",
  717. searchtype => "st",
  718. snapshot_format => "sf",
  719. extra_options => "opt",
  720. search_use_regexp => "sr",
  721. ctag => "by_tag",
  722. diff_style => "ds",
  723. project_filter => "pf",
  724. # this must be last entry (for manipulation from JavaScript)
  725. javascript => "js"
  726. );
  727. our %cgi_param_mapping = @cgi_param_mapping;
  728. # we will also need to know the possible actions, for validation
  729. our %actions = (
  730. "blame" => \&git_blame,
  731. "blame_incremental" => \&git_blame_incremental,
  732. "blame_data" => \&git_blame_data,
  733. "blobdiff" => \&git_blobdiff,
  734. "blobdiff_plain" => \&git_blobdiff_plain,
  735. "blob" => \&git_blob,
  736. "blob_plain" => \&git_blob_plain,
  737. "commitdiff" => \&git_commitdiff,
  738. "commitdiff_plain" => \&git_commitdiff_plain,
  739. "commit" => \&git_commit,
  740. "forks" => \&git_forks,
  741. "heads" => \&git_heads,
  742. "history" => \&git_history,
  743. "log" => \&git_log,
  744. "patch" => \&git_patch,
  745. "patches" => \&git_patches,
  746. "remotes" => \&git_remotes,
  747. "rss" => \&git_rss,
  748. "atom" => \&git_atom,
  749. "search" => \&git_search,
  750. "search_help" => \&git_search_help,
  751. "shortlog" => \&git_shortlog,
  752. "summary" => \&git_summary,
  753. "tag" => \&git_tag,
  754. "tags" => \&git_tags,
  755. "tree" => \&git_tree,
  756. "snapshot" => \&git_snapshot,
  757. "object" => \&git_object,
  758. # those below don't need $project
  759. "opml" => \&git_opml,
  760. "project_list" => \&git_project_list,
  761. "project_index" => \&git_project_index,
  762. );
  763. # finally, we have the hash of allowed extra_options for the commands that
  764. # allow them
  765. our %allowed_options = (
  766. "--no-merges" => [ qw(rss atom log shortlog history) ],
  767. );
  768. # fill %input_params with the CGI parameters. All values except for 'opt'
  769. # should be single values, but opt can be an array. We should probably
  770. # build an array of parameters that can be multi-valued, but since for the time
  771. # being it's only this one, we just single it out
  772. sub evaluate_query_params {
  773. our $cgi;
  774. while (my ($name, $symbol) = each %cgi_param_mapping) {
  775. if ($symbol eq 'opt') {
  776. $input_params{$name} = [ map { decode_utf8($_) } $cgi->multi_param($symbol) ];
  777. } else {
  778. $input_params{$name} = decode_utf8($cgi->param($symbol));
  779. }
  780. }
  781. }
  782. # now read PATH_INFO and update the parameter list for missing parameters
  783. sub evaluate_path_info {
  784. return if defined $input_params{'project'};
  785. return if !$path_info;
  786. $path_info =~ s,^/+,,;
  787. return if !$path_info;
  788. # find which part of PATH_INFO is project
  789. my $project = $path_info;
  790. $project =~ s,/+$,,;
  791. while ($project && !check_head_link("$projectroot/$project")) {
  792. $project =~ s,/*[^/]*$,,;
  793. }
  794. return unless $project;
  795. $input_params{'project'} = $project;
  796. # do not change any parameters if an action is given using the query string
  797. return if $input_params{'action'};
  798. $path_info =~ s,^\Q$project\E/*,,;
  799. # next, check if we have an action
  800. my $action = $path_info;
  801. $action =~ s,/.*$,,;
  802. if (exists $actions{$action}) {
  803. $path_info =~ s,^$action/*,,;
  804. $input_params{'action'} = $action;
  805. }
  806. # list of actions that want hash_base instead of hash, but can have no
  807. # pathname (f) parameter
  808. my @wants_base = (
  809. 'tree',
  810. 'history',
  811. );
  812. # we want to catch, among others
  813. # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
  814. my ($parentrefname, $parentpathname, $refname, $pathname) =
  815. ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
  816. # first, analyze the 'current' part
  817. if (defined $pathname) {
  818. # we got "branch:filename" or "branch:dir/"
  819. # we could use git_get_type(branch:pathname), but:
  820. # - it needs $git_dir
  821. # - it does a git() call
  822. # - the convention of terminating directories with a slash
  823. # makes it superfluous
  824. # - embedding the action in the PATH_INFO would make it even
  825. # more superfluous
  826. $pathname =~ s,^/+,,;
  827. if (!$pathname || substr($pathname, -1) eq "/") {
  828. $input_params{'action'} ||= "tree";
  829. $pathname =~ s,/$,,;
  830. } else {
  831. # the default action depends on whether we had parent info
  832. # or not
  833. if ($parentrefname) {
  834. $input_params{'action'} ||= "blobdiff_plain";
  835. } else {
  836. $input_params{'action'} ||= "blob_plain";
  837. }
  838. }
  839. $input_params{'hash_base'} ||= $refname;
  840. $input_params{'file_name'} ||= $pathname;
  841. } elsif (defined $refname) {
  842. # we got "branch". In this case we have to choose if we have to
  843. # set hash or hash_base.
  844. #
  845. # Most of the actions without a pathname only want hash to be
  846. # set, except for the ones specified in @wants_base that want
  847. # hash_base instead. It should also be noted that hand-crafted
  848. # links having 'history' as an action and no pathname or hash
  849. # set will fail, but that happens regardless of PATH_INFO.
  850. if (defined $parentrefname) {
  851. # if there is parent let the default be 'shortlog' action
  852. # (for http://git.example.com/repo.git/A..B links); if there
  853. # is no parent, dispatch will detect type of object and set
  854. # action appropriately if required (if action is not set)
  855. $input_params{'action'} ||= "shortlog";
  856. }
  857. if ($input_params{'action'} &&
  858. grep { $_ eq $input_params{'action'} } @wants_base) {
  859. $input_params{'hash_base'} ||= $refname;
  860. } else {
  861. $input_params{'hash'} ||= $refname;
  862. }
  863. }
  864. # next, handle the 'parent' part, if present
  865. if (defined $parentrefname) {
  866. # a missing pathspec defaults to the 'current' filename, allowing e.g.
  867. # someproject/blobdiff/oldrev..newrev:/filename
  868. if ($parentpathname) {
  869. $parentpathname =~ s,^/+,,;
  870. $parentpathname =~ s,/$,,;
  871. $input_params{'file_parent'} ||= $parentpathname;
  872. } else {
  873. $input_params{'file_parent'} ||= $input_params{'file_name'};
  874. }
  875. # we assume that hash_parent_base is wanted if a path was specified,
  876. # or if the action wants hash_base instead of hash
  877. if (defined $input_params{'file_parent'} ||
  878. grep { $_ eq $input_params{'action'} } @wants_base) {
  879. $input_params{'hash_parent_base'} ||= $parentrefname;
  880. } else {
  881. $input_params{'hash_parent'} ||= $parentrefname;
  882. }
  883. }
  884. # for the snapshot action, we allow URLs in the form
  885. # $project/snapshot/$hash.ext
  886. # where .ext determines the snapshot and gets removed from the
  887. # passed $refname to provide the $hash.
  888. #
  889. # To be able to tell that $refname includes the format extension, we
  890. # require the following two conditions to be satisfied:
  891. # - the hash input parameter MUST have been set from the $refname part
  892. # of the URL (i.e. they must be equal)
  893. # - the snapshot format MUST NOT have been defined already (e.g. from
  894. # CGI parameter sf)
  895. # It's also useless to try any matching unless $refname has a dot,
  896. # so we check for that too
  897. if (defined $input_params{'action'} &&
  898. $input_params{'action'} eq 'snapshot' &&
  899. defined $refname && index($refname, '.') != -1 &&
  900. $refname eq $input_params{'hash'} &&
  901. !defined $input_params{'snapshot_format'}) {
  902. # We loop over the known snapshot formats, checking for
  903. # extensions. Allowed extensions are both the defined suffix
  904. # (which includes the initial dot already) and the snapshot
  905. # format key itself, with a prepended dot
  906. while (my ($fmt, $opt) = each %known_snapshot_formats) {
  907. my $hash = $refname;
  908. unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
  909. next;
  910. }
  911. my $sfx = $1;
  912. # a valid suffix was found, so set the snapshot format
  913. # and reset the hash parameter
  914. $input_params{'snapshot_format'} = $fmt;
  915. $input_params{'hash'} = $hash;
  916. # we also set the format suffix to the one requested
  917. # in the URL: this way a request for e.g. .tgz returns
  918. # a .tgz instead of a .tar.gz
  919. $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
  920. last;
  921. }
  922. }
  923. }
  924. our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
  925. $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
  926. $searchtext, $search_regexp, $project_filter);
  927. sub evaluate_and_validate_params {
  928. our $action = $input_params{'action'};
  929. if (defined $action) {
  930. if (!is_valid_action($action)) {
  931. die_error(400, "Invalid action parameter");
  932. }
  933. }
  934. # parameters which are pathnames
  935. our $project = $input_params{'project'};
  936. if (defined $project) {
  937. if (!is_valid_project($project)) {
  938. undef $project;
  939. die_error(404, "No such project");
  940. }
  941. }
  942. our $project_filter = $input_params{'project_filter'};
  943. if (defined $project_filter) {
  944. if (!is_valid_pathname($project_filter)) {
  945. die_error(404, "Invalid project_filter parameter");
  946. }
  947. }
  948. our $file_name = $input_params{'file_name'};
  949. if (defined $file_name) {
  950. if (!is_valid_pathname($file_name)) {
  951. die_error(400, "Invalid file parameter");
  952. }
  953. }
  954. our $file_parent = $input_params{'file_parent'};
  955. if (defined $file_parent) {
  956. if (!is_valid_pathname($file_parent)) {
  957. die_error(400, "Invalid file parent parameter");
  958. }
  959. }
  960. # parameters which are refnames
  961. our $hash = $input_params{'hash'};
  962. if (defined $hash) {
  963. if (!is_valid_refname($hash)) {
  964. die_error(400, "Invalid hash parameter");
  965. }
  966. }
  967. our $hash_parent = $input_params{'hash_parent'};
  968. if (defined $hash_parent) {
  969. if (!is_valid_refname($hash_parent)) {
  970. die_error(400, "Invalid hash parent parameter");
  971. }
  972. }
  973. our $hash_base = $input_params{'hash_base'};
  974. if (defined $hash_base) {
  975. if (!is_valid_refname($hash_base)) {
  976. die_error(400, "Invalid hash base parameter");
  977. }
  978. }
  979. our @extra_options = @{$input_params{'extra_options'}};
  980. # @extra_options is always defined, since it can only be (currently) set from
  981. # CGI, and $cgi->param() returns the empty array in array context if the param
  982. # is not set
  983. foreach my $opt (@extra_options) {
  984. if (not exists $allowed_options{$opt}) {
  985. die_error(400, "Invalid option parameter");
  986. }
  987. if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
  988. die_error(400, "Invalid option parameter for this action");
  989. }
  990. }
  991. our $hash_parent_base = $input_params{'hash_parent_base'};
  992. if (defined $hash_parent_base) {
  993. if (!is_valid_refname($hash_parent_base)) {
  994. die_error(400, "Invalid hash parent base parameter");
  995. }
  996. }
  997. # other parameters
  998. our $page = $input_params{'page'};
  999. if (defined $page) {
  1000. if ($page =~ m/[^0-9]/) {
  1001. die_error(400, "Invalid page parameter");
  1002. }
  1003. }
  1004. our $searchtype = $input_params{'searchtype'};
  1005. if (defined $searchtype) {
  1006. if ($searchtype =~ m/[^a-z]/) {
  1007. die_error(400, "Invalid searchtype parameter");
  1008. }
  1009. }
  1010. our $search_use_regexp = $input_params{'search_use_regexp'};
  1011. our $searchtext = $input_params{'searchtext'};
  1012. our $search_regexp = undef;
  1013. if (defined $searchtext) {
  1014. if (length($searchtext) < 2) {
  1015. die_error(403, "At least two characters are required for search parameter");
  1016. }
  1017. if ($search_use_regexp) {
  1018. $search_regexp = $searchtext;
  1019. if (!eval { qr/$search_regexp/; 1; }) {
  1020. (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
  1021. die_error(400, "Invalid search regexp '$search_regexp'",
  1022. esc_html($error));
  1023. }
  1024. } else {
  1025. $search_regexp = quotemeta $searchtext;
  1026. }
  1027. }
  1028. }
  1029. # path to the current git repository
  1030. our $git_dir;
  1031. sub evaluate_git_dir {
  1032. our $git_dir = "$projectroot/$project" if $project;
  1033. }
  1034. our (@snapshot_fmts, $git_avatar, @extra_branch_refs);
  1035. sub configure_gitweb_features {
  1036. # list of supported snapshot formats
  1037. our @snapshot_fmts = gitweb_get_feature('snapshot');
  1038. @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
  1039. our ($git_avatar) = gitweb_get_feature('avatar');
  1040. $git_avatar = '' unless $git_avatar =~ /^(?:gravatar|picon)$/s;
  1041. our @extra_branch_refs = gitweb_get_feature('extra-branch-refs');
  1042. @extra_branch_refs = filter_and_validate_refs (@extra_branch_refs);
  1043. }
  1044. sub get_branch_refs {
  1045. return ('heads', @extra_branch_refs);
  1046. }
  1047. # custom error handler: 'die <message>' is Internal Server Error
  1048. sub handle_errors_html {
  1049. my $msg = shift; # it is already HTML escaped
  1050. # to avoid infinite loop where error occurs in die_error,
  1051. # change handler to default handler, disabling handle_errors_html
  1052. set_message("Error occurred when inside die_error:\n$msg");
  1053. # you cannot jump out of die_error when called as error handler;
  1054. # the subroutine set via CGI::Carp::set_message is called _after_
  1055. # HTTP headers are already written, so it cannot write them itself
  1056. die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
  1057. }
  1058. set_message(\&handle_errors_html);
  1059. # dispatch
  1060. sub dispatch {
  1061. if (!defined $action) {
  1062. if (defined $hash) {
  1063. $action = git_get_type($hash);
  1064. $action or die_error(404, "Object does not exist");
  1065. } elsif (defined $hash_base && defined $file_name) {
  1066. $action = git_get_type("$hash_base:$file_name");
  1067. $action or die_error(404, "File or directory does not exist");
  1068. } elsif (defined $project) {
  1069. $action = 'summary';
  1070. } else {
  1071. $action = 'project_list';
  1072. }
  1073. }
  1074. if (!defined($actions{$action})) {
  1075. die_error(400, "Unknown action");
  1076. }
  1077. if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
  1078. !$project) {
  1079. die_error(400, "Project needed");
  1080. }
  1081. $actions{$action}->();
  1082. }
  1083. sub reset_timer {
  1084. our $t0 = [ gettimeofday() ]
  1085. if defined $t0;
  1086. our $number_of_git_cmds = 0;
  1087. }
  1088. our $first_request = 1;
  1089. sub run_request {
  1090. reset_timer();
  1091. evaluate_uri();
  1092. if ($first_request) {
  1093. evaluate_gitweb_config();
  1094. evaluate_git_version();
  1095. }
  1096. if ($per_request_config) {
  1097. if (ref($per_request_config) eq 'CODE') {
  1098. $per_request_config->();
  1099. } elsif (!$first_request) {
  1100. evaluate_gitweb_config();
  1101. }
  1102. }
  1103. check_loadavg();
  1104. # $projectroot and $projects_list might be set in gitweb config file
  1105. $projects_list ||= $projectroot;
  1106. evaluate_query_params();
  1107. evaluate_path_info();
  1108. evaluate_and_validate_params();
  1109. evaluate_git_dir();
  1110. configure_gitweb_features();
  1111. dispatch();
  1112. }
  1113. our $is_last_request = sub { 1 };
  1114. our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
  1115. our $CGI = 'CGI';
  1116. our $cgi;
  1117. our $FCGI_Stream_PRINT_raw = \&FCGI::Stream::PRINT;
  1118. sub configure_as_fcgi {
  1119. require CGI::Fast;
  1120. our $CGI = 'CGI::Fast';
  1121. # FCGI is not Unicode aware hence the UTF-8 encoding must be done manually.
  1122. # However no encoding must be done within git_blob_plain() and git_snapshot()
  1123. # which must still output in raw binary mode.
  1124. no warnings 'redefine';
  1125. my $enc = Encode::find_encoding('UTF-8');
  1126. *FCGI::Stream::PRINT = sub {
  1127. my @OUTPUT = @_;
  1128. for (my $i = 1; $i < @_; $i++) {
  1129. $OUTPUT[$i] = $enc->encode($_[$i], Encode::FB_CROAK|Encode::LEAVE_SRC);
  1130. }
  1131. @_ = @OUTPUT;
  1132. goto $FCGI_Stream_PRINT_raw;
  1133. };
  1134. my $request_number = 0;
  1135. # let each child service 100 requests
  1136. our $is_last_request = sub { ++$request_number > 100 };
  1137. }
  1138. sub evaluate_argv {
  1139. my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
  1140. configure_as_fcgi()
  1141. if $script_name =~ /\.fcgi$/;
  1142. return unless (@ARGV);
  1143. require Getopt::Long;
  1144. Getopt::Long::GetOptions(
  1145. 'fastcgi|fcgi|f' => \&configure_as_fcgi,
  1146. 'nproc|n=i' => sub {
  1147. my ($arg, $val) = @_;
  1148. return unless eval { require FCGI::ProcManager; 1; };
  1149. my $proc_manager = FCGI::ProcManager->new({
  1150. n_processes => $val,
  1151. });
  1152. our $pre_listen_hook = sub { $proc_manager->pm_manage() };
  1153. our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
  1154. our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
  1155. },
  1156. );
  1157. }
  1158. sub run {
  1159. evaluate_argv();
  1160. $first_request = 1;
  1161. $pre_listen_hook->()
  1162. if $pre_listen_hook;
  1163. REQUEST:
  1164. while ($cgi = $CGI->new()) {
  1165. $pre_dispatch_hook->()
  1166. if $pre_dispatch_hook;
  1167. run_request();
  1168. $post_dispatch_hook->()
  1169. if $post_dispatch_hook;
  1170. $first_request = 0;
  1171. last REQUEST if ($is_last_request->());
  1172. }
  1173. DONE_GITWEB:
  1174. 1;
  1175. }
  1176. run();
  1177. if (defined caller) {
  1178. # wrapped in a subroutine processing requests,
  1179. # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
  1180. return;
  1181. } else {
  1182. # pure CGI script, serving single request
  1183. exit;
  1184. }
  1185. ## ======================================================================
  1186. ## action links
  1187. # possible values of extra options
  1188. # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
  1189. # -replay => 1 - start from a current view (replay with modifications)
  1190. # -path_info => 0|1 - don't use/use path_info URL (if possible)
  1191. # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
  1192. sub href {
  1193. my %params = @_;
  1194. # default is to use -absolute url() i.e. $my_uri
  1195. my $href = $params{-full} ? $my_url : $my_uri;
  1196. # implicit -replay, must be first of implicit params
  1197. $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
  1198. $params{'project'} = $project unless exists $params{'project'};
  1199. if ($params{-replay}) {
  1200. while (my ($name, $symbol) = each %cgi_param_mapping) {
  1201. if (!exists $params{$name}) {
  1202. $params{$name} = $input_params{$name};
  1203. }
  1204. }
  1205. }
  1206. my $use_pathinfo = gitweb_check_feature('pathinfo');
  1207. if (defined $params{'project'} &&
  1208. (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
  1209. # try to put as many parameters as possible in PATH_INFO:
  1210. # - project name
  1211. # - action
  1212. # - hash_parent or hash_parent_base:/file_parent
  1213. # - hash or hash_base:/filename
  1214. # - the snapshot_format as an appropriate suffix
  1215. # When the script is the root DirectoryIndex for the domain,
  1216. # $href here would be something like http://gitweb.example.com/
  1217. # Thus, we strip any trailing / from $href, to spare us double
  1218. # slashes in the final URL
  1219. $href =~ s,/$,,;
  1220. # Then add the project name, if present
  1221. $href .= "/".esc_path_info($params{'project'});
  1222. delete $params{'project'};
  1223. # since we destructively absorb parameters, we keep this
  1224. # boolean that remembers if we're handling a snapshot
  1225. my $is_snapshot = $params{'action'} eq 'snapshot';
  1226. # Summary just uses the project path URL, any other action is
  1227. # added to the URL
  1228. if (defined $params{'action'}) {
  1229. $href .= "/".esc_path_info($params{'action'})
  1230. unless $params{'action'} eq 'summary';
  1231. delete $params{'action'};
  1232. }
  1233. # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
  1234. # stripping nonexistent or useless pieces
  1235. $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
  1236. || $params{'hash_parent'} || $params{'hash'});
  1237. if (defined $params{'hash_base'}) {
  1238. if (defined $params{'hash_parent_base'}) {
  1239. $href .= esc_path_info($params{'hash_parent_base'});
  1240. # skip the file_parent if it's the same as the file_name
  1241. if (defined $params{'file_parent'}) {
  1242. if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
  1243. delete $params{'file_parent'};
  1244. } elsif ($params{'file_parent'} !~ /\.\./) {
  1245. $href .= ":/".esc_path_info($params{'file_parent'});
  1246. delete $params{'file_parent'};
  1247. }
  1248. }
  1249. $href .= "..";
  1250. delete $params{'hash_parent'};
  1251. delete $params{'hash_parent_base'};
  1252. } elsif (defined $params{'hash_parent'}) {
  1253. $href .= esc_path_info($params{'hash_parent'}). "..";
  1254. delete $params{'hash_parent'};
  1255. }
  1256. $href .= esc_path_info($params{'hash_base'});
  1257. if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
  1258. $href .= ":/".esc_path_info($params{'file_name'});
  1259. delete $params{'file_name'};
  1260. }
  1261. delete $params{'hash'};
  1262. delete $params{'hash_base'};
  1263. } elsif (defined $params{'hash'}) {
  1264. $href .= esc_path_info($params{'hash'});
  1265. delete $params{'hash'};
  1266. }
  1267. # If the action was a snapshot, we can absorb the
  1268. # snapshot_format parameter too
  1269. if ($is_snapshot) {
  1270. my $fmt = $params{'snapshot_format'};
  1271. # snapshot_format should always be defined when href()
  1272. # is called, but just in case some code forgets, we
  1273. # fall back to the default
  1274. $fmt ||= $snapshot_fmts[0];
  1275. $href .= $known_snapshot_formats{$fmt}{'suffix'};
  1276. delete $params{'snapshot_format'};
  1277. }
  1278. }
  1279. # now encode the parameters explicitly
  1280. my @result = ();
  1281. for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
  1282. my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
  1283. if (defined $params{$name}) {
  1284. if (ref($params{$name}) eq "ARRAY") {
  1285. foreach my $par (@{$params{$name}}) {
  1286. push @result, $symbol . "=" . esc_param($par);
  1287. }
  1288. } else {
  1289. push @result, $symbol . "=" . esc_param($params{$name});
  1290. }
  1291. }
  1292. }
  1293. $href .= "?" . join(';', @result) if scalar @result;
  1294. # final transformation: trailing spaces must be escaped (URI-encoded)
  1295. $href =~ s/(\s+)$/CGI::escape($1)/e;
  1296. if ($params{-anchor}) {
  1297. $href .= "#".esc_param($params{-anchor});
  1298. }
  1299. return $href;
  1300. }
  1301. ## ======================================================================
  1302. ## validation, quoting/unquoting and escaping
  1303. sub is_valid_action {
  1304. my $input = shift;
  1305. return undef unless exists $actions{$input};
  1306. return 1;
  1307. }
  1308. sub is_valid_project {
  1309. my $input = shift;
  1310. return unless defined $input;
  1311. if (!is_valid_pathname($input) ||
  1312. !(-d "$projectroot/$input") ||
  1313. !check_export_ok("$projectroot/$input") ||
  1314. ($strict_export && !project_in_list($input))) {
  1315. return undef;
  1316. } else {
  1317. return 1;
  1318. }
  1319. }
  1320. sub is_valid_pathname {
  1321. my $input = shift;
  1322. return undef unless defined $input;
  1323. # no '.' or '..' as elements of path, i.e. no '.' or '..'
  1324. # at the beginning, at the end, and between slashes.
  1325. # also this catches doubled slashes
  1326. if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
  1327. return undef;
  1328. }
  1329. # no null characters
  1330. if ($input =~ m!\0!) {
  1331. return undef;
  1332. }
  1333. return 1;
  1334. }
  1335. sub is_valid_ref_format {
  1336. my $input = shift;
  1337. return undef unless defined $input;
  1338. # restrictions on ref name according to git-check-ref-format
  1339. if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
  1340. return undef;
  1341. }
  1342. return 1;
  1343. }
  1344. sub is_valid_refname {
  1345. my $input = shift;
  1346. return undef unless defined $input;
  1347. # textual hashes are O.K.
  1348. if ($input =~ m/^$oid_regex$/) {
  1349. return 1;
  1350. }
  1351. # it must be correct pathname
  1352. is_valid_pathname($input) or return undef;
  1353. # check git-check-ref-format restrictions
  1354. is_valid_ref_format($input) or return undef;
  1355. return 1;
  1356. }
  1357. # decode sequences of octets in utf8 into Perl's internal form,
  1358. # which is utf-8 with utf8 flag set if needed. gitweb writes out
  1359. # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
  1360. sub to_utf8 {
  1361. my $str = shift;
  1362. return undef unless defined $str;
  1363. if (utf8::is_utf8($str) || utf8::decode($str)) {
  1364. return $str;
  1365. } else {
  1366. return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
  1367. }
  1368. }
  1369. # quote unsafe chars, but keep the slash, even when it's not
  1370. # correct, but quoted slashes look too horrible in bookmarks
  1371. sub esc_param {
  1372. my $str = shift;
  1373. return undef unless defined $str;
  1374. $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
  1375. $str =~ s/ /\+/g;
  1376. return $str;
  1377. }
  1378. # the quoting rules for path_info fragment are slightly different
  1379. sub esc_path_info {
  1380. my $str = shift;
  1381. return undef unless defined $str;
  1382. # path_info doesn't treat '+' as space (specially), but '?' must be escaped
  1383. $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
  1384. return $str;
  1385. }
  1386. # quote unsafe chars in whole URL, so some characters cannot be quoted
  1387. sub esc_url {
  1388. my $str = shift;
  1389. return undef unless defined $str;
  1390. $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
  1391. $str =~ s/ /\+/g;
  1392. return $str;
  1393. }
  1394. # quote unsafe characters in HTML attributes
  1395. sub esc_attr {
  1396. # for XHTML conformance escaping '"' to '&quot;' is not enough
  1397. return esc_html(@_);
  1398. }
  1399. # replace invalid utf8 character with SUBSTITUTION sequence
  1400. sub esc_html {
  1401. my $str = shift;
  1402. my %opts = @_;
  1403. return undef unless defined $str;
  1404. $str = to_utf8($str);
  1405. $str = $cgi->escapeHTML($str);
  1406. if ($opts{'-nbsp'}) {
  1407. $str =~ s/ /&nbsp;/g;
  1408. }
  1409. $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
  1410. return $str;
  1411. }
  1412. # quote control characters and escape filename to HTML
  1413. sub esc_path {
  1414. my $str = shift;
  1415. my %opts = @_;
  1416. return undef unless defined $str;
  1417. $str = to_utf8($str);
  1418. $str = $cgi->escapeHTML($str);
  1419. if ($opts{'-nbsp'}) {
  1420. $str =~ s/ /&nbsp;/g;
  1421. }
  1422. $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
  1423. return $str;
  1424. }
  1425. # Sanitize for use in XHTML + application/xml+xhtml (valid XML 1.0)
  1426. sub sanitize {
  1427. my $str = shift;
  1428. return undef unless defined $str;
  1429. $str = to_utf8($str);
  1430. $str =~ s|([[:cntrl:]])|(index("\t\n\r", $1) != -1 ? $1 : quot_cec($1))|eg;
  1431. return $str;
  1432. }
  1433. # Make control characters "printable", using character escape codes (CEC)
  1434. sub quot_cec {
  1435. my $cntrl = shift;
  1436. my %opts = @_;
  1437. my %es = ( # character escape codes, aka escape sequences
  1438. "\t" => '\t', # tab (HT)
  1439. "\n" => '\n', # line feed (LF)
  1440. "\r" => '\r', # carriage return (CR)
  1441. "\f" => '\f', # form feed (FF)
  1442. "\b" => '\b', # backspace (BS)
  1443. "\a" => '\a', # alarm (bell) (BEL)
  1444. "\e" => '\e', # escape (ESC)
  1445. "\013" => '\v', # vertical tab (VT)
  1446. "\000" => '\0', # nul character (NUL)
  1447. );
  1448. my $chr = ( (exists $es{$cntrl})
  1449. ? $es{$cntrl}
  1450. : sprintf('\%2x', ord($cntrl)) );
  1451. if ($opts{-nohtml}) {
  1452. return $chr;
  1453. } else {
  1454. return "<span class=\"cntrl\">$chr</span>";
  1455. }
  1456. }
  1457. # Alternatively use unicode control pictures codepoints,
  1458. # Unicode "printable representation" (PR)
  1459. sub quot_upr {
  1460. my $cntrl = shift;
  1461. my %opts = @_;
  1462. my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
  1463. if ($opts{-nohtml}) {
  1464. return $chr;
  1465. } else {
  1466. return "<span class=\"cntrl\">$chr</span>";
  1467. }
  1468. }
  1469. # git may return quoted and escaped filenames
  1470. sub unquote {
  1471. my $str = shift;
  1472. sub unq {
  1473. my $seq = shift;
  1474. my %es = ( # character escape codes, aka escape sequences
  1475. 't' => "\t", # tab (HT, TAB)
  1476. 'n' => "\n", # newline (NL)
  1477. 'r' => "\r", # return (CR)
  1478. 'f' => "\f", # form feed (FF)
  1479. 'b' => "\b", # backspace (BS)
  1480. 'a' => "\a", # alarm (bell) (BEL)
  1481. 'e' => "\e", # escape (ESC)
  1482. 'v' => "\013", # vertical tab (VT)
  1483. );
  1484. if ($seq =~ m/^[0-7]{1,3}$/) {
  1485. # octal char sequence
  1486. return chr(oct($seq));
  1487. } elsif (exists $es{$seq}) {
  1488. # C escape sequence, aka character escape code
  1489. return $es{$seq};
  1490. }
  1491. # quoted ordinary character
  1492. return $seq;
  1493. }
  1494. if ($str =~ m/^"(.*)"$/) {
  1495. # needs unquoting
  1496. $str = $1;
  1497. $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
  1498. }
  1499. return $str;
  1500. }
  1501. # escape tabs (convert tabs to spaces)
  1502. sub untabify {
  1503. my $line = shift;
  1504. while ((my $pos = index($line, "\t")) != -1) {
  1505. if (my $count = (8 - ($pos % 8))) {
  1506. my $spaces = ' ' x $count;
  1507. $line =~ s/\t/$spaces/;
  1508. }
  1509. }
  1510. return $line;
  1511. }
  1512. sub project_in_list {
  1513. my $project = shift;
  1514. my @list = git_get_projects_list();
  1515. return @list && scalar(grep { $_->{'path'} eq $project } @list);
  1516. }
  1517. ## ----------------------------------------------------------------------
  1518. ## HTML aware string manipulation
  1519. # Try to chop given string on a word boundary between position
  1520. # $len and $len+$add_len. If there is no word boundary there,
  1521. # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
  1522. # (marking chopped part) would be longer than given string.
  1523. sub chop_str {
  1524. my $str = shift;
  1525. my $len = shift;
  1526. my $add_len = shift || 10;
  1527. my $where = shift || 'right'; # 'left' | 'center' | 'right'
  1528. # Make sure perl knows it is utf8 encoded so we don't
  1529. # cut in the middle of a utf8 multibyte char.
  1530. $str = to_utf8($str);
  1531. # allow only $len chars, but don't cut a word if it would fit in $add_len
  1532. # if it doesn't fit, cut it if it's still longer than the dots we would add
  1533. # remove chopped character entities entirely
  1534. # when chopping in the middle, distribute $len into left and right part
  1535. # return early if chopping wouldn't make string shorter
  1536. if ($where eq 'center') {
  1537. return $str if ($len + 5 >= length($str)); # filler is length 5
  1538. $len = int($len/2);
  1539. } else {
  1540. return $str if ($len + 4 >= length($str)); # filler is length 4
  1541. }
  1542. # regexps: ending and beginning with word part up to $add_len
  1543. my $endre = qr/.{$len}\w{0,$add_len}/;
  1544. my $begre = qr/\w{0,$add_len}.{$len}/;
  1545. if ($where eq 'left') {
  1546. $str =~ m/^(.*?)($begre)$/;
  1547. my ($lead, $body) = ($1, $2);
  1548. if (length($lead) > 4) {
  1549. $lead = " ...";
  1550. }
  1551. return "$lead$body";
  1552. } elsif ($where eq 'center') {
  1553. $str =~ m/^($endre)(.*)$/;
  1554. my ($left, $str) = ($1, $2);
  1555. $str =~ m/^(.*?)($begre)$/;
  1556. my ($mid, $right) = ($1, $2);
  1557. if (length($mid) > 5) {
  1558. $mid = " ... ";
  1559. }
  1560. return "$left$mid$right";
  1561. } else {
  1562. $str =~ m/^($endre)(.*)$/;
  1563. my $body = $1;
  1564. my $tail = $2;
  1565. if (length($tail) > 4) {
  1566. $tail = "... ";
  1567. }
  1568. return "$body$tail";
  1569. }
  1570. }
  1571. # takes the same arguments as chop_str, but also wraps a <span> around the
  1572. # result with a title attribute if it does get chopped. Additionally, the
  1573. # string is HTML-escaped.
  1574. sub chop_and_escape_str {
  1575. my ($str) = @_;
  1576. my $chopped = chop_str(@_);
  1577. $str = to_utf8($str);
  1578. if ($chopped eq $str) {
  1579. return esc_html($chopped);
  1580. } else {
  1581. $str =~ s/[[:cntrl:]]/?/g;
  1582. return $cgi->span({-title=>$str}, esc_html($chopped));
  1583. }
  1584. }
  1585. # Highlight selected fragments of string, using given CSS class,
  1586. # and escape HTML. It is assumed that fragments do not overlap.
  1587. # Regions are passed as list of pairs (array references).
  1588. #
  1589. # Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
  1590. # '<span class="mark">foo</span>bar'
  1591. sub esc_html_hl_regions {
  1592. my ($str, $css_class, @sel) = @_;
  1593. my %opts = grep { ref($_) ne 'ARRAY' } @sel;
  1594. @sel = grep { ref($_) eq 'ARRAY' } @sel;
  1595. return esc_html($str, %opts) unless @sel;
  1596. my $out = '';
  1597. my $pos = 0;
  1598. for my $s (@sel) {
  1599. my ($begin, $end) = @$s;
  1600. # Don't create empty <span> elements.
  1601. next if $end <= $begin;
  1602. my $escaped = esc_html(substr($str, $begin, $end - $begin),
  1603. %opts);
  1604. $out .= esc_html(substr($str, $pos, $begin - $pos), %opts)
  1605. if ($begin - $pos > 0);
  1606. $out .= $cgi->span({-class => $css_class}, $escaped);
  1607. $pos = $end;
  1608. }
  1609. $out .= esc_html(substr($str, $pos), %opts)
  1610. if ($pos < length($str));
  1611. return $out;
  1612. }
  1613. # return positions of beginning and end of each match
  1614. sub matchpos_list {
  1615. my ($str, $regexp) = @_;
  1616. return unless (defined $str && defined $regexp);
  1617. my @matches;
  1618. while ($str =~ /$regexp/g) {
  1619. push @matches, [$-[0], $+[0]];
  1620. }
  1621. return @matches;
  1622. }
  1623. # highlight match (if any), and escape HTML
  1624. sub esc_html_match_hl {
  1625. my ($str, $regexp) = @_;
  1626. return esc_html($str) unless defined $regexp;
  1627. my @matches = matchpos_list($str, $regexp);
  1628. return esc_html($str) unless @matches;
  1629. return esc_html_hl_regions($str, 'match', @matches);
  1630. }
  1631. # highlight match (if any) of shortened string, and escape HTML
  1632. sub esc_html_match_hl_chopped {
  1633. my ($str, $chopped, $regexp) = @_;
  1634. return esc_html_match_hl($str, $regexp) unless defined $chopped;
  1635. my @matches = matchpos_list($str, $regexp);
  1636. return esc_html($chopped) unless @matches;
  1637. # filter matches so that we mark chopped string
  1638. my $tail = "... "; # see chop_str
  1639. unless ($chopped =~ s/\Q$tail\E$//) {
  1640. $tail = '';
  1641. }
  1642. my $chop_len = length($chopped);
  1643. my $tail_len = length($tail);
  1644. my @filtered;
  1645. for my $m (@matches) {
  1646. if ($m->[0] > $chop_len) {
  1647. push @filtered, [ $chop_len, $chop_len + $tail_len ] if ($tail_len > 0);
  1648. last;
  1649. } elsif ($m->[1] > $chop_len) {
  1650. push @filtered, [ $m->[0], $chop_len + $tail_len ];
  1651. last;
  1652. }
  1653. push @filtered, $m;
  1654. }
  1655. return esc_html_hl_regions($chopped . $tail, 'match', @filtered);
  1656. }
  1657. ## ----------------------------------------------------------------------
  1658. ## functions returning short strings
  1659. # CSS class for given age value (in seconds)
  1660. sub age_class {
  1661. my $age = shift;
  1662. if (!defined $age) {
  1663. return "noage";
  1664. } elsif ($age < 60*60*2) {
  1665. return "age0";
  1666. } elsif ($age < 60*60*24*2) {
  1667. return "age1";
  1668. } else {
  1669. return "age2";
  1670. }
  1671. }
  1672. # convert age in seconds to "nn units ago" string
  1673. sub age_string {
  1674. my $age = shift;
  1675. my $age_str;
  1676. if ($age > 60*60*24*365*2) {
  1677. $age_str = (int $age/60/60/24/365);
  1678. $age_str .= " years ago";
  1679. } elsif ($age > 60*60*24*(365/12)*2) {
  1680. $age_str = int $age/60/60/24/(365/12);
  1681. $age_str .= " months ago";
  1682. } elsif ($age > 60*60*24*7*2) {
  1683. $age_str = int $age/60/60/24/7;
  1684. $age_str .= " weeks ago";
  1685. } elsif ($age > 60*60*24*2) {
  1686. $age_str = int $age/60/60/24;
  1687. $age_str .= " days ago";
  1688. } elsif ($age > 60*60*2) {
  1689. $age_str = int $age/60/60;
  1690. $age_str .= " hours ago";
  1691. } elsif ($age > 60*2) {
  1692. $age_str = int $age/60;
  1693. $age_str .= " min ago";
  1694. } elsif ($age > 2) {
  1695. $age_str = int $age;
  1696. $age_str .= " sec ago";
  1697. } else {
  1698. $age_str .= " right now";
  1699. }
  1700. return $age_str;
  1701. }
  1702. use constant {
  1703. S_IFINVALID => 0030000,
  1704. S_IFGITLINK => 0160000,
  1705. };
  1706. # submodule/subproject, a commit object reference
  1707. sub S_ISGITLINK {
  1708. my $mode = shift;
  1709. return (($mode & S_IFMT) == S_IFGITLINK)
  1710. }
  1711. # convert file mode in octal to symbolic file mode string
  1712. sub mode_str {
  1713. my $mode = oct shift;
  1714. if (S_ISGITLINK($mode)) {
  1715. return 'm---------';
  1716. } elsif (S_ISDIR($mode & S_IFMT)) {
  1717. return 'drwxr-xr-x';
  1718. } elsif (S_ISLNK($mode)) {
  1719. return 'lrwxrwxrwx';
  1720. } elsif (S_ISREG($mode)) {
  1721. # git cares only about the executable bit
  1722. if ($mode & S_IXUSR) {
  1723. return '-rwxr-xr-x';
  1724. } else {
  1725. return '-rw-r--r--';
  1726. };
  1727. } else {
  1728. return '----------';
  1729. }
  1730. }
  1731. # convert file mode in octal to file type string
  1732. sub file_type {
  1733. my $mode = shift;
  1734. if ($mode !~ m/^[0-7]+$/) {
  1735. return $mode;
  1736. } else {
  1737. $mode = oct $mode;
  1738. }
  1739. if (S_ISGITLINK($mode)) {
  1740. return "submodule";
  1741. } elsif (S_ISDIR($mode & S_IFMT)) {
  1742. return "directory";
  1743. } elsif (S_ISLNK($mode)) {
  1744. return "symlink";
  1745. } elsif (S_ISREG($mode)) {
  1746. return "file";
  1747. } else {
  1748. return "unknown";
  1749. }
  1750. }
  1751. # convert file mode in octal to file type description string
  1752. sub file_type_long {
  1753. my $mode = shift;
  1754. if ($mode !~ m/^[0-7]+$/) {
  1755. return $mode;
  1756. } else {
  1757. $mode = oct $mode;
  1758. }
  1759. if (S_ISGITLINK($mode)) {
  1760. return "submodule";
  1761. } elsif (S_ISDIR($mode & S_IFMT)) {
  1762. return "directory";
  1763. } elsif (S_ISLNK($mode)) {
  1764. return "symlink";
  1765. } elsif (S_ISREG($mode)) {
  1766. if ($mode & S_IXUSR) {
  1767. return "executable";
  1768. } else {
  1769. return "file";
  1770. };
  1771. } else {
  1772. return "unknown";
  1773. }
  1774. }
  1775. ## ----------------------------------------------------------------------
  1776. ## functions returning short HTML fragments, or transforming HTML fragments
  1777. ## which don't belong to other sections
  1778. # format line of commit message.
  1779. sub format_log_line_html {
  1780. my $line = shift;
  1781. # Potentially abbreviated OID.
  1782. my $regex = oid_nlen_regex("7,64");
  1783. $line = esc_html($line, -nbsp=>1);
  1784. $line =~ s{
  1785. \b
  1786. (
  1787. # The output of "git describe", e.g. v2.10.0-297-gf6727b0
  1788. # or hadoop-20160921-113441-20-g094fb7d
  1789. (?<!-) # see strbuf_check_tag_ref(). Tags can't start with -
  1790. [A-Za-z0-9.-]+
  1791. (?!\.) # refs can't end with ".", see check_refname_format()
  1792. -g$regex
  1793. |
  1794. # Just a normal looking Git SHA1
  1795. $regex
  1796. )
  1797. \b
  1798. }{
  1799. $cgi->a({-href => href(action=>"object", hash=>$1),
  1800. -class => "text"}, $1);
  1801. }egx;
  1802. return $line;
  1803. }
  1804. # format marker of refs pointing to given object
  1805. # the destination action is chosen based on object type and current context:
  1806. # - for annotated tags, we choose the tag view unless it's the current view
  1807. # already, in which case we go to shortlog view
  1808. # - for other refs, we keep the current view if we're in history, shortlog or
  1809. # log view, and select shortlog otherwise
  1810. sub format_ref_marker {
  1811. my ($refs, $id) = @_;
  1812. my $markers = '';
  1813. if (defined $refs->{$id}) {
  1814. foreach my $ref (@{$refs->{$id}}) {
  1815. # this code exploits the fact that non-lightweight tags are the
  1816. # only indirect objects, and that they are the only objects for which
  1817. # we want to use tag instead of shortlog as action
  1818. my ($type, $name) = qw();
  1819. my $indirect = ($ref =~ s/\^\{\}$//);
  1820. # e.g. tags/v2.6.11 or heads/next
  1821. if ($ref =~ m!^(.*?)s?/(.*)$!) {
  1822. $type = $1;
  1823. $name = $2;
  1824. } else {
  1825. $type = "ref";
  1826. $name = $ref;
  1827. }
  1828. my $class = $type;
  1829. $class .= " indirect" if $indirect;
  1830. my $dest_action = "shortlog";
  1831. if ($indirect) {
  1832. $dest_action = "tag" unless $action eq "tag";
  1833. } elsif ($action =~ /^(history|(short)?log)$/) {
  1834. $dest_action = $action;
  1835. }
  1836. my $dest = "";
  1837. $dest .= "refs/" unless $ref =~ m!^refs/!;
  1838. $dest .= $ref;
  1839. my $link = $cgi->a({
  1840. -href => href(
  1841. action=>$dest_action,
  1842. hash=>$dest
  1843. )}, esc_html($name));
  1844. $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
  1845. $link . "</span>";
  1846. }
  1847. }
  1848. if ($markers) {
  1849. return ' <span class="refs">'. $markers . '</span>';
  1850. } else {
  1851. return "";
  1852. }
  1853. }
  1854. # format, perhaps shortened and with markers, title line
  1855. sub format_subject_html {
  1856. my ($long, $short, $href, $extra) = @_;
  1857. $extra = '' unless defined($extra);
  1858. if (length($short) < length($long)) {
  1859. $long =~ s/[[:cntrl:]]/?/g;
  1860. return $cgi->a({-href => $href, -class => "list subject",
  1861. -title => to_utf8($long)},
  1862. esc_html($short)) . $extra;
  1863. } else {
  1864. return $cgi->a({-href => $href, -class => "list subject"},
  1865. esc_html($long)) . $extra;
  1866. }
  1867. }
  1868. # Rather than recomputing the url for an email multiple times, we cache it
  1869. # after the first hit. This gives a visible benefit in views where the avatar
  1870. # for the same email is used repeatedly (e.g. shortlog).
  1871. # The cache is shared by all avatar engines (currently gravatar only), which
  1872. # are free to use it as preferred. Since only one avatar engine is used for any
  1873. # given page, there's no risk for cache conflicts.
  1874. our %avatar_cache = ();
  1875. # Compute the picon url for a given email, by using the picon search service over at
  1876. # http://www.cs.indiana.edu/picons/search.html
  1877. sub picon_url {
  1878. my $email = lc shift;
  1879. if (!$avatar_cache{$email}) {
  1880. my ($user, $domain) = split('@', $email);
  1881. $avatar_cache{$email} =
  1882. "//www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
  1883. "$domain/$user/" .
  1884. "users+domains+unknown/up/single";
  1885. }
  1886. return $avatar_cache{$email};
  1887. }
  1888. # Compute the gravatar url for a given email, if it's not in the cache already.
  1889. # Gravatar stores only the part of the URL before the size, since that's the
  1890. # one computationally more expensive. This also allows reuse of the cache for
  1891. # different sizes (for this particular engine).
  1892. sub gravatar_url {
  1893. my $email = lc shift;
  1894. my $size = shift;
  1895. $avatar_cache{$email} ||=
  1896. "//www.gravatar.com/avatar/" .
  1897. md5_hex($email) . "?s=";
  1898. return $avatar_cache{$email} . $size;
  1899. }
  1900. # Insert an avatar for the given $email at the given $size if the feature
  1901. # is enabled.
  1902. sub git_get_avatar {
  1903. my ($email, %opts) = @_;
  1904. my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
  1905. my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
  1906. $opts{-size} ||= 'default';
  1907. my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
  1908. my $url = "";
  1909. if ($git_avatar eq 'gravatar') {
  1910. $url = gravatar_url($email, $size);
  1911. } elsif ($git_avatar eq 'picon') {
  1912. $url = picon_url($email);
  1913. }
  1914. # Other providers can be added by extending the if chain, defining $url
  1915. # as needed. If no variant puts something in $url, we assume avatars
  1916. # are completely disabled/unavailable.
  1917. if ($url) {
  1918. return $pre_white .
  1919. "<img width=\"$size\" " .
  1920. "class=\"avatar\" " .
  1921. "src=\"".esc_url($url)."\" " .
  1922. "alt=\"\" " .
  1923. "/>" . $post_white;
  1924. } else {
  1925. return "";
  1926. }
  1927. }
  1928. sub format_search_author {
  1929. my ($author, $searchtype, $displaytext) = @_;
  1930. my $have_search = gitweb_check_feature('search');
  1931. if ($have_search) {
  1932. my $performed = "";
  1933. if ($searchtype eq 'author') {
  1934. $performed = "authored";
  1935. } elsif ($searchtype eq 'committer') {
  1936. $performed = "committed";
  1937. }
  1938. return $cgi->a({-href => href(action=>"search", hash=>$hash,
  1939. searchtext=>$author,
  1940. searchtype=>$searchtype), class=>"list",
  1941. title=>"Search for commits $performed by $author"},
  1942. $displaytext);
  1943. } else {
  1944. return $displaytext;
  1945. }
  1946. }
  1947. # format the author name of the given commit with the given tag
  1948. # the author name is chopped and escaped according to the other
  1949. # optional parameters (see chop_str).
  1950. sub format_author_html {
  1951. my $tag = shift;
  1952. my $co = shift;
  1953. my $author = chop_and_escape_str($co->{'author_name'}, @_);
  1954. return "<$tag class=\"author\">" .
  1955. format_search_author($co->{'author_name'}, "author",
  1956. git_get_avatar($co->{'author_email'}, -pad_after => 1) .
  1957. $author) .
  1958. "</$tag>";
  1959. }
  1960. # format git diff header line, i.e. "diff --(git|combined|cc) ..."
  1961. sub format_git_diff_header_line {
  1962. my $line = shift;
  1963. my $diffinfo = shift;
  1964. my ($from, $to) = @_;
  1965. if ($diffinfo->{'nparents'}) {
  1966. # combined diff
  1967. $line =~ s!^(diff (.*?) )"?.*$!$1!;
  1968. if ($to->{'href'}) {
  1969. $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
  1970. esc_path($to->{'file'}));
  1971. } else { # file was deleted (no href)
  1972. $line .= esc_path($to->{'file'});
  1973. }
  1974. } else {
  1975. # "ordinary" diff
  1976. $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
  1977. if ($from->{'href'}) {
  1978. $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
  1979. 'a/' . esc_path($from->{'file'}));
  1980. } else { # file was added (no href)
  1981. $line .= 'a/' . esc_path($from->{'file'});
  1982. }
  1983. $line .= ' ';
  1984. if ($to->{'href'}) {
  1985. $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
  1986. 'b/' . esc_path($to->{'file'}));
  1987. } else { # file was deleted
  1988. $line .= 'b/' . esc_path($to->{'file'});
  1989. }
  1990. }
  1991. return "<div class=\"diff header\">$line</div>\n";
  1992. }
  1993. # format extended diff header line, before patch itself
  1994. sub format_extended_diff_header_line {
  1995. my $line = shift;
  1996. my $diffinfo = shift;
  1997. my ($from, $to) = @_;
  1998. # match <path>
  1999. if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
  2000. $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
  2001. esc_path($from->{'file'}));
  2002. }
  2003. if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
  2004. $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
  2005. esc_path($to->{'file'}));
  2006. }
  2007. # match single <mode>
  2008. if ($line =~ m/\s(\d{6})$/) {
  2009. $line .= '<span class="info"> (' .
  2010. file_type_long($1) .
  2011. ')</span>';
  2012. }
  2013. # match <hash>
  2014. if ($line =~ oid_nlen_prefix_infix_regex($sha1_len, "index ", ",") |
  2015. $line =~ oid_nlen_prefix_infix_regex($sha256_len, "index ", ",")) {
  2016. # can match only for combined diff
  2017. $line = 'index ';
  2018. for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
  2019. if ($from->{'href'}[$i]) {
  2020. $line .= $cgi->a({-href=>$from->{'href'}[$i],
  2021. -class=>"hash"},
  2022. substr($diffinfo->{'from_id'}[$i],0,7));
  2023. } else {
  2024. $line .= '0' x 7;
  2025. }
  2026. # separator
  2027. $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
  2028. }
  2029. $line .= '..';
  2030. if ($to->{'href'}) {
  2031. $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
  2032. substr($diffinfo->{'to_id'},0,7));
  2033. } else {
  2034. $line .= '0' x 7;
  2035. }
  2036. } elsif ($line =~ oid_nlen_prefix_infix_regex($sha1_len, "index ", "..") |
  2037. $line =~ oid_nlen_prefix_infix_regex($sha256_len, "index ", "..")) {
  2038. # can match only for ordinary diff
  2039. my ($from_link, $to_link);
  2040. if ($from->{'href'}) {
  2041. $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
  2042. substr($diffinfo->{'from_id'},0,7));
  2043. } else {
  2044. $from_link = '0' x 7;
  2045. }
  2046. if ($to->{'href'}) {
  2047. $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
  2048. substr($diffinfo->{'to_id'},0,7));
  2049. } else {
  2050. $to_link = '0' x 7;
  2051. }
  2052. my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
  2053. $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
  2054. }
  2055. return $line . "<br/>\n";
  2056. }
  2057. # format from-file/to-file diff header
  2058. sub format_diff_from_to_header {
  2059. my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
  2060. my $line;
  2061. my $result = '';
  2062. $line = $from_line;
  2063. #assert($line =~ m/^---/) if DEBUG;
  2064. # no extra formatting for "^--- /dev/null"
  2065. if (! $diffinfo->{'nparents'}) {
  2066. # ordinary (single parent) diff
  2067. if ($line =~ m!^--- "?a/!) {
  2068. if ($from->{'href'}) {
  2069. $line = '--- a/' .
  2070. $cgi->a({-href=>$from->{'href'}, -class=>"path"},
  2071. esc_path($from->{'file'}));
  2072. } else {
  2073. $line = '--- a/' .
  2074. esc_path($from->{'file'});
  2075. }
  2076. }
  2077. $result .= qq!<div class="diff from_file">$line</div>\n!;
  2078. } else {
  2079. # combined diff (merge commit)
  2080. for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
  2081. if ($from->{'href'}[$i]) {
  2082. $line = '--- ' .
  2083. $cgi->a({-href=>href(action=>"blobdiff",
  2084. hash_parent=>$diffinfo->{'from_id'}[$i],
  2085. hash_parent_base=>$parents[$i],
  2086. file_parent=>$from->{'file'}[$i],
  2087. hash=>$diffinfo->{'to_id'},
  2088. hash_base=>$hash,
  2089. file_name=>$to->{'file'}),
  2090. -class=>"path",
  2091. -title=>"diff" . ($i+1)},
  2092. $i+1) .
  2093. '/' .
  2094. $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
  2095. esc_path($from->{'file'}[$i]));
  2096. } else {
  2097. $line = '--- /dev/null';
  2098. }
  2099. $result .= qq!<div class="diff from_file">$line</div>\n!;
  2100. }
  2101. }
  2102. $line = $to_line;
  2103. #assert($line =~ m/^\+\+\+/) if DEBUG;
  2104. # no extra formatting for "^+++ /dev/null"
  2105. if ($line =~ m!^\+\+\+ "?b/!) {
  2106. if ($to->{'href'}) {
  2107. $line = '+++ b/' .
  2108. $cgi->a({-href=>$to->{'href'}, -class=>"path"},
  2109. esc_path($to->{'file'}));
  2110. } else {
  2111. $line = '+++ b/' .
  2112. esc_path($to->{'file'});
  2113. }
  2114. }
  2115. $result .= qq!<div class="diff to_file">$line</div>\n!;
  2116. return $result;
  2117. }
  2118. # create note for patch simplified by combined diff
  2119. sub format_diff_cc_simplified {
  2120. my ($diffinfo, @parents) = @_;
  2121. my $result = '';
  2122. $result .= "<div class=\"diff header\">" .
  2123. "diff --cc ";
  2124. if (!is_deleted($diffinfo)) {
  2125. $result .= $cgi->a({-href => href(action=>"blob",
  2126. hash_base=>$hash,
  2127. hash=>$diffinfo->{'to_id'},
  2128. file_name=>$diffinfo->{'to_file'}),
  2129. -class => "path"},
  2130. esc_path($diffinfo->{'to_file'}));
  2131. } else {
  2132. $result .= esc_path($diffinfo->{'to_file'});
  2133. }
  2134. $result .= "</div>\n" . # class="diff header"
  2135. "<div class=\"diff nodifferences\">" .
  2136. "Simple merge" .
  2137. "</div>\n"; # class="diff nodifferences"
  2138. return $result;
  2139. }
  2140. sub diff_line_class {
  2141. my ($line, $from, $to) = @_;
  2142. # ordinary diff
  2143. my $num_sign = 1;
  2144. # combined diff
  2145. if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
  2146. $num_sign = scalar @{$from->{'href'}};
  2147. }
  2148. my @diff_line_classifier = (
  2149. { regexp => qr/^\@\@{$num_sign} /, class => "chunk_header"},
  2150. { regexp => qr/^\\/, class => "incomplete" },
  2151. { regexp => qr/^ {$num_sign}/, class => "ctx" },
  2152. # classifier for context must come before classifier add/rem,
  2153. # or we would have to use more complicated regexp, for example
  2154. # qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;
  2155. { regexp => qr/^[+ ]{$num_sign}/, class => "add" },
  2156. { regexp => qr/^[- ]{$num_sign}/, class => "rem" },
  2157. );
  2158. for my $clsfy (@diff_line_classifier) {
  2159. return $clsfy->{'class'}
  2160. if ($line =~ $clsfy->{'regexp'});
  2161. }
  2162. # fallback
  2163. return "";
  2164. }
  2165. # assumes that $from and $to are defined and correctly filled,
  2166. # and that $line holds a line of chunk header for unified diff
  2167. sub format_unidiff_chunk_header {
  2168. my ($line, $from, $to) = @_;
  2169. my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
  2170. $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
  2171. $from_lines = 0 unless defined $from_lines;
  2172. $to_lines = 0 unless defined $to_lines;
  2173. if ($from->{'href'}) {
  2174. $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
  2175. -class=>"list"}, $from_text);
  2176. }
  2177. if ($to->{'href'}) {
  2178. $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
  2179. -class=>"list"}, $to_text);
  2180. }
  2181. $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
  2182. "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
  2183. return $line;
  2184. }
  2185. # assumes that $from and $to are defined and correctly filled,
  2186. # and that $line holds a line of chunk header for combined diff
  2187. sub format_cc_diff_chunk_header {
  2188. my ($line, $from, $to) = @_;
  2189. my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
  2190. my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
  2191. @from_text = split(' ', $ranges);
  2192. for (my $i = 0; $i < @from_text; ++$i) {
  2193. ($from_start[$i], $from_nlines[$i]) =
  2194. (split(',', substr($from_text[$i], 1)), 0);
  2195. }
  2196. $to_text = pop @from_text;
  2197. $to_start = pop @from_start;
  2198. $to_nlines = pop @from_nlines;
  2199. $line = "<span class=\"chunk_info\">$prefix ";
  2200. for (my $i = 0; $i < @from_text; ++$i) {
  2201. if ($from->{'href'}[$i]) {
  2202. $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
  2203. -class=>"list"}, $from_text[$i]);
  2204. } else {
  2205. $line .= $from_text[$i];
  2206. }
  2207. $line .= " ";
  2208. }
  2209. if ($to->{'href'}) {
  2210. $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
  2211. -class=>"list"}, $to_text);
  2212. } else {
  2213. $line .= $to_text;
  2214. }
  2215. $line .= " $prefix</span>" .
  2216. "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
  2217. return $line;
  2218. }
  2219. # process patch (diff) line (not to be used for diff headers),
  2220. # returning HTML-formatted (but not wrapped) line.
  2221. # If the line is passed as a reference, it is treated as HTML and not
  2222. # esc_html()'ed.
  2223. sub format_diff_line {
  2224. my ($line, $diff_class, $from, $to) = @_;
  2225. if (ref($line)) {
  2226. $line = $$line;
  2227. } else {
  2228. chomp $line;
  2229. $line = untabify($line);
  2230. if ($from && $to && $line =~ m/^\@{2} /) {
  2231. $line = format_unidiff_chunk_header($line, $from, $to);
  2232. } elsif ($from && $to && $line =~ m/^\@{3}/) {
  2233. $line = format_cc_diff_chunk_header($line, $from, $to);
  2234. } else {
  2235. $line = esc_html($line, -nbsp=>1);
  2236. }
  2237. }
  2238. my $diff_classes = "diff";
  2239. $diff_classes .= " $diff_class" if ($diff_class);
  2240. $line = "<div class=\"$diff_classes\">$line</div>\n";
  2241. return $line;
  2242. }
  2243. # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
  2244. # linked. Pass the hash of the tree/commit to snapshot.
  2245. sub format_snapshot_links {
  2246. my ($hash) = @_;
  2247. my $num_fmts = @snapshot_fmts;
  2248. if ($num_fmts > 1) {
  2249. # A parenthesized list of links bearing format names.
  2250. # e.g. "snapshot (_tar.gz_ _zip_)"
  2251. return "snapshot (" . join(' ', map
  2252. $cgi->a({
  2253. -href => href(
  2254. action=>"snapshot",
  2255. hash=>$hash,
  2256. snapshot_format=>$_
  2257. )
  2258. }, $known_snapshot_formats{$_}{'display'})
  2259. , @snapshot_fmts) . ")";
  2260. } elsif ($num_fmts == 1) {
  2261. # A single "snapshot" link whose tooltip bears the format name.
  2262. # i.e. "_snapshot_"
  2263. my ($fmt) = @snapshot_fmts;
  2264. return
  2265. $cgi->a({
  2266. -href => href(
  2267. action=>"snapshot",
  2268. hash=>$hash,
  2269. snapshot_format=>$fmt
  2270. ),
  2271. -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
  2272. }, "snapshot");
  2273. } else { # $num_fmts == 0
  2274. return undef;
  2275. }
  2276. }
  2277. ## ......................................................................
  2278. ## functions returning values to be passed, perhaps after some
  2279. ## transformation, to other functions; e.g. returning arguments to href()
  2280. # returns hash to be passed to href to generate gitweb URL
  2281. # in -title key it returns description of link
  2282. sub get_feed_info {
  2283. my $format = shift || 'Atom';
  2284. my %res = (action => lc($format));
  2285. my $matched_ref = 0;
  2286. # feed links are possible only for project views
  2287. return unless (defined $project);
  2288. # some views should link to OPML, or to generic project feed,
  2289. # or don't have specific feed yet (so they should use generic)
  2290. return if (!$action || $action =~ /^(?:tags|heads|forks|tag|search)$/x);
  2291. my $branch = undef;
  2292. # branches refs uses 'refs/' + $get_branch_refs()[x] + '/' prefix
  2293. # (fullname) to differentiate from tag links; this also makes
  2294. # possible to detect branch links
  2295. for my $ref (get_branch_refs()) {
  2296. if ((defined $hash_base && $hash_base =~ m!^refs/\Q$ref\E/(.*)$!) ||
  2297. (defined $hash && $hash =~ m!^refs/\Q$ref\E/(.*)$!)) {
  2298. $branch = $1;
  2299. $matched_ref = $ref;
  2300. last;
  2301. }
  2302. }
  2303. # find log type for feed description (title)
  2304. my $type = 'log';
  2305. if (defined $file_name) {
  2306. $type = "history of $file_name";
  2307. $type .= "/" if ($action eq 'tree');
  2308. $type .= " on '$branch'" if (defined $branch);
  2309. } else {
  2310. $type = "log of $branch" if (defined $branch);
  2311. }
  2312. $res{-title} = $type;
  2313. $res{'hash'} = (defined $branch ? "refs/$matched_ref/$branch" : undef);
  2314. $res{'file_name'} = $file_name;
  2315. return %res;
  2316. }
  2317. ## ----------------------------------------------------------------------
  2318. ## git utility subroutines, invoking git commands
  2319. # returns path to the core git executable and the --git-dir parameter as list
  2320. sub git_cmd {
  2321. $number_of_git_cmds++;
  2322. return $GIT, '--git-dir='.$git_dir;
  2323. }
  2324. # quote the given arguments for passing them to the shell
  2325. # quote_command("command", "arg 1", "arg with ' and ! characters")
  2326. # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
  2327. # Try to avoid using this function wherever possible.
  2328. sub quote_command {
  2329. return join(' ',
  2330. map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
  2331. }
  2332. # get HEAD ref of given project as hash
  2333. sub git_get_head_hash {
  2334. return git_get_full_hash(shift, 'HEAD');
  2335. }
  2336. sub git_get_full_hash {
  2337. return git_get_hash(@_);
  2338. }
  2339. sub git_get_short_hash {
  2340. return git_get_hash(@_, '--short=7');
  2341. }
  2342. sub git_get_hash {
  2343. my ($project, $hash, @options) = @_;
  2344. my $o_git_dir = $git_dir;
  2345. my $retval = undef;
  2346. $git_dir = "$projectroot/$project";
  2347. if (open my $fd, '-|', git_cmd(), 'rev-parse',
  2348. '--verify', '-q', @options, $hash) {
  2349. $retval = <$fd>;
  2350. chomp $retval if defined $retval;
  2351. close $fd;
  2352. }
  2353. if (defined $o_git_dir) {
  2354. $git_dir = $o_git_dir;
  2355. }
  2356. return $retval;
  2357. }
  2358. # get type of given object
  2359. sub git_get_type {
  2360. my $hash = shift;
  2361. open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
  2362. my $type = <$fd>;
  2363. close $fd or return;
  2364. chomp $type;
  2365. return $type;
  2366. }
  2367. # repository configuration
  2368. our $config_file = '';
  2369. our %config;
  2370. # store multiple values for single key as anonymous array reference
  2371. # single values stored directly in the hash, not as [ <value> ]
  2372. sub hash_set_multi {
  2373. my ($hash, $key, $value) = @_;
  2374. if (!exists $hash->{$key}) {
  2375. $hash->{$key} = $value;
  2376. } elsif (!ref $hash->{$key}) {
  2377. $hash->{$key} = [ $hash->{$key}, $value ];
  2378. } else {
  2379. push @{$hash->{$key}}, $value;
  2380. }
  2381. }
  2382. # return hash of git project configuration
  2383. # optionally limited to some section, e.g. 'gitweb'
  2384. sub git_parse_project_config {
  2385. my $section_regexp = shift;
  2386. my %config;
  2387. local $/ = "\0";
  2388. open my $fh, "-|", git_cmd(), "config", '-z', '-l',
  2389. or return;
  2390. while (my $keyval = <$fh>) {
  2391. chomp $keyval;
  2392. my ($key, $value) = split(/\n/, $keyval, 2);
  2393. hash_set_multi(\%config, $key, $value)
  2394. if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
  2395. }
  2396. close $fh;
  2397. return %config;
  2398. }
  2399. # convert config value to boolean: 'true' or 'false'
  2400. # no value, number > 0, 'true' and 'yes' values are true
  2401. # rest of values are treated as false (never as error)
  2402. sub config_to_bool {
  2403. my $val = shift;
  2404. return 1 if !defined $val; # section.key
  2405. # strip leading and trailing whitespace
  2406. $val =~ s/^\s+//;
  2407. $val =~ s/\s+$//;
  2408. return (($val =~ /^\d+$/ && $val) || # section.key = 1
  2409. ($val =~ /^(?:true|yes)$/i)); # section.key = true
  2410. }
  2411. # convert config value to simple decimal number
  2412. # an optional value suffix of 'k', 'm', or 'g' will cause the value
  2413. # to be multiplied by 1024, 1048576, or 1073741824
  2414. sub config_to_int {
  2415. my $val = shift;
  2416. # strip leading and trailing whitespace
  2417. $val =~ s/^\s+//;
  2418. $val =~ s/\s+$//;
  2419. if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
  2420. $unit = lc($unit);
  2421. # unknown unit is treated as 1
  2422. return $num * ($unit eq 'g' ? 1073741824 :
  2423. $unit eq 'm' ? 1048576 :
  2424. $unit eq 'k' ? 1024 : 1);
  2425. }
  2426. return $val;
  2427. }
  2428. # convert config value to array reference, if needed
  2429. sub config_to_multi {
  2430. my $val = shift;
  2431. return ref($val) ? $val : (defined($val) ? [ $val ] : []);
  2432. }
  2433. sub git_get_project_config {
  2434. my ($key, $type) = @_;
  2435. return unless defined $git_dir;
  2436. # key sanity check
  2437. return unless ($key);
  2438. # only subsection, if exists, is case sensitive,
  2439. # and not lowercased by 'git config -z -l'
  2440. if (my ($hi, $mi, $lo) = ($key =~ /^([^.]*)\.(.*)\.([^.]*)$/)) {
  2441. $lo =~ s/_//g;
  2442. $key = join(".", lc($hi), $mi, lc($lo));
  2443. return if ($lo =~ /\W/ || $hi =~ /\W/);
  2444. } else {
  2445. $key = lc($key);
  2446. $key =~ s/_//g;
  2447. return if ($key =~ /\W/);
  2448. }
  2449. $key =~ s/^gitweb\.//;
  2450. # type sanity check
  2451. if (defined $type) {
  2452. $type =~ s/^--//;
  2453. $type = undef
  2454. unless ($type eq 'bool' || $type eq 'int');
  2455. }
  2456. # get config
  2457. if (!defined $config_file ||
  2458. $config_file ne "$git_dir/config") {
  2459. %config = git_parse_project_config('gitweb');
  2460. $config_file = "$git_dir/config";
  2461. }
  2462. # check if config variable (key) exists
  2463. return unless exists $config{"gitweb.$key"};
  2464. # ensure given type
  2465. if (!defined $type) {
  2466. return $config{"gitweb.$key"};
  2467. } elsif ($type eq 'bool') {
  2468. # backward compatibility: 'git config --bool' returns true/false
  2469. return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
  2470. } elsif ($type eq 'int') {
  2471. return config_to_int($config{"gitweb.$key"});
  2472. }
  2473. return $config{"gitweb.$key"};
  2474. }
  2475. # get hash of given path at given ref
  2476. sub git_get_hash_by_path {
  2477. my $base = shift;
  2478. my $path = shift || return undef;
  2479. my $type = shift;
  2480. $path =~ s,/+$,,;
  2481. open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
  2482. or die_error(500, "Open git-ls-tree failed");
  2483. my $line = <$fd>;
  2484. close $fd or return undef;
  2485. if (!defined $line) {
  2486. # there is no tree or hash given by $path at $base
  2487. return undef;
  2488. }
  2489. #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
  2490. $line =~ m/^([0-9]+) (.+) ($oid_regex)\t/;
  2491. if (defined $type && $type ne $2) {
  2492. # type doesn't match
  2493. return undef;
  2494. }
  2495. return $3;
  2496. }
  2497. # get path of entry with given hash at given tree-ish (ref)
  2498. # used to get 'from' filename for combined diff (merge commit) for renames
  2499. sub git_get_path_by_hash {
  2500. my $base = shift || return;
  2501. my $hash = shift || return;
  2502. local $/ = "\0";
  2503. open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
  2504. or return undef;
  2505. while (my $line = <$fd>) {
  2506. chomp $line;
  2507. #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
  2508. #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
  2509. if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
  2510. close $fd;
  2511. return $1;
  2512. }
  2513. }
  2514. close $fd;
  2515. return undef;
  2516. }
  2517. ## ......................................................................
  2518. ## git utility functions, directly accessing git repository
  2519. # get the value of config variable either from file named as the variable
  2520. # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
  2521. # configuration variable in the repository config file.
  2522. sub git_get_file_or_project_config {
  2523. my ($path, $name) = @_;
  2524. $git_dir = "$projectroot/$path";
  2525. open my $fd, '<', "$git_dir/$name"
  2526. or return git_get_project_config($name);
  2527. my $conf = <$fd>;
  2528. close $fd;
  2529. if (defined $conf) {
  2530. chomp $conf;
  2531. }
  2532. return $conf;
  2533. }
  2534. sub git_get_project_description {
  2535. my $path = shift;
  2536. return git_get_file_or_project_config($path, 'description');
  2537. }
  2538. sub git_get_project_category {
  2539. my $path = shift;
  2540. return git_get_file_or_project_config($path, 'category');
  2541. }
  2542. # supported formats:
  2543. # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
  2544. # - if its contents is a number, use it as tag weight,
  2545. # - otherwise add a tag with weight 1
  2546. # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
  2547. # the same value multiple times increases tag weight
  2548. # * `gitweb.ctag' multi-valued repo config variable
  2549. sub git_get_project_ctags {
  2550. my $project = shift;
  2551. my $ctags = {};
  2552. $git_dir = "$projectroot/$project";
  2553. if (opendir my $dh, "$git_dir/ctags") {
  2554. my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
  2555. foreach my $tagfile (@files) {
  2556. open my $ct, '<', $tagfile
  2557. or next;
  2558. my $val = <$ct>;
  2559. chomp $val if $val;
  2560. close $ct;
  2561. (my $ctag = $tagfile) =~ s#.*/##;
  2562. if ($val =~ /^\d+$/) {
  2563. $ctags->{$ctag} = $val;
  2564. } else {
  2565. $ctags->{$ctag} = 1;
  2566. }
  2567. }
  2568. closedir $dh;
  2569. } elsif (open my $fh, '<', "$git_dir/ctags") {
  2570. while (my $line = <$fh>) {
  2571. chomp $line;
  2572. $ctags->{$line}++ if $line;
  2573. }
  2574. close $fh;
  2575. } else {
  2576. my $taglist = config_to_multi(git_get_project_config('ctag'));
  2577. foreach my $tag (@$taglist) {
  2578. $ctags->{$tag}++;
  2579. }
  2580. }
  2581. return $ctags;
  2582. }
  2583. # return hash, where keys are content tags ('ctags'),
  2584. # and values are sum of weights of given tag in every project
  2585. sub git_gather_all_ctags {
  2586. my $projects = shift;
  2587. my $ctags = {};
  2588. foreach my $p (@$projects) {
  2589. foreach my $ct (keys %{$p->{'ctags'}}) {
  2590. $ctags->{$ct} += $p->{'ctags'}->{$ct};
  2591. }
  2592. }
  2593. return $ctags;
  2594. }
  2595. sub git_populate_project_tagcloud {
  2596. my $ctags = shift;
  2597. # First, merge different-cased tags; tags vote on casing
  2598. my %ctags_lc;
  2599. foreach (keys %$ctags) {
  2600. $ctags_lc{lc $_}->{count} += $ctags->{$_};
  2601. if (not $ctags_lc{lc $_}->{topcount}
  2602. or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
  2603. $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
  2604. $ctags_lc{lc $_}->{topname} = $_;
  2605. }
  2606. }
  2607. my $cloud;
  2608. my $matched = $input_params{'ctag'};
  2609. if (eval { require HTML::TagCloud; 1; }) {
  2610. $cloud = HTML::TagCloud->new;
  2611. foreach my $ctag (sort keys %ctags_lc) {
  2612. # Pad the title with spaces so that the cloud looks
  2613. # less crammed.
  2614. my $title = esc_html($ctags_lc{$ctag}->{topname});
  2615. $title =~ s/ /&nbsp;/g;
  2616. $title =~ s/^/&nbsp;/g;
  2617. $title =~ s/$/&nbsp;/g;
  2618. if (defined $matched && $matched eq $ctag) {
  2619. $title = qq(<span class="match">$title</span>);
  2620. }
  2621. $cloud->add($title, href(project=>undef, ctag=>$ctag),
  2622. $ctags_lc{$ctag}->{count});
  2623. }
  2624. } else {
  2625. $cloud = {};
  2626. foreach my $ctag (keys %ctags_lc) {
  2627. my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
  2628. if (defined $matched && $matched eq $ctag) {
  2629. $title = qq(<span class="match">$title</span>);
  2630. }
  2631. $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
  2632. $cloud->{$ctag}{ctag} =
  2633. $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
  2634. }
  2635. }
  2636. return $cloud;
  2637. }
  2638. sub git_show_project_tagcloud {
  2639. my ($cloud, $count) = @_;
  2640. if (ref $cloud eq 'HTML::TagCloud') {
  2641. return $cloud->html_and_css($count);
  2642. } else {
  2643. my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
  2644. return
  2645. '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
  2646. join (', ', map {
  2647. $cloud->{$_}->{'ctag'}
  2648. } splice(@tags, 0, $count)) .
  2649. '</div>';
  2650. }
  2651. }
  2652. sub git_get_project_url_list {
  2653. my $path = shift;
  2654. $git_dir = "$projectroot/$path";
  2655. open my $fd, '<', "$git_dir/cloneurl"
  2656. or return wantarray ?
  2657. @{ config_to_multi(git_get_project_config('url')) } :
  2658. config_to_multi(git_get_project_config('url'));
  2659. my @git_project_url_list = map { chomp; $_ } <$fd>;
  2660. close $fd;
  2661. return wantarray ? @git_project_url_list : \@git_project_url_list;
  2662. }
  2663. sub git_get_projects_list {
  2664. my $filter = shift || '';
  2665. my $paranoid = shift;
  2666. my @list;
  2667. if (-d $projects_list) {
  2668. # search in directory
  2669. my $dir = $projects_list;
  2670. # remove the trailing "/"
  2671. $dir =~ s!/+$!!;
  2672. my $pfxlen = length("$dir");
  2673. my $pfxdepth = ($dir =~ tr!/!!);
  2674. # when filtering, search only given subdirectory
  2675. if ($filter && !$paranoid) {
  2676. $dir .= "/$filter";
  2677. $dir =~ s!/+$!!;
  2678. }
  2679. File::Find::find({
  2680. follow_fast => 1, # follow symbolic links
  2681. follow_skip => 2, # ignore duplicates
  2682. dangling_symlinks => 0, # ignore dangling symlinks, silently
  2683. wanted => sub {
  2684. # global variables
  2685. our $project_maxdepth;
  2686. our $projectroot;
  2687. # skip project-list toplevel, if we get it.
  2688. return if (m!^[/.]$!);
  2689. # only directories can be git repositories
  2690. return unless (-d $_);
  2691. # need search permission
  2692. return unless (-x $_);
  2693. # don't traverse too deep (Find is super slow on os x)
  2694. # $project_maxdepth excludes depth of $projectroot
  2695. if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
  2696. $File::Find::prune = 1;
  2697. return;
  2698. }
  2699. my $path = substr($File::Find::name, $pfxlen + 1);
  2700. # paranoidly only filter here
  2701. if ($paranoid && $filter && $path !~ m!^\Q$filter\E/!) {
  2702. next;
  2703. }
  2704. # we check related file in $projectroot
  2705. if (check_export_ok("$projectroot/$path")) {
  2706. push @list, { path => $path };
  2707. $File::Find::prune = 1;
  2708. }
  2709. },
  2710. }, "$dir");
  2711. } elsif (-f $projects_list) {
  2712. # read from file(url-encoded):
  2713. # 'git%2Fgit.git Linus+Torvalds'
  2714. # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
  2715. # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
  2716. open my $fd, '<', $projects_list or return;
  2717. PROJECT:
  2718. while (my $line = <$fd>) {
  2719. chomp $line;
  2720. my ($path, $owner) = split ' ', $line;
  2721. $path = unescape($path);
  2722. $owner = unescape($owner);
  2723. if (!defined $path) {
  2724. next;
  2725. }
  2726. # if $filter is rpovided, check if $path begins with $filter
  2727. if ($filter && $path !~ m!^\Q$filter\E/!) {
  2728. next;
  2729. }
  2730. if (check_export_ok("$projectroot/$path")) {
  2731. my $pr = {
  2732. path => $path
  2733. };
  2734. if ($owner) {
  2735. $pr->{'owner'} = to_utf8($owner);
  2736. }
  2737. push @list, $pr;
  2738. }
  2739. }
  2740. close $fd;
  2741. }
  2742. return @list;
  2743. }
  2744. # written with help of Tree::Trie module (Perl Artistic License, GPL compatible)
  2745. # as side effects it sets 'forks' field to list of forks for forked projects
  2746. sub filter_forks_from_projects_list {
  2747. my $projects = shift;
  2748. my %trie; # prefix tree of directories (path components)
  2749. # generate trie out of those directories that might contain forks
  2750. foreach my $pr (@$projects) {
  2751. my $path = $pr->{'path'};
  2752. $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
  2753. next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
  2754. next unless ($path); # skip '.git' repository: tests, git-instaweb
  2755. next unless (-d "$projectroot/$path"); # containing directory exists
  2756. $pr->{'forks'} = []; # there can be 0 or more forks of project
  2757. # add to trie
  2758. my @dirs = split('/', $path);
  2759. # walk the trie, until either runs out of components or out of trie
  2760. my $ref = \%trie;
  2761. while (scalar @dirs &&
  2762. exists($ref->{$dirs[0]})) {
  2763. $ref = $ref->{shift @dirs};
  2764. }
  2765. # create rest of trie structure from rest of components
  2766. foreach my $dir (@dirs) {
  2767. $ref = $ref->{$dir} = {};
  2768. }
  2769. # create end marker, store $pr as a data
  2770. $ref->{''} = $pr if (!exists $ref->{''});
  2771. }
  2772. # filter out forks, by finding shortest prefix match for paths
  2773. my @filtered;
  2774. PROJECT:
  2775. foreach my $pr (@$projects) {
  2776. # trie lookup
  2777. my $ref = \%trie;
  2778. DIR:
  2779. foreach my $dir (split('/', $pr->{'path'})) {
  2780. if (exists $ref->{''}) {
  2781. # found [shortest] prefix, is a fork - skip it
  2782. push @{$ref->{''}{'forks'}}, $pr;
  2783. next PROJECT;
  2784. }
  2785. if (!exists $ref->{$dir}) {
  2786. # not in trie, cannot have prefix, not a fork
  2787. push @filtered, $pr;
  2788. next PROJECT;
  2789. }
  2790. # If the dir is there, we just walk one step down the trie.
  2791. $ref = $ref->{$dir};
  2792. }
  2793. # we ran out of trie
  2794. # (shouldn't happen: it's either no match, or end marker)
  2795. push @filtered, $pr;
  2796. }
  2797. return @filtered;
  2798. }
  2799. # note: fill_project_list_info must be run first,
  2800. # for 'descr_long' and 'ctags' to be filled
  2801. sub search_projects_list {
  2802. my ($projlist, %opts) = @_;
  2803. my $tagfilter = $opts{'tagfilter'};
  2804. my $search_re = $opts{'search_regexp'};
  2805. return @$projlist
  2806. unless ($tagfilter || $search_re);
  2807. # searching projects require filling to be run before it;
  2808. fill_project_list_info($projlist,
  2809. $tagfilter ? 'ctags' : (),
  2810. $search_re ? ('path', 'descr') : ());
  2811. my @projects;
  2812. PROJECT:
  2813. foreach my $pr (@$projlist) {
  2814. if ($tagfilter) {
  2815. next unless ref($pr->{'ctags'}) eq 'HASH';
  2816. next unless
  2817. grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
  2818. }
  2819. if ($search_re) {
  2820. next unless
  2821. $pr->{'path'} =~ /$search_re/ ||
  2822. $pr->{'descr_long'} =~ /$search_re/;
  2823. }
  2824. push @projects, $pr;
  2825. }
  2826. return @projects;
  2827. }
  2828. our $gitweb_project_owner = undef;
  2829. sub git_get_project_list_from_file {
  2830. return if (defined $gitweb_project_owner);
  2831. $gitweb_project_owner = {};
  2832. # read from file (url-encoded):
  2833. # 'git%2Fgit.git Linus+Torvalds'
  2834. # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
  2835. # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
  2836. if (-f $projects_list) {
  2837. open(my $fd, '<', $projects_list);
  2838. while (my $line = <$fd>) {
  2839. chomp $line;
  2840. my ($pr, $ow) = split ' ', $line;
  2841. $pr = unescape($pr);
  2842. $ow = unescape($ow);
  2843. $gitweb_project_owner->{$pr} = to_utf8($ow);
  2844. }
  2845. close $fd;
  2846. }
  2847. }
  2848. sub git_get_project_owner {
  2849. my $project = shift;
  2850. my $owner;
  2851. return undef unless $project;
  2852. $git_dir = "$projectroot/$project";
  2853. if (!defined $gitweb_project_owner) {
  2854. git_get_project_list_from_file();
  2855. }
  2856. if (exists $gitweb_project_owner->{$project}) {
  2857. $owner = $gitweb_project_owner->{$project};
  2858. }
  2859. if (!defined $owner){
  2860. $owner = git_get_project_config('owner');
  2861. }
  2862. if (!defined $owner) {
  2863. $owner = get_file_owner("$git_dir");
  2864. }
  2865. return $owner;
  2866. }
  2867. sub git_get_last_activity {
  2868. my ($path) = @_;
  2869. my $fd;
  2870. $git_dir = "$projectroot/$path";
  2871. open($fd, "-|", git_cmd(), 'for-each-ref',
  2872. '--format=%(committer)',
  2873. '--sort=-committerdate',
  2874. '--count=1',
  2875. map { "refs/$_" } get_branch_refs ()) or return;
  2876. my $most_recent = <$fd>;
  2877. close $fd or return;
  2878. if (defined $most_recent &&
  2879. $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
  2880. my $timestamp = $1;
  2881. my $age = time - $timestamp;
  2882. return ($age, age_string($age));
  2883. }
  2884. return (undef, undef);
  2885. }
  2886. # Implementation note: when a single remote is wanted, we cannot use 'git
  2887. # remote show -n' because that command always work (assuming it's a remote URL
  2888. # if it's not defined), and we cannot use 'git remote show' because that would
  2889. # try to make a network roundtrip. So the only way to find if that particular
  2890. # remote is defined is to walk the list provided by 'git remote -v' and stop if
  2891. # and when we find what we want.
  2892. sub git_get_remotes_list {
  2893. my $wanted = shift;
  2894. my %remotes = ();
  2895. open my $fd, '-|' , git_cmd(), 'remote', '-v';
  2896. return unless $fd;
  2897. while (my $remote = <$fd>) {
  2898. chomp $remote;
  2899. $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
  2900. next if $wanted and not $remote eq $wanted;
  2901. my ($url, $key) = ($1, $2);
  2902. $remotes{$remote} ||= { 'heads' => () };
  2903. $remotes{$remote}{$key} = $url;
  2904. }
  2905. close $fd or return;
  2906. return wantarray ? %remotes : \%remotes;
  2907. }
  2908. # Takes a hash of remotes as first parameter and fills it by adding the
  2909. # available remote heads for each of the indicated remotes.
  2910. sub fill_remote_heads {
  2911. my $remotes = shift;
  2912. my @heads = map { "remotes/$_" } keys %$remotes;
  2913. my @remoteheads = git_get_heads_list(undef, @heads);
  2914. foreach my $remote (keys %$remotes) {
  2915. $remotes->{$remote}{'heads'} = [ grep {
  2916. $_->{'name'} =~ s!^$remote/!!
  2917. } @remoteheads ];
  2918. }
  2919. }
  2920. sub git_get_references {
  2921. my $type = shift || "";
  2922. my %refs;
  2923. # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
  2924. # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
  2925. open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
  2926. ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
  2927. or return;
  2928. while (my $line = <$fd>) {
  2929. chomp $line;
  2930. if ($line =~ m!^($oid_regex)\srefs/($type.*)$!) {
  2931. if (defined $refs{$1}) {
  2932. push @{$refs{$1}}, $2;
  2933. } else {
  2934. $refs{$1} = [ $2 ];
  2935. }
  2936. }
  2937. }
  2938. close $fd or return;
  2939. return \%refs;
  2940. }
  2941. sub git_get_rev_name_tags {
  2942. my $hash = shift || return undef;
  2943. open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
  2944. or return;
  2945. my $name_rev = <$fd>;
  2946. close $fd;
  2947. if ($name_rev =~ m|^$hash tags/(.*)$|) {
  2948. return $1;
  2949. } else {
  2950. # catches also '$hash undefined' output
  2951. return undef;
  2952. }
  2953. }
  2954. ## ----------------------------------------------------------------------
  2955. ## parse to hash functions
  2956. sub parse_date {
  2957. my $epoch = shift;
  2958. my $tz = shift || "-0000";
  2959. my %date;
  2960. my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
  2961. my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
  2962. my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
  2963. $date{'hour'} = $hour;
  2964. $date{'minute'} = $min;
  2965. $date{'mday'} = $mday;
  2966. $date{'day'} = $days[$wday];
  2967. $date{'month'} = $months[$mon];
  2968. $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
  2969. $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
  2970. $date{'mday-time'} = sprintf "%d %s %02d:%02d",
  2971. $mday, $months[$mon], $hour ,$min;
  2972. $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
  2973. 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
  2974. my ($tz_sign, $tz_hour, $tz_min) =
  2975. ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
  2976. $tz_sign = ($tz_sign eq '-' ? -1 : +1);
  2977. my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
  2978. ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
  2979. $date{'hour_local'} = $hour;
  2980. $date{'minute_local'} = $min;
  2981. $date{'tz_local'} = $tz;
  2982. $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
  2983. 1900+$year, $mon+1, $mday,
  2984. $hour, $min, $sec, $tz);
  2985. return %date;
  2986. }
  2987. sub parse_tag {
  2988. my $tag_id = shift;
  2989. my %tag;
  2990. my @comment;
  2991. open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
  2992. $tag{'id'} = $tag_id;
  2993. while (my $line = <$fd>) {
  2994. chomp $line;
  2995. if ($line =~ m/^object ($oid_regex)$/) {
  2996. $tag{'object'} = $1;
  2997. } elsif ($line =~ m/^type (.+)$/) {
  2998. $tag{'type'} = $1;
  2999. } elsif ($line =~ m/^tag (.+)$/) {
  3000. $tag{'name'} = $1;
  3001. } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
  3002. $tag{'author'} = $1;
  3003. $tag{'author_epoch'} = $2;
  3004. $tag{'author_tz'} = $3;
  3005. if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
  3006. $tag{'author_name'} = $1;
  3007. $tag{'author_email'} = $2;
  3008. } else {
  3009. $tag{'author_name'} = $tag{'author'};
  3010. }
  3011. } elsif ($line =~ m/--BEGIN/) {
  3012. push @comment, $line;
  3013. last;
  3014. } elsif ($line eq "") {
  3015. last;
  3016. }
  3017. }
  3018. push @comment, <$fd>;
  3019. $tag{'comment'} = \@comment;
  3020. close $fd or return;
  3021. if (!defined $tag{'name'}) {
  3022. return
  3023. };
  3024. return %tag
  3025. }
  3026. sub parse_commit_text {
  3027. my ($commit_text, $withparents) = @_;
  3028. my @commit_lines = split '\n', $commit_text;
  3029. my %co;
  3030. pop @commit_lines; # Remove '\0'
  3031. if (! @commit_lines) {
  3032. return;
  3033. }
  3034. my $header = shift @commit_lines;
  3035. if ($header !~ m/^$oid_regex/) {
  3036. return;
  3037. }
  3038. ($co{'id'}, my @parents) = split ' ', $header;
  3039. while (my $line = shift @commit_lines) {
  3040. last if $line eq "\n";
  3041. if ($line =~ m/^tree ($oid_regex)$/) {
  3042. $co{'tree'} = $1;
  3043. } elsif ((!defined $withparents) && ($line =~ m/^parent ($oid_regex)$/)) {
  3044. push @parents, $1;
  3045. } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
  3046. $co{'author'} = to_utf8($1);
  3047. $co{'author_epoch'} = $2;
  3048. $co{'author_tz'} = $3;
  3049. if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
  3050. $co{'author_name'} = $1;
  3051. $co{'author_email'} = $2;
  3052. } else {
  3053. $co{'author_name'} = $co{'author'};
  3054. }
  3055. } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
  3056. $co{'committer'} = to_utf8($1);
  3057. $co{'committer_epoch'} = $2;
  3058. $co{'committer_tz'} = $3;
  3059. if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
  3060. $co{'committer_name'} = $1;
  3061. $co{'committer_email'} = $2;
  3062. } else {
  3063. $co{'committer_name'} = $co{'committer'};
  3064. }
  3065. }
  3066. }
  3067. if (!defined $co{'tree'}) {
  3068. return;
  3069. };
  3070. $co{'parents'} = \@parents;
  3071. $co{'parent'} = $parents[0];
  3072. foreach my $title (@commit_lines) {
  3073. $title =~ s/^ //;
  3074. if ($title ne "") {
  3075. $co{'title'} = chop_str($title, 80, 5);
  3076. # remove leading stuff of merges to make the interesting part visible
  3077. if (length($title) > 50) {
  3078. $title =~ s/^Automatic //;
  3079. $title =~ s/^merge (of|with) /Merge ... /i;
  3080. if (length($title) > 50) {
  3081. $title =~ s/(http|rsync):\/\///;
  3082. }
  3083. if (length($title) > 50) {
  3084. $title =~ s/(master|www|rsync)\.//;
  3085. }
  3086. if (length($title) > 50) {
  3087. $title =~ s/kernel.org:?//;
  3088. }
  3089. if (length($title) > 50) {
  3090. $title =~ s/\/pub\/scm//;
  3091. }
  3092. }
  3093. $co{'title_short'} = chop_str($title, 50, 5);
  3094. last;
  3095. }
  3096. }
  3097. if (! defined $co{'title'} || $co{'title'} eq "") {
  3098. $co{'title'} = $co{'title_short'} = '(no commit message)';
  3099. }
  3100. # remove added spaces
  3101. foreach my $line (@commit_lines) {
  3102. $line =~ s/^ //;
  3103. }
  3104. $co{'comment'} = \@commit_lines;
  3105. my $age = time - $co{'committer_epoch'};
  3106. $co{'age'} = $age;
  3107. $co{'age_string'} = age_string($age);
  3108. my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
  3109. if ($age > 60*60*24*7*2) {
  3110. $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
  3111. $co{'age_string_age'} = $co{'age_string'};
  3112. } else {
  3113. $co{'age_string_date'} = $co{'age_string'};
  3114. $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
  3115. }
  3116. return %co;
  3117. }
  3118. sub parse_commit {
  3119. my ($commit_id) = @_;
  3120. my %co;
  3121. local $/ = "\0";
  3122. open my $fd, "-|", git_cmd(), "rev-list",
  3123. "--parents",
  3124. "--header",
  3125. "--max-count=1",
  3126. $commit_id,
  3127. "--",
  3128. or die_error(500, "Open git-rev-list failed");
  3129. %co = parse_commit_text(<$fd>, 1);
  3130. close $fd;
  3131. return %co;
  3132. }
  3133. sub parse_commits {
  3134. my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
  3135. my @cos;
  3136. $maxcount ||= 1;
  3137. $skip ||= 0;
  3138. local $/ = "\0";
  3139. open my $fd, "-|", git_cmd(), "rev-list",
  3140. "--header",
  3141. @args,
  3142. ("--max-count=" . $maxcount),
  3143. ("--skip=" . $skip),
  3144. @extra_options,
  3145. $commit_id,
  3146. "--",
  3147. ($filename ? ($filename) : ())
  3148. or die_error(500, "Open git-rev-list failed");
  3149. while (my $line = <$fd>) {
  3150. my %co = parse_commit_text($line);
  3151. push @cos, \%co;
  3152. }
  3153. close $fd;
  3154. return wantarray ? @cos : \@cos;
  3155. }
  3156. # parse line of git-diff-tree "raw" output
  3157. sub parse_difftree_raw_line {
  3158. my $line = shift;
  3159. my %res;
  3160. # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
  3161. # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
  3162. if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ($oid_regex) ($oid_regex) (.)([0-9]{0,3})\t(.*)$/) {
  3163. $res{'from_mode'} = $1;
  3164. $res{'to_mode'} = $2;
  3165. $res{'from_id'} = $3;
  3166. $res{'to_id'} = $4;
  3167. $res{'status'} = $5;
  3168. $res{'similarity'} = $6;
  3169. if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
  3170. ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
  3171. } else {
  3172. $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
  3173. }
  3174. }
  3175. # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
  3176. # combined diff (for merge commit)
  3177. elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:$oid_regex )+)([a-zA-Z]+)\t(.*)$//) {
  3178. $res{'nparents'} = length($1);
  3179. $res{'from_mode'} = [ split(' ', $2) ];
  3180. $res{'to_mode'} = pop @{$res{'from_mode'}};
  3181. $res{'from_id'} = [ split(' ', $3) ];
  3182. $res{'to_id'} = pop @{$res{'from_id'}};
  3183. $res{'status'} = [ split('', $4) ];
  3184. $res{'to_file'} = unquote($5);
  3185. }
  3186. # 'c512b523472485aef4fff9e57b229d9d243c967f'
  3187. elsif ($line =~ m/^($oid_regex)$/) {
  3188. $res{'commit'} = $1;
  3189. }
  3190. return wantarray ? %res : \%res;
  3191. }
  3192. # wrapper: return parsed line of git-diff-tree "raw" output
  3193. # (the argument might be raw line, or parsed info)
  3194. sub parsed_difftree_line {
  3195. my $line_or_ref = shift;
  3196. if (ref($line_or_ref) eq "HASH") {
  3197. # pre-parsed (or generated by hand)
  3198. return $line_or_ref;
  3199. } else {
  3200. return parse_difftree_raw_line($line_or_ref);
  3201. }
  3202. }
  3203. # parse line of git-ls-tree output
  3204. sub parse_ls_tree_line {
  3205. my $line = shift;
  3206. my %opts = @_;
  3207. my %res;
  3208. if ($opts{'-l'}) {
  3209. #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
  3210. $line =~ m/^([0-9]+) (.+) ($oid_regex) +(-|[0-9]+)\t(.+)$/s;
  3211. $res{'mode'} = $1;
  3212. $res{'type'} = $2;
  3213. $res{'hash'} = $3;
  3214. $res{'size'} = $4;
  3215. if ($opts{'-z'}) {
  3216. $res{'name'} = $5;
  3217. } else {
  3218. $res{'name'} = unquote($5);
  3219. }
  3220. } else {
  3221. #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
  3222. $line =~ m/^([0-9]+) (.+) ($oid_regex)\t(.+)$/s;
  3223. $res{'mode'} = $1;
  3224. $res{'type'} = $2;
  3225. $res{'hash'} = $3;
  3226. if ($opts{'-z'}) {
  3227. $res{'name'} = $4;
  3228. } else {
  3229. $res{'name'} = unquote($4);
  3230. }
  3231. }
  3232. return wantarray ? %res : \%res;
  3233. }
  3234. # generates _two_ hashes, references to which are passed as 2 and 3 argument
  3235. sub parse_from_to_diffinfo {
  3236. my ($diffinfo, $from, $to, @parents) = @_;
  3237. if ($diffinfo->{'nparents'}) {
  3238. # combined diff
  3239. $from->{'file'} = [];
  3240. $from->{'href'} = [];
  3241. fill_from_file_info($diffinfo, @parents)
  3242. unless exists $diffinfo->{'from_file'};
  3243. for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
  3244. $from->{'file'}[$i] =
  3245. defined $diffinfo->{'from_file'}[$i] ?
  3246. $diffinfo->{'from_file'}[$i] :
  3247. $diffinfo->{'to_file'};
  3248. if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
  3249. $from->{'href'}[$i] = href(action=>"blob",
  3250. hash_base=>$parents[$i],
  3251. hash=>$diffinfo->{'from_id'}[$i],
  3252. file_name=>$from->{'file'}[$i]);
  3253. } else {
  3254. $from->{'href'}[$i] = undef;
  3255. }
  3256. }
  3257. } else {
  3258. # ordinary (not combined) diff
  3259. $from->{'file'} = $diffinfo->{'from_file'};
  3260. if ($diffinfo->{'status'} ne "A") { # not new (added) file
  3261. $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
  3262. hash=>$diffinfo->{'from_id'},
  3263. file_name=>$from->{'file'});
  3264. } else {
  3265. delete $from->{'href'};
  3266. }
  3267. }
  3268. $to->{'file'} = $diffinfo->{'to_file'};
  3269. if (!is_deleted($diffinfo)) { # file exists in result
  3270. $to->{'href'} = href(action=>"blob", hash_base=>$hash,
  3271. hash=>$diffinfo->{'to_id'},
  3272. file_name=>$to->{'file'});
  3273. } else {
  3274. delete $to->{'href'};
  3275. }
  3276. }
  3277. ## ......................................................................
  3278. ## parse to array of hashes functions
  3279. sub git_get_heads_list {
  3280. my ($limit, @classes) = @_;
  3281. @classes = get_branch_refs() unless @classes;
  3282. my @patterns = map { "refs/$_" } @classes;
  3283. my @headslist;
  3284. open my $fd, '-|', git_cmd(), 'for-each-ref',
  3285. ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
  3286. '--format=%(objectname) %(refname) %(subject)%00%(committer)',
  3287. @patterns
  3288. or return;
  3289. while (my $line = <$fd>) {
  3290. my %ref_item;
  3291. chomp $line;
  3292. my ($refinfo, $committerinfo) = split(/\0/, $line);
  3293. my ($hash, $name, $title) = split(' ', $refinfo, 3);
  3294. my ($committer, $epoch, $tz) =
  3295. ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
  3296. $ref_item{'fullname'} = $name;
  3297. my $strip_refs = join '|', map { quotemeta } get_branch_refs();
  3298. $name =~ s!^refs/($strip_refs|remotes)/!!;
  3299. $ref_item{'name'} = $name;
  3300. # for refs neither in 'heads' nor 'remotes' we want to
  3301. # show their ref dir
  3302. my $ref_dir = (defined $1) ? $1 : '';
  3303. if ($ref_dir ne '' and $ref_dir ne 'heads' and $ref_dir ne 'remotes') {
  3304. $ref_item{'name'} .= ' (' . $ref_dir . ')';
  3305. }
  3306. $ref_item{'id'} = $hash;
  3307. $ref_item{'title'} = $title || '(no commit message)';
  3308. $ref_item{'epoch'} = $epoch;
  3309. if ($epoch) {
  3310. $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
  3311. } else {
  3312. $ref_item{'age'} = "unknown";
  3313. }
  3314. push @headslist, \%ref_item;
  3315. }
  3316. close $fd;
  3317. return wantarray ? @headslist : \@headslist;
  3318. }
  3319. sub git_get_tags_list {
  3320. my $limit = shift;
  3321. my @tagslist;
  3322. open my $fd, '-|', git_cmd(), 'for-each-ref',
  3323. ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
  3324. '--format=%(objectname) %(objecttype) %(refname) '.
  3325. '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
  3326. 'refs/tags'
  3327. or return;
  3328. while (my $line = <$fd>) {
  3329. my %ref_item;
  3330. chomp $line;
  3331. my ($refinfo, $creatorinfo) = split(/\0/, $line);
  3332. my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
  3333. my ($creator, $epoch, $tz) =
  3334. ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
  3335. $ref_item{'fullname'} = $name;
  3336. $name =~ s!^refs/tags/!!;
  3337. $ref_item{'type'} = $type;
  3338. $ref_item{'id'} = $id;
  3339. $ref_item{'name'} = $name;
  3340. if ($type eq "tag") {
  3341. $ref_item{'subject'} = $title;
  3342. $ref_item{'reftype'} = $reftype;
  3343. $ref_item{'refid'} = $refid;
  3344. } else {
  3345. $ref_item{'reftype'} = $type;
  3346. $ref_item{'refid'} = $id;
  3347. }
  3348. if ($type eq "tag" || $type eq "commit") {
  3349. $ref_item{'epoch'} = $epoch;
  3350. if ($epoch) {
  3351. $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
  3352. } else {
  3353. $ref_item{'age'} = "unknown";
  3354. }
  3355. }
  3356. push @tagslist, \%ref_item;
  3357. }
  3358. close $fd;
  3359. return wantarray ? @tagslist : \@tagslist;
  3360. }
  3361. ## ----------------------------------------------------------------------
  3362. ## filesystem-related functions
  3363. sub get_file_owner {
  3364. my $path = shift;
  3365. my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
  3366. my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
  3367. if (!defined $gcos) {
  3368. return undef;
  3369. }
  3370. my $owner = $gcos;
  3371. $owner =~ s/[,;].*$//;
  3372. return to_utf8($owner);
  3373. }
  3374. # assume that file exists
  3375. sub insert_file {
  3376. my $filename = shift;
  3377. open my $fd, '<', $filename;
  3378. print map { to_utf8($_) } <$fd>;
  3379. close $fd;
  3380. }
  3381. ## ......................................................................
  3382. ## mimetype related functions
  3383. sub mimetype_guess_file {
  3384. my $filename = shift;
  3385. my $mimemap = shift;
  3386. -r $mimemap or return undef;
  3387. my %mimemap;
  3388. open(my $mh, '<', $mimemap) or return undef;
  3389. while (<$mh>) {
  3390. next if m/^#/; # skip comments
  3391. my ($mimetype, @exts) = split(/\s+/);
  3392. foreach my $ext (@exts) {
  3393. $mimemap{$ext} = $mimetype;
  3394. }
  3395. }
  3396. close($mh);
  3397. $filename =~ /\.([^.]*)$/;
  3398. return $mimemap{$1};
  3399. }
  3400. sub mimetype_guess {
  3401. my $filename = shift;
  3402. my $mime;
  3403. $filename =~ /\./ or return undef;
  3404. if ($mimetypes_file) {
  3405. my $file = $mimetypes_file;
  3406. if ($file !~ m!^/!) { # if it is relative path
  3407. # it is relative to project
  3408. $file = "$projectroot/$project/$file";
  3409. }
  3410. $mime = mimetype_guess_file($filename, $file);
  3411. }
  3412. $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
  3413. return $mime;
  3414. }
  3415. sub blob_mimetype {
  3416. my $fd = shift;
  3417. my $filename = shift;
  3418. if ($filename) {
  3419. my $mime = mimetype_guess($filename);
  3420. $mime and return $mime;
  3421. }
  3422. # just in case
  3423. return $default_blob_plain_mimetype unless $fd;
  3424. if (-T $fd) {
  3425. return 'text/plain';
  3426. } elsif (! $filename) {
  3427. return 'application/octet-stream';
  3428. } elsif ($filename =~ m/\.png$/i) {
  3429. return 'image/png';
  3430. } elsif ($filename =~ m/\.gif$/i) {
  3431. return 'image/gif';
  3432. } elsif ($filename =~ m/\.jpe?g$/i) {
  3433. return 'image/jpeg';
  3434. } else {
  3435. return 'application/octet-stream';
  3436. }
  3437. }
  3438. sub blob_contenttype {
  3439. my ($fd, $file_name, $type) = @_;
  3440. $type ||= blob_mimetype($fd, $file_name);
  3441. if ($type eq 'text/plain' && defined $default_text_plain_charset) {
  3442. $type .= "; charset=$default_text_plain_charset";
  3443. }
  3444. return $type;
  3445. }
  3446. # guess file syntax for syntax highlighting; return undef if no highlighting
  3447. # the name of syntax can (in the future) depend on syntax highlighter used
  3448. sub guess_file_syntax {
  3449. my ($highlight, $file_name) = @_;
  3450. return undef unless ($highlight && defined $file_name);
  3451. my $basename = basename($file_name, '.in');
  3452. return $highlight_basename{$basename}
  3453. if exists $highlight_basename{$basename};
  3454. $basename =~ /\.([^.]*)$/;
  3455. my $ext = $1 or return undef;
  3456. return $highlight_ext{$ext}
  3457. if exists $highlight_ext{$ext};
  3458. return undef;
  3459. }
  3460. # run highlighter and return FD of its output,
  3461. # or return original FD if no highlighting
  3462. sub run_highlighter {
  3463. my ($fd, $highlight, $syntax) = @_;
  3464. return $fd unless ($highlight);
  3465. close $fd;
  3466. my $syntax_arg = (defined $syntax) ? "--syntax $syntax" : "--force";
  3467. open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
  3468. quote_command($^X, '-CO', '-MEncode=decode,FB_DEFAULT', '-pse',
  3469. '$_ = decode($fe, $_, FB_DEFAULT) if !utf8::decode($_);',
  3470. '--', "-fe=$fallback_encoding")." | ".
  3471. quote_command($highlight_bin).
  3472. " --replace-tabs=8 --fragment $syntax_arg |"
  3473. or die_error(500, "Couldn't open file or run syntax highlighter");
  3474. return $fd;
  3475. }
  3476. ## ======================================================================
  3477. ## functions printing HTML: header, footer, error page
  3478. sub get_page_title {
  3479. my $title = to_utf8($site_name);
  3480. unless (defined $project) {
  3481. if (defined $project_filter) {
  3482. $title .= " - projects in '" . esc_path($project_filter) . "'";
  3483. }
  3484. return $title;
  3485. }
  3486. $title .= " - " . to_utf8($project);
  3487. return $title unless (defined $action);
  3488. $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
  3489. return $title unless (defined $file_name);
  3490. $title .= " - " . esc_path($file_name);
  3491. if ($action eq "tree" && $file_name !~ m|/$|) {
  3492. $title .= "/";
  3493. }
  3494. return $title;
  3495. }
  3496. sub get_content_type_html {
  3497. # require explicit support from the UA if we are to send the page as
  3498. # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
  3499. # we have to do this because MSIE sometimes globs '*/*', pretending to
  3500. # support xhtml+xml but choking when it gets what it asked for.
  3501. if (defined $cgi->http('HTTP_ACCEPT') &&
  3502. $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
  3503. $cgi->Accept('application/xhtml+xml') != 0) {
  3504. return 'application/xhtml+xml';
  3505. } else {
  3506. return 'text/html';
  3507. }
  3508. }
  3509. sub print_feed_meta {
  3510. if (defined $project) {
  3511. my %href_params = get_feed_info();
  3512. if (!exists $href_params{'-title'}) {
  3513. $href_params{'-title'} = 'log';
  3514. }
  3515. foreach my $format (qw(RSS Atom)) {
  3516. my $type = lc($format);
  3517. my %link_attr = (
  3518. '-rel' => 'alternate',
  3519. '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
  3520. '-type' => "application/$type+xml"
  3521. );
  3522. $href_params{'extra_options'} = undef;
  3523. $href_params{'action'} = $type;
  3524. $link_attr{'-href'} = esc_attr(href(%href_params));
  3525. print "<link ".
  3526. "rel=\"$link_attr{'-rel'}\" ".
  3527. "title=\"$link_attr{'-title'}\" ".
  3528. "href=\"$link_attr{'-href'}\" ".
  3529. "type=\"$link_attr{'-type'}\" ".
  3530. "/>\n";
  3531. $href_params{'extra_options'} = '--no-merges';
  3532. $link_attr{'-href'} = esc_attr(href(%href_params));
  3533. $link_attr{'-title'} .= ' (no merges)';
  3534. print "<link ".
  3535. "rel=\"$link_attr{'-rel'}\" ".
  3536. "title=\"$link_attr{'-title'}\" ".
  3537. "href=\"$link_attr{'-href'}\" ".
  3538. "type=\"$link_attr{'-type'}\" ".
  3539. "/>\n";
  3540. }
  3541. } else {
  3542. printf('<link rel="alternate" title="%s projects list" '.
  3543. 'href="%s" type="text/plain; charset=utf-8" />'."\n",
  3544. esc_attr($site_name),
  3545. esc_attr(href(project=>undef, action=>"project_index")));
  3546. printf('<link rel="alternate" title="%s projects feeds" '.
  3547. 'href="%s" type="text/x-opml" />'."\n",
  3548. esc_attr($site_name),
  3549. esc_attr(href(project=>undef, action=>"opml")));
  3550. }
  3551. }
  3552. sub print_header_links {
  3553. my $status = shift;
  3554. # print out each stylesheet that exist, providing backwards capability
  3555. # for those people who defined $stylesheet in a config file
  3556. if (defined $stylesheet) {
  3557. print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
  3558. } else {
  3559. foreach my $stylesheet (@stylesheets) {
  3560. next unless $stylesheet;
  3561. print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
  3562. }
  3563. }
  3564. print_feed_meta()
  3565. if ($status eq '200 OK');
  3566. if (defined $favicon) {
  3567. print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
  3568. }
  3569. }
  3570. sub print_nav_breadcrumbs_path {
  3571. my $dirprefix = undef;
  3572. while (my $part = shift) {
  3573. $dirprefix .= "/" if defined $dirprefix;
  3574. $dirprefix .= $part;
  3575. print $cgi->a({-href => href(project => undef,
  3576. project_filter => $dirprefix,
  3577. action => "project_list")},
  3578. esc_html($part)) . " / ";
  3579. }
  3580. }
  3581. sub print_nav_breadcrumbs {
  3582. my %opts = @_;
  3583. for my $crumb (@extra_breadcrumbs, [ $home_link_str => $home_link ]) {
  3584. print $cgi->a({-href => esc_url($crumb->[1])}, $crumb->[0]) . " / ";
  3585. }
  3586. if (defined $project) {
  3587. my @dirname = split '/', $project;
  3588. my $projectbasename = pop @dirname;
  3589. print_nav_breadcrumbs_path(@dirname);
  3590. print $cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));
  3591. if (defined $action) {
  3592. my $action_print = $action ;
  3593. if (defined $opts{-action_extra}) {
  3594. $action_print = $cgi->a({-href => href(action=>$action)},
  3595. $action);
  3596. }
  3597. print " / $action_print";
  3598. }
  3599. if (defined $opts{-action_extra}) {
  3600. print " / $opts{-action_extra}";
  3601. }
  3602. print "\n";
  3603. } elsif (defined $project_filter) {
  3604. print_nav_breadcrumbs_path(split '/', $project_filter);
  3605. }
  3606. }
  3607. sub print_search_form {
  3608. if (!defined $searchtext) {
  3609. $searchtext = "";
  3610. }
  3611. my $search_hash;
  3612. if (defined $hash_base) {
  3613. $search_hash = $hash_base;
  3614. } elsif (defined $hash) {
  3615. $search_hash = $hash;
  3616. } else {
  3617. $search_hash = "HEAD";
  3618. }
  3619. my $action = $my_uri;
  3620. my $use_pathinfo = gitweb_check_feature('pathinfo');
  3621. if ($use_pathinfo) {
  3622. $action .= "/".esc_url($project);
  3623. }
  3624. print $cgi->start_form(-method => "get", -action => $action) .
  3625. "<div class=\"search\">\n" .
  3626. (!$use_pathinfo &&
  3627. $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
  3628. $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
  3629. $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
  3630. $cgi->popup_menu(-name => 'st', -default => 'commit',
  3631. -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
  3632. " " . $cgi->a({-href => href(action=>"search_help"),
  3633. -title => "search help" }, "?") . " search:\n",
  3634. $cgi->textfield(-name => "s", -value => $searchtext, -override => 1) . "\n" .
  3635. "<span title=\"Extended regular expression\">" .
  3636. $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
  3637. -checked => $search_use_regexp) .
  3638. "</span>" .
  3639. "</div>" .
  3640. $cgi->end_form() . "\n";
  3641. }
  3642. sub git_header_html {
  3643. my $status = shift || "200 OK";
  3644. my $expires = shift;
  3645. my %opts = @_;
  3646. my $title = get_page_title();
  3647. my $content_type = get_content_type_html();
  3648. print $cgi->header(-type=>$content_type, -charset => 'utf-8',
  3649. -status=> $status, -expires => $expires)
  3650. unless ($opts{'-no_http_header'});
  3651. my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
  3652. print <<EOF;
  3653. <?xml version="1.0" encoding="utf-8"?>
  3654. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3655. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
  3656. <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
  3657. <!-- git core binaries version $git_version -->
  3658. <head>
  3659. <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
  3660. <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
  3661. <meta name="robots" content="index, nofollow"/>
  3662. <title>$title</title>
  3663. EOF
  3664. # the stylesheet, favicon etc urls won't work correctly with path_info
  3665. # unless we set the appropriate base URL
  3666. if ($ENV{'PATH_INFO'}) {
  3667. print "<base href=\"".esc_url($base_url)."\" />\n";
  3668. }
  3669. print_header_links($status);
  3670. if (defined $site_html_head_string) {
  3671. print to_utf8($site_html_head_string);
  3672. }
  3673. print "</head>\n" .
  3674. "<body>\n";
  3675. if (defined $site_header && -f $site_header) {
  3676. insert_file($site_header);
  3677. }
  3678. print "<div class=\"page_header\">\n";
  3679. if (defined $logo) {
  3680. print $cgi->a({-href => esc_url($logo_url),
  3681. -title => $logo_label},
  3682. $cgi->img({-src => esc_url($logo),
  3683. -width => 72, -height => 27,
  3684. -alt => "git",
  3685. -class => "logo"}));
  3686. }
  3687. print_nav_breadcrumbs(%opts);
  3688. print "</div>\n";
  3689. my $have_search = gitweb_check_feature('search');
  3690. if (defined $project && $have_search) {
  3691. print_search_form();
  3692. }
  3693. }
  3694. sub git_footer_html {
  3695. my $feed_class = 'rss_logo';
  3696. print "<div class=\"page_footer\">\n";
  3697. if (defined $project) {
  3698. my $descr = git_get_project_description($project);
  3699. if (defined $descr) {
  3700. print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
  3701. }
  3702. my %href_params = get_feed_info();
  3703. if (!%href_params) {
  3704. $feed_class .= ' generic';
  3705. }
  3706. $href_params{'-title'} ||= 'log';
  3707. foreach my $format (qw(RSS Atom)) {
  3708. $href_params{'action'} = lc($format);
  3709. print $cgi->a({-href => href(%href_params),
  3710. -title => "$href_params{'-title'} $format feed",
  3711. -class => $feed_class}, $format)."\n";
  3712. }
  3713. } else {
  3714. print $cgi->a({-href => href(project=>undef, action=>"opml",
  3715. project_filter => $project_filter),
  3716. -class => $feed_class}, "OPML") . " ";
  3717. print $cgi->a({-href => href(project=>undef, action=>"project_index",
  3718. project_filter => $project_filter),
  3719. -class => $feed_class}, "TXT") . "\n";
  3720. }
  3721. print "</div>\n"; # class="page_footer"
  3722. if (defined $t0 && gitweb_check_feature('timed')) {
  3723. print "<div id=\"generating_info\">\n";
  3724. print 'This page took '.
  3725. '<span id="generating_time" class="time_span">'.
  3726. tv_interval($t0, [ gettimeofday() ]).
  3727. ' seconds </span>'.
  3728. ' and '.
  3729. '<span id="generating_cmd">'.
  3730. $number_of_git_cmds.
  3731. '</span> git commands '.
  3732. " to generate.\n";
  3733. print "</div>\n"; # class="page_footer"
  3734. }
  3735. if (defined $site_footer && -f $site_footer) {
  3736. insert_file($site_footer);
  3737. }
  3738. print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
  3739. if (defined $action &&
  3740. $action eq 'blame_incremental') {
  3741. print qq!<script type="text/javascript">\n!.
  3742. qq!startBlame("!. esc_attr(href(action=>"blame_data", -replay=>1)) .qq!",\n!.
  3743. qq! "!. esc_attr(href()) .qq!");\n!.
  3744. qq!</script>\n!;
  3745. } else {
  3746. my ($jstimezone, $tz_cookie, $datetime_class) =
  3747. gitweb_get_feature('javascript-timezone');
  3748. print qq!<script type="text/javascript">\n!.
  3749. qq!window.onload = function () {\n!;
  3750. if (gitweb_check_feature('javascript-actions')) {
  3751. print qq! fixLinks();\n!;
  3752. }
  3753. if ($jstimezone && $tz_cookie && $datetime_class) {
  3754. print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
  3755. qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
  3756. }
  3757. print qq!};\n!.
  3758. qq!</script>\n!;
  3759. }
  3760. print "</body>\n" .
  3761. "</html>";
  3762. }
  3763. # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
  3764. # Example: die_error(404, 'Hash not found')
  3765. # By convention, use the following status codes (as defined in RFC 2616):
  3766. # 400: Invalid or missing CGI parameters, or
  3767. # requested object exists but has wrong type.
  3768. # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
  3769. # this server or project.
  3770. # 404: Requested object/revision/project doesn't exist.
  3771. # 500: The server isn't configured properly, or
  3772. # an internal error occurred (e.g. failed assertions caused by bugs), or
  3773. # an unknown error occurred (e.g. the git binary died unexpectedly).
  3774. # 503: The server is currently unavailable (because it is overloaded,
  3775. # or down for maintenance). Generally, this is a temporary state.
  3776. sub die_error {
  3777. my $status = shift || 500;
  3778. my $error = esc_html(shift) || "Internal Server Error";
  3779. my $extra = shift;
  3780. my %opts = @_;
  3781. my %http_responses = (
  3782. 400 => '400 Bad Request',
  3783. 403 => '403 Forbidden',
  3784. 404 => '404 Not Found',
  3785. 500 => '500 Internal Server Error',
  3786. 503 => '503 Service Unavailable',
  3787. );
  3788. git_header_html($http_responses{$status}, undef, %opts);
  3789. print <<EOF;
  3790. <div class="page_body">
  3791. <br /><br />
  3792. $status - $error
  3793. <br />
  3794. EOF
  3795. if (defined $extra) {
  3796. print "<hr />\n" .
  3797. "$extra\n";
  3798. }
  3799. print "</div>\n";
  3800. git_footer_html();
  3801. goto DONE_GITWEB
  3802. unless ($opts{'-error_handler'});
  3803. }
  3804. ## ----------------------------------------------------------------------
  3805. ## functions printing or outputting HTML: navigation
  3806. sub git_print_page_nav {
  3807. my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
  3808. $extra = '' if !defined $extra; # pager or formats
  3809. my @navs = qw(summary shortlog log commit commitdiff tree);
  3810. if ($suppress) {
  3811. @navs = grep { $_ ne $suppress } @navs;
  3812. }
  3813. my %arg = map { $_ => {action=>$_} } @navs;
  3814. if (defined $head) {
  3815. for (qw(commit commitdiff)) {
  3816. $arg{$_}{'hash'} = $head;
  3817. }
  3818. if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
  3819. for (qw(shortlog log)) {
  3820. $arg{$_}{'hash'} = $head;
  3821. }
  3822. }
  3823. }
  3824. $arg{'tree'}{'hash'} = $treehead if defined $treehead;
  3825. $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
  3826. my @actions = gitweb_get_feature('actions');
  3827. my %repl = (
  3828. '%' => '%',
  3829. 'n' => $project, # project name
  3830. 'f' => $git_dir, # project path within filesystem
  3831. 'h' => $treehead || '', # current hash ('h' parameter)
  3832. 'b' => $treebase || '', # hash base ('hb' parameter)
  3833. );
  3834. while (@actions) {
  3835. my ($label, $link, $pos) = splice(@actions,0,3);
  3836. # insert
  3837. @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
  3838. # munch munch
  3839. $link =~ s/%([%nfhb])/$repl{$1}/g;
  3840. $arg{$label}{'_href'} = $link;
  3841. }
  3842. print "<div class=\"page_nav\">\n" .
  3843. (join " | ",
  3844. map { $_ eq $current ?
  3845. $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
  3846. } @navs);
  3847. print "<br/>\n$extra<br/>\n" .
  3848. "</div>\n";
  3849. }
  3850. # returns a submenu for the navigation of the refs views (tags, heads,
  3851. # remotes) with the current view disabled and the remotes view only
  3852. # available if the feature is enabled
  3853. sub format_ref_views {
  3854. my ($current) = @_;
  3855. my @ref_views = qw{tags heads};
  3856. push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
  3857. return join " | ", map {
  3858. $_ eq $current ? $_ :
  3859. $cgi->a({-href => href(action=>$_)}, $_)
  3860. } @ref_views
  3861. }
  3862. sub format_paging_nav {
  3863. my ($action, $page, $has_next_link) = @_;
  3864. my $paging_nav;
  3865. if ($page > 0) {
  3866. $paging_nav .=
  3867. $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
  3868. " &sdot; " .
  3869. $cgi->a({-href => href(-replay=>1, page=>$page-1),
  3870. -accesskey => "p", -title => "Alt-p"}, "prev");
  3871. } else {
  3872. $paging_nav .= "first &sdot; prev";
  3873. }
  3874. if ($has_next_link) {
  3875. $paging_nav .= " &sdot; " .
  3876. $cgi->a({-href => href(-replay=>1, page=>$page+1),
  3877. -accesskey => "n", -title => "Alt-n"}, "next");
  3878. } else {
  3879. $paging_nav .= " &sdot; next";
  3880. }
  3881. return $paging_nav;
  3882. }
  3883. ## ......................................................................
  3884. ## functions printing or outputting HTML: div
  3885. sub git_print_header_div {
  3886. my ($action, $title, $hash, $hash_base) = @_;
  3887. my %args = ();
  3888. $args{'action'} = $action;
  3889. $args{'hash'} = $hash if $hash;
  3890. $args{'hash_base'} = $hash_base if $hash_base;
  3891. print "<div class=\"header\">\n" .
  3892. $cgi->a({-href => href(%args), -class => "title"},
  3893. $title ? $title : $action) .
  3894. "\n</div>\n";
  3895. }
  3896. sub format_repo_url {
  3897. my ($name, $url) = @_;
  3898. return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
  3899. }
  3900. # Group output by placing it in a DIV element and adding a header.
  3901. # Options for start_div() can be provided by passing a hash reference as the
  3902. # first parameter to the function.
  3903. # Options to git_print_header_div() can be provided by passing an array
  3904. # reference. This must follow the options to start_div if they are present.
  3905. # The content can be a scalar, which is output as-is, a scalar reference, which
  3906. # is output after html escaping, an IO handle passed either as *handle or
  3907. # *handle{IO}, or a function reference. In the latter case all following
  3908. # parameters will be taken as argument to the content function call.
  3909. sub git_print_section {
  3910. my ($div_args, $header_args, $content);
  3911. my $arg = shift;
  3912. if (ref($arg) eq 'HASH') {
  3913. $div_args = $arg;
  3914. $arg = shift;
  3915. }
  3916. if (ref($arg) eq 'ARRAY') {
  3917. $header_args = $arg;
  3918. $arg = shift;
  3919. }
  3920. $content = $arg;
  3921. print $cgi->start_div($div_args);
  3922. git_print_header_div(@$header_args);
  3923. if (ref($content) eq 'CODE') {
  3924. $content->(@_);
  3925. } elsif (ref($content) eq 'SCALAR') {
  3926. print esc_html($$content);
  3927. } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
  3928. print <$content>;
  3929. } elsif (!ref($content) && defined($content)) {
  3930. print $content;
  3931. }
  3932. print $cgi->end_div;
  3933. }
  3934. sub format_timestamp_html {
  3935. my $date = shift;
  3936. my $strtime = $date->{'rfc2822'};
  3937. my (undef, undef, $datetime_class) =
  3938. gitweb_get_feature('javascript-timezone');
  3939. if ($datetime_class) {
  3940. $strtime = qq!<span class="$datetime_class">$strtime</span>!;
  3941. }
  3942. my $localtime_format = '(%02d:%02d %s)';
  3943. if ($date->{'hour_local'} < 6) {
  3944. $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
  3945. }
  3946. $strtime .= ' ' .
  3947. sprintf($localtime_format,
  3948. $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
  3949. return $strtime;
  3950. }
  3951. # Outputs the author name and date in long form
  3952. sub git_print_authorship {
  3953. my $co = shift;
  3954. my %opts = @_;
  3955. my $tag = $opts{-tag} || 'div';
  3956. my $author = $co->{'author_name'};
  3957. my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
  3958. print "<$tag class=\"author_date\">" .
  3959. format_search_author($author, "author", esc_html($author)) .
  3960. " [".format_timestamp_html(\%ad)."]".
  3961. git_get_avatar($co->{'author_email'}, -pad_before => 1) .
  3962. "</$tag>\n";
  3963. }
  3964. # Outputs table rows containing the full author or committer information,
  3965. # in the format expected for 'commit' view (& similar).
  3966. # Parameters are a commit hash reference, followed by the list of people
  3967. # to output information for. If the list is empty it defaults to both
  3968. # author and committer.
  3969. sub git_print_authorship_rows {
  3970. my $co = shift;
  3971. # too bad we can't use @people = @_ || ('author', 'committer')
  3972. my @people = @_;
  3973. @people = ('author', 'committer') unless @people;
  3974. foreach my $who (@people) {
  3975. my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
  3976. print "<tr><td>$who</td><td>" .
  3977. format_search_author($co->{"${who}_name"}, $who,
  3978. esc_html($co->{"${who}_name"})) . " " .
  3979. format_search_author($co->{"${who}_email"}, $who,
  3980. esc_html("<" . $co->{"${who}_email"} . ">")) .
  3981. "</td><td rowspan=\"2\">" .
  3982. git_get_avatar($co->{"${who}_email"}, -size => 'double') .
  3983. "</td></tr>\n" .
  3984. "<tr>" .
  3985. "<td></td><td>" .
  3986. format_timestamp_html(\%wd) .
  3987. "</td>" .
  3988. "</tr>\n";
  3989. }
  3990. }
  3991. sub git_print_page_path {
  3992. my $name = shift;
  3993. my $type = shift;
  3994. my $hb = shift;
  3995. print "<div class=\"page_path\">";
  3996. print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
  3997. -title => 'tree root'}, to_utf8("[$project]"));
  3998. print " / ";
  3999. if (defined $name) {
  4000. my @dirname = split '/', $name;
  4001. my $basename = pop @dirname;
  4002. my $fullname = '';
  4003. foreach my $dir (@dirname) {
  4004. $fullname .= ($fullname ? '/' : '') . $dir;
  4005. print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
  4006. hash_base=>$hb),
  4007. -title => $fullname}, esc_path($dir));
  4008. print " / ";
  4009. }
  4010. if (defined $type && $type eq 'blob') {
  4011. print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
  4012. hash_base=>$hb),
  4013. -title => $name}, esc_path($basename));
  4014. } elsif (defined $type && $type eq 'tree') {
  4015. print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
  4016. hash_base=>$hb),
  4017. -title => $name}, esc_path($basename));
  4018. print " / ";
  4019. } else {
  4020. print esc_path($basename);
  4021. }
  4022. }
  4023. print "<br/></div>\n";
  4024. }
  4025. sub git_print_log {
  4026. my $log = shift;
  4027. my %opts = @_;
  4028. if ($opts{'-remove_title'}) {
  4029. # remove title, i.e. first line of log
  4030. shift @$log;
  4031. }
  4032. # remove leading empty lines
  4033. while (defined $log->[0] && $log->[0] eq "") {
  4034. shift @$log;
  4035. }
  4036. # print log
  4037. my $skip_blank_line = 0;
  4038. foreach my $line (@$log) {
  4039. if ($line =~ m/^\s*([A-Z][-A-Za-z]*-([Bb]y|[Tt]o)|C[Cc]|(Clos|Fix)es): /) {
  4040. if (! $opts{'-remove_signoff'}) {
  4041. print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
  4042. $skip_blank_line = 1;
  4043. }
  4044. next;
  4045. }
  4046. if ($line =~ m,\s*([a-z]*link): (https?://\S+),i) {
  4047. if (! $opts{'-remove_signoff'}) {
  4048. print "<span class=\"signoff\">" . esc_html($1) . ": " .
  4049. "<a href=\"" . esc_html($2) . "\">" . esc_html($2) . "</a>" .
  4050. "</span><br/>\n";
  4051. $skip_blank_line = 1;
  4052. }
  4053. next;
  4054. }
  4055. # print only one empty line
  4056. # do not print empty line after signoff
  4057. if ($line eq "") {
  4058. next if ($skip_blank_line);
  4059. $skip_blank_line = 1;
  4060. } else {
  4061. $skip_blank_line = 0;
  4062. }
  4063. print format_log_line_html($line) . "<br/>\n";
  4064. }
  4065. if ($opts{'-final_empty_line'}) {
  4066. # end with single empty line
  4067. print "<br/>\n" unless $skip_blank_line;
  4068. }
  4069. }
  4070. # return link target (what link points to)
  4071. sub git_get_link_target {
  4072. my $hash = shift;
  4073. my $link_target;
  4074. # read link
  4075. open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
  4076. or return;
  4077. {
  4078. local $/ = undef;
  4079. $link_target = <$fd>;
  4080. }
  4081. close $fd
  4082. or return;
  4083. return $link_target;
  4084. }
  4085. # given link target, and the directory (basedir) the link is in,
  4086. # return target of link relative to top directory (top tree);
  4087. # return undef if it is not possible (including absolute links).
  4088. sub normalize_link_target {
  4089. my ($link_target, $basedir) = @_;
  4090. # absolute symlinks (beginning with '/') cannot be normalized
  4091. return if (substr($link_target, 0, 1) eq '/');
  4092. # normalize link target to path from top (root) tree (dir)
  4093. my $path;
  4094. if ($basedir) {
  4095. $path = $basedir . '/' . $link_target;
  4096. } else {
  4097. # we are in top (root) tree (dir)
  4098. $path = $link_target;
  4099. }
  4100. # remove //, /./, and /../
  4101. my @path_parts;
  4102. foreach my $part (split('/', $path)) {
  4103. # discard '.' and ''
  4104. next if (!$part || $part eq '.');
  4105. # handle '..'
  4106. if ($part eq '..') {
  4107. if (@path_parts) {
  4108. pop @path_parts;
  4109. } else {
  4110. # link leads outside repository (outside top dir)
  4111. return;
  4112. }
  4113. } else {
  4114. push @path_parts, $part;
  4115. }
  4116. }
  4117. $path = join('/', @path_parts);
  4118. return $path;
  4119. }
  4120. # print tree entry (row of git_tree), but without encompassing <tr> element
  4121. sub git_print_tree_entry {
  4122. my ($t, $basedir, $hash_base, $have_blame) = @_;
  4123. my %base_key = ();
  4124. $base_key{'hash_base'} = $hash_base if defined $hash_base;
  4125. # The format of a table row is: mode list link. Where mode is
  4126. # the mode of the entry, list is the name of the entry, an href,
  4127. # and link is the action links of the entry.
  4128. print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
  4129. if (exists $t->{'size'}) {
  4130. print "<td class=\"size\">$t->{'size'}</td>\n";
  4131. }
  4132. if ($t->{'type'} eq "blob") {
  4133. print "<td class=\"list\">" .
  4134. $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
  4135. file_name=>"$basedir$t->{'name'}", %base_key),
  4136. -class => "list"}, esc_path($t->{'name'}));
  4137. if (S_ISLNK(oct $t->{'mode'})) {
  4138. my $link_target = git_get_link_target($t->{'hash'});
  4139. if ($link_target) {
  4140. my $norm_target = normalize_link_target($link_target, $basedir);
  4141. if (defined $norm_target) {
  4142. print " -> " .
  4143. $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
  4144. file_name=>$norm_target),
  4145. -title => $norm_target}, esc_path($link_target));
  4146. } else {
  4147. print " -> " . esc_path($link_target);
  4148. }
  4149. }
  4150. }
  4151. print "</td>\n";
  4152. print "<td class=\"link\">";
  4153. print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
  4154. file_name=>"$basedir$t->{'name'}", %base_key)},
  4155. "blob");
  4156. if ($have_blame) {
  4157. print " | " .
  4158. $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
  4159. file_name=>"$basedir$t->{'name'}", %base_key)},
  4160. "blame");
  4161. }
  4162. if (defined $hash_base) {
  4163. print " | " .
  4164. $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
  4165. hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
  4166. "history");
  4167. }
  4168. print " | " .
  4169. $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
  4170. file_name=>"$basedir$t->{'name'}")},
  4171. "raw");
  4172. print "</td>\n";
  4173. } elsif ($t->{'type'} eq "tree") {
  4174. print "<td class=\"list\">";
  4175. print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
  4176. file_name=>"$basedir$t->{'name'}",
  4177. %base_key)},
  4178. esc_path($t->{'name'}));
  4179. print "</td>\n";
  4180. print "<td class=\"link\">";
  4181. print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
  4182. file_name=>"$basedir$t->{'name'}",
  4183. %base_key)},
  4184. "tree");
  4185. if (defined $hash_base) {
  4186. print " | " .
  4187. $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
  4188. file_name=>"$basedir$t->{'name'}")},
  4189. "history");
  4190. }
  4191. print "</td>\n";
  4192. } else {
  4193. # unknown object: we can only present history for it
  4194. # (this includes 'commit' object, i.e. submodule support)
  4195. print "<td class=\"list\">" .
  4196. esc_path($t->{'name'}) .
  4197. "</td>\n";
  4198. print "<td class=\"link\">";
  4199. if (defined $hash_base) {
  4200. print $cgi->a({-href => href(action=>"history",
  4201. hash_base=>$hash_base,
  4202. file_name=>"$basedir$t->{'name'}")},
  4203. "history");
  4204. }
  4205. print "</td>\n";
  4206. }
  4207. }
  4208. ## ......................................................................
  4209. ## functions printing large fragments of HTML
  4210. # get pre-image filenames for merge (combined) diff
  4211. sub fill_from_file_info {
  4212. my ($diff, @parents) = @_;
  4213. $diff->{'from_file'} = [ ];
  4214. $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
  4215. for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
  4216. if ($diff->{'status'}[$i] eq 'R' ||
  4217. $diff->{'status'}[$i] eq 'C') {
  4218. $diff->{'from_file'}[$i] =
  4219. git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
  4220. }
  4221. }
  4222. return $diff;
  4223. }
  4224. # is current raw difftree line of file deletion
  4225. sub is_deleted {
  4226. my $diffinfo = shift;
  4227. return $diffinfo->{'to_id'} eq ('0' x 40) || $diffinfo->{'to_id'} eq ('0' x 64);
  4228. }
  4229. # does patch correspond to [previous] difftree raw line
  4230. # $diffinfo - hashref of parsed raw diff format
  4231. # $patchinfo - hashref of parsed patch diff format
  4232. # (the same keys as in $diffinfo)
  4233. sub is_patch_split {
  4234. my ($diffinfo, $patchinfo) = @_;
  4235. return defined $diffinfo && defined $patchinfo
  4236. && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
  4237. }
  4238. sub git_difftree_body {
  4239. my ($difftree, $hash, @parents) = @_;
  4240. my ($parent) = $parents[0];
  4241. my $have_blame = gitweb_check_feature('blame');
  4242. print "<div class=\"list_head\">\n";
  4243. if ($#{$difftree} > 10) {
  4244. print(($#{$difftree} + 1) . " files changed:\n");
  4245. }
  4246. print "</div>\n";
  4247. print "<table class=\"" .
  4248. (@parents > 1 ? "combined " : "") .
  4249. "diff_tree\">\n";
  4250. # header only for combined diff in 'commitdiff' view
  4251. my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
  4252. if ($has_header) {
  4253. # table header
  4254. print "<thead><tr>\n" .
  4255. "<th></th><th></th>\n"; # filename, patchN link
  4256. for (my $i = 0; $i < @parents; $i++) {
  4257. my $par = $parents[$i];
  4258. print "<th>" .
  4259. $cgi->a({-href => href(action=>"commitdiff",
  4260. hash=>$hash, hash_parent=>$par),
  4261. -title => 'commitdiff to parent number ' .
  4262. ($i+1) . ': ' . substr($par,0,7)},
  4263. $i+1) .
  4264. "&nbsp;</th>\n";
  4265. }
  4266. print "</tr></thead>\n<tbody>\n";
  4267. }
  4268. my $alternate = 1;
  4269. my $patchno = 0;
  4270. foreach my $line (@{$difftree}) {
  4271. my $diff = parsed_difftree_line($line);
  4272. if ($alternate) {
  4273. print "<tr class=\"dark\">\n";
  4274. } else {
  4275. print "<tr class=\"light\">\n";
  4276. }
  4277. $alternate ^= 1;
  4278. if (exists $diff->{'nparents'}) { # combined diff
  4279. fill_from_file_info($diff, @parents)
  4280. unless exists $diff->{'from_file'};
  4281. if (!is_deleted($diff)) {
  4282. # file exists in the result (child) commit
  4283. print "<td>" .
  4284. $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
  4285. file_name=>$diff->{'to_file'},
  4286. hash_base=>$hash),
  4287. -class => "list"}, esc_path($diff->{'to_file'})) .
  4288. "</td>\n";
  4289. } else {
  4290. print "<td>" .
  4291. esc_path($diff->{'to_file'}) .
  4292. "</td>\n";
  4293. }
  4294. if ($action eq 'commitdiff') {
  4295. # link to patch
  4296. $patchno++;
  4297. print "<td class=\"link\">" .
  4298. $cgi->a({-href => href(-anchor=>"patch$patchno")},
  4299. "patch") .
  4300. " | " .
  4301. "</td>\n";
  4302. }
  4303. my $has_history = 0;
  4304. my $not_deleted = 0;
  4305. for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
  4306. my $hash_parent = $parents[$i];
  4307. my $from_hash = $diff->{'from_id'}[$i];
  4308. my $from_path = $diff->{'from_file'}[$i];
  4309. my $status = $diff->{'status'}[$i];
  4310. $has_history ||= ($status ne 'A');
  4311. $not_deleted ||= ($status ne 'D');
  4312. if ($status eq 'A') {
  4313. print "<td class=\"link\" align=\"right\"> | </td>\n";
  4314. } elsif ($status eq 'D') {
  4315. print "<td class=\"link\">" .
  4316. $cgi->a({-href => href(action=>"blob",
  4317. hash_base=>$hash,
  4318. hash=>$from_hash,
  4319. file_name=>$from_path)},
  4320. "blob" . ($i+1)) .
  4321. " | </td>\n";
  4322. } else {
  4323. if ($diff->{'to_id'} eq $from_hash) {
  4324. print "<td class=\"link nochange\">";
  4325. } else {
  4326. print "<td class=\"link\">";
  4327. }
  4328. print $cgi->a({-href => href(action=>"blobdiff",
  4329. hash=>$diff->{'to_id'},
  4330. hash_parent=>$from_hash,
  4331. hash_base=>$hash,
  4332. hash_parent_base=>$hash_parent,
  4333. file_name=>$diff->{'to_file'},
  4334. file_parent=>$from_path)},
  4335. "diff" . ($i+1)) .
  4336. " | </td>\n";
  4337. }
  4338. }
  4339. print "<td class=\"link\">";
  4340. if ($not_deleted) {
  4341. print $cgi->a({-href => href(action=>"blob",
  4342. hash=>$diff->{'to_id'},
  4343. file_name=>$diff->{'to_file'},
  4344. hash_base=>$hash)},
  4345. "blob");
  4346. print " | " if ($has_history);
  4347. }
  4348. if ($has_history) {
  4349. print $cgi->a({-href => href(action=>"history",
  4350. file_name=>$diff->{'to_file'},
  4351. hash_base=>$hash)},
  4352. "history");
  4353. }
  4354. print "</td>\n";
  4355. print "</tr>\n";
  4356. next; # instead of 'else' clause, to avoid extra indent
  4357. }
  4358. # else ordinary diff
  4359. my ($to_mode_oct, $to_mode_str, $to_file_type);
  4360. my ($from_mode_oct, $from_mode_str, $from_file_type);
  4361. if ($diff->{'to_mode'} ne ('0' x 6)) {
  4362. $to_mode_oct = oct $diff->{'to_mode'};
  4363. if (S_ISREG($to_mode_oct)) { # only for regular file
  4364. $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
  4365. }
  4366. $to_file_type = file_type($diff->{'to_mode'});
  4367. }
  4368. if ($diff->{'from_mode'} ne ('0' x 6)) {
  4369. $from_mode_oct = oct $diff->{'from_mode'};
  4370. if (S_ISREG($from_mode_oct)) { # only for regular file
  4371. $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
  4372. }
  4373. $from_file_type = file_type($diff->{'from_mode'});
  4374. }
  4375. if ($diff->{'status'} eq "A") { # created
  4376. my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
  4377. $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
  4378. $mode_chng .= "]</span>";
  4379. print "<td>";
  4380. print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
  4381. hash_base=>$hash, file_name=>$diff->{'file'}),
  4382. -class => "list"}, esc_path($diff->{'file'}));
  4383. print "</td>\n";
  4384. print "<td>$mode_chng</td>\n";
  4385. print "<td class=\"link\">";
  4386. if ($action eq 'commitdiff') {
  4387. # link to patch
  4388. $patchno++;
  4389. print $cgi->a({-href => href(-anchor=>"patch$patchno")},
  4390. "patch") .
  4391. " | ";
  4392. }
  4393. print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
  4394. hash_base=>$hash, file_name=>$diff->{'file'})},
  4395. "blob");
  4396. print "</td>\n";
  4397. } elsif ($diff->{'status'} eq "D") { # deleted
  4398. my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
  4399. print "<td>";
  4400. print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
  4401. hash_base=>$parent, file_name=>$diff->{'file'}),
  4402. -class => "list"}, esc_path($diff->{'file'}));
  4403. print "</td>\n";
  4404. print "<td>$mode_chng</td>\n";
  4405. print "<td class=\"link\">";
  4406. if ($action eq 'commitdiff') {
  4407. # link to patch
  4408. $patchno++;
  4409. print $cgi->a({-href => href(-anchor=>"patch$patchno")},
  4410. "patch") .
  4411. " | ";
  4412. }
  4413. print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
  4414. hash_base=>$parent, file_name=>$diff->{'file'})},
  4415. "blob") . " | ";
  4416. if ($have_blame) {
  4417. print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
  4418. file_name=>$diff->{'file'})},
  4419. "blame") . " | ";
  4420. }
  4421. print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
  4422. file_name=>$diff->{'file'})},
  4423. "history");
  4424. print "</td>\n";
  4425. } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
  4426. my $mode_chnge = "";
  4427. if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
  4428. $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
  4429. if ($from_file_type ne $to_file_type) {
  4430. $mode_chnge .= " from $from_file_type to $to_file_type";
  4431. }
  4432. if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
  4433. if ($from_mode_str && $to_mode_str) {
  4434. $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
  4435. } elsif ($to_mode_str) {
  4436. $mode_chnge .= " mode: $to_mode_str";
  4437. }
  4438. }
  4439. $mode_chnge .= "]</span>\n";
  4440. }
  4441. print "<td>";
  4442. print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
  4443. hash_base=>$hash, file_name=>$diff->{'file'}),
  4444. -class => "list"}, esc_path($diff->{'file'}));
  4445. print "</td>\n";
  4446. print "<td>$mode_chnge</td>\n";
  4447. print "<td class=\"link\">";
  4448. if ($action eq 'commitdiff') {
  4449. # link to patch
  4450. $patchno++;
  4451. print $cgi->a({-href => href(-anchor=>"patch$patchno")},
  4452. "patch") .
  4453. " | ";
  4454. } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
  4455. # "commit" view and modified file (not onlu mode changed)
  4456. print $cgi->a({-href => href(action=>"blobdiff",
  4457. hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
  4458. hash_base=>$hash, hash_parent_base=>$parent,
  4459. file_name=>$diff->{'file'})},
  4460. "diff") .
  4461. " | ";
  4462. }
  4463. print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
  4464. hash_base=>$hash, file_name=>$diff->{'file'})},
  4465. "blob") . " | ";
  4466. if ($have_blame) {
  4467. print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
  4468. file_name=>$diff->{'file'})},
  4469. "blame") . " | ";
  4470. }
  4471. print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
  4472. file_name=>$diff->{'file'})},
  4473. "history");
  4474. print "</td>\n";
  4475. } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
  4476. my %status_name = ('R' => 'moved', 'C' => 'copied');
  4477. my $nstatus = $status_name{$diff->{'status'}};
  4478. my $mode_chng = "";
  4479. if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
  4480. # mode also for directories, so we cannot use $to_mode_str
  4481. $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
  4482. }
  4483. print "<td>" .
  4484. $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
  4485. hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
  4486. -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
  4487. "<td><span class=\"file_status $nstatus\">[$nstatus from " .
  4488. $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
  4489. hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
  4490. -class => "list"}, esc_path($diff->{'from_file'})) .
  4491. " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
  4492. "<td class=\"link\">";
  4493. if ($action eq 'commitdiff') {
  4494. # link to patch
  4495. $patchno++;
  4496. print $cgi->a({-href => href(-anchor=>"patch$patchno")},
  4497. "patch") .
  4498. " | ";
  4499. } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
  4500. # "commit" view and modified file (not only pure rename or copy)
  4501. print $cgi->a({-href => href(action=>"blobdiff",
  4502. hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
  4503. hash_base=>$hash, hash_parent_base=>$parent,
  4504. file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
  4505. "diff") .
  4506. " | ";
  4507. }
  4508. print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
  4509. hash_base=>$parent, file_name=>$diff->{'to_file'})},
  4510. "blob") . " | ";
  4511. if ($have_blame) {
  4512. print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
  4513. file_name=>$diff->{'to_file'})},
  4514. "blame") . " | ";
  4515. }
  4516. print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
  4517. file_name=>$diff->{'to_file'})},
  4518. "history");
  4519. print "</td>\n";
  4520. } # we should not encounter Unmerged (U) or Unknown (X) status
  4521. print "</tr>\n";
  4522. }
  4523. print "</tbody>" if $has_header;
  4524. print "</table>\n";
  4525. }
  4526. # Print context lines and then rem/add lines in a side-by-side manner.
  4527. sub print_sidebyside_diff_lines {
  4528. my ($ctx, $rem, $add) = @_;
  4529. # print context block before add/rem block
  4530. if (@$ctx) {
  4531. print join '',
  4532. '<div class="chunk_block ctx">',
  4533. '<div class="old">',
  4534. @$ctx,
  4535. '</div>',
  4536. '<div class="new">',
  4537. @$ctx,
  4538. '</div>',
  4539. '</div>';
  4540. }
  4541. if (!@$add) {
  4542. # pure removal
  4543. print join '',
  4544. '<div class="chunk_block rem">',
  4545. '<div class="old">',
  4546. @$rem,
  4547. '</div>',
  4548. '</div>';
  4549. } elsif (!@$rem) {
  4550. # pure addition
  4551. print join '',
  4552. '<div class="chunk_block add">',
  4553. '<div class="new">',
  4554. @$add,
  4555. '</div>',
  4556. '</div>';
  4557. } else {
  4558. print join '',
  4559. '<div class="chunk_block chg">',
  4560. '<div class="old">',
  4561. @$rem,
  4562. '</div>',
  4563. '<div class="new">',
  4564. @$add,
  4565. '</div>',
  4566. '</div>';
  4567. }
  4568. }
  4569. # Print context lines and then rem/add lines in inline manner.
  4570. sub print_inline_diff_lines {
  4571. my ($ctx, $rem, $add) = @_;
  4572. print @$ctx, @$rem, @$add;
  4573. }
  4574. # Format removed and added line, mark changed part and HTML-format them.
  4575. # Implementation is based on contrib/diff-highlight
  4576. sub format_rem_add_lines_pair {
  4577. my ($rem, $add, $num_parents) = @_;
  4578. # We need to untabify lines before split()'ing them;
  4579. # otherwise offsets would be invalid.
  4580. chomp $rem;
  4581. chomp $add;
  4582. $rem = untabify($rem);
  4583. $add = untabify($add);
  4584. my @rem = split(//, $rem);
  4585. my @add = split(//, $add);
  4586. my ($esc_rem, $esc_add);
  4587. # Ignore leading +/- characters for each parent.
  4588. my ($prefix_len, $suffix_len) = ($num_parents, 0);
  4589. my ($prefix_has_nonspace, $suffix_has_nonspace);
  4590. my $shorter = (@rem < @add) ? @rem : @add;
  4591. while ($prefix_len < $shorter) {
  4592. last if ($rem[$prefix_len] ne $add[$prefix_len]);
  4593. $prefix_has_nonspace = 1 if ($rem[$prefix_len] !~ /\s/);
  4594. $prefix_len++;
  4595. }
  4596. while ($prefix_len + $suffix_len < $shorter) {
  4597. last if ($rem[-1 - $suffix_len] ne $add[-1 - $suffix_len]);
  4598. $suffix_has_nonspace = 1 if ($rem[-1 - $suffix_len] !~ /\s/);
  4599. $suffix_len++;
  4600. }
  4601. # Mark lines that are different from each other, but have some common
  4602. # part that isn't whitespace. If lines are completely different, don't
  4603. # mark them because that would make output unreadable, especially if
  4604. # diff consists of multiple lines.
  4605. if ($prefix_has_nonspace || $suffix_has_nonspace) {
  4606. $esc_rem = esc_html_hl_regions($rem, 'marked',
  4607. [$prefix_len, @rem - $suffix_len], -nbsp=>1);
  4608. $esc_add = esc_html_hl_regions($add, 'marked',
  4609. [$prefix_len, @add - $suffix_len], -nbsp=>1);
  4610. } else {
  4611. $esc_rem = esc_html($rem, -nbsp=>1);
  4612. $esc_add = esc_html($add, -nbsp=>1);
  4613. }
  4614. return format_diff_line(\$esc_rem, 'rem'),
  4615. format_diff_line(\$esc_add, 'add');
  4616. }
  4617. # HTML-format diff context, removed and added lines.
  4618. sub format_ctx_rem_add_lines {
  4619. my ($ctx, $rem, $add, $num_parents) = @_;
  4620. my (@new_ctx, @new_rem, @new_add);
  4621. my $can_highlight = 0;
  4622. my $is_combined = ($num_parents > 1);
  4623. # Highlight if every removed line has a corresponding added line.
  4624. if (@$add > 0 && @$add == @$rem) {
  4625. $can_highlight = 1;
  4626. # Highlight lines in combined diff only if the chunk contains
  4627. # diff between the same version, e.g.
  4628. #
  4629. # - a
  4630. # - b
  4631. # + c
  4632. # + d
  4633. #
  4634. # Otherwise the highlighting would be confusing.
  4635. if ($is_combined) {
  4636. for (my $i = 0; $i < @$add; $i++) {
  4637. my $prefix_rem = substr($rem->[$i], 0, $num_parents);
  4638. my $prefix_add = substr($add->[$i], 0, $num_parents);
  4639. $prefix_rem =~ s/-/+/g;
  4640. if ($prefix_rem ne $prefix_add) {
  4641. $can_highlight = 0;
  4642. last;
  4643. }
  4644. }
  4645. }
  4646. }
  4647. if ($can_highlight) {
  4648. for (my $i = 0; $i < @$add; $i++) {
  4649. my ($line_rem, $line_add) = format_rem_add_lines_pair(
  4650. $rem->[$i], $add->[$i], $num_parents);
  4651. push @new_rem, $line_rem;
  4652. push @new_add, $line_add;
  4653. }
  4654. } else {
  4655. @new_rem = map { format_diff_line($_, 'rem') } @$rem;
  4656. @new_add = map { format_diff_line($_, 'add') } @$add;
  4657. }
  4658. @new_ctx = map { format_diff_line($_, 'ctx') } @$ctx;
  4659. return (\@new_ctx, \@new_rem, \@new_add);
  4660. }
  4661. # Print context lines and then rem/add lines.
  4662. sub print_diff_lines {
  4663. my ($ctx, $rem, $add, $diff_style, $num_parents) = @_;
  4664. my $is_combined = $num_parents > 1;
  4665. ($ctx, $rem, $add) = format_ctx_rem_add_lines($ctx, $rem, $add,
  4666. $num_parents);
  4667. if ($diff_style eq 'sidebyside' && !$is_combined) {
  4668. print_sidebyside_diff_lines($ctx, $rem, $add);
  4669. } else {
  4670. # default 'inline' style and unknown styles
  4671. print_inline_diff_lines($ctx, $rem, $add);
  4672. }
  4673. }
  4674. sub print_diff_chunk {
  4675. my ($diff_style, $num_parents, $from, $to, @chunk) = @_;
  4676. my (@ctx, @rem, @add);
  4677. # The class of the previous line.
  4678. my $prev_class = '';
  4679. return unless @chunk;
  4680. # incomplete last line might be among removed or added lines,
  4681. # or both, or among context lines: find which
  4682. for (my $i = 1; $i < @chunk; $i++) {
  4683. if ($chunk[$i][0] eq 'incomplete') {
  4684. $chunk[$i][0] = $chunk[$i-1][0];
  4685. }
  4686. }
  4687. # guardian
  4688. push @chunk, ["", ""];
  4689. foreach my $line_info (@chunk) {
  4690. my ($class, $line) = @$line_info;
  4691. # print chunk headers
  4692. if ($class && $class eq 'chunk_header') {
  4693. print format_diff_line($line, $class, $from, $to);
  4694. next;
  4695. }
  4696. ## print from accumulator when have some add/rem lines or end
  4697. # of chunk (flush context lines), or when have add and rem
  4698. # lines and new block is reached (otherwise add/rem lines could
  4699. # be reordered)
  4700. if (!$class || ((@rem || @add) && $class eq 'ctx') ||
  4701. (@rem && @add && $class ne $prev_class)) {
  4702. print_diff_lines(\@ctx, \@rem, \@add,
  4703. $diff_style, $num_parents);
  4704. @ctx = @rem = @add = ();
  4705. }
  4706. ## adding lines to accumulator
  4707. # guardian value
  4708. last unless $line;
  4709. # rem, add or change
  4710. if ($class eq 'rem') {
  4711. push @rem, $line;
  4712. } elsif ($class eq 'add') {
  4713. push @add, $line;
  4714. }
  4715. # context line
  4716. if ($class eq 'ctx') {
  4717. push @ctx, $line;
  4718. }
  4719. $prev_class = $class;
  4720. }
  4721. }
  4722. sub git_patchset_body {
  4723. my ($fd, $diff_style, $difftree, $hash, @hash_parents) = @_;
  4724. my ($hash_parent) = $hash_parents[0];
  4725. my $is_combined = (@hash_parents > 1);
  4726. my $patch_idx = 0;
  4727. my $patch_number = 0;
  4728. my $patch_line;
  4729. my $diffinfo;
  4730. my $to_name;
  4731. my (%from, %to);
  4732. my @chunk; # for side-by-side diff
  4733. print "<div class=\"patchset\">\n";
  4734. # skip to first patch
  4735. while ($patch_line = <$fd>) {
  4736. chomp $patch_line;
  4737. last if ($patch_line =~ m/^diff /);
  4738. }
  4739. PATCH:
  4740. while ($patch_line) {
  4741. # parse "git diff" header line
  4742. if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
  4743. # $1 is from_name, which we do not use
  4744. $to_name = unquote($2);
  4745. $to_name =~ s!^b/!!;
  4746. } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
  4747. # $1 is 'cc' or 'combined', which we do not use
  4748. $to_name = unquote($2);
  4749. } else {
  4750. $to_name = undef;
  4751. }
  4752. # check if current patch belong to current raw line
  4753. # and parse raw git-diff line if needed
  4754. if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
  4755. # this is continuation of a split patch
  4756. print "<div class=\"patch cont\">\n";
  4757. } else {
  4758. # advance raw git-diff output if needed
  4759. $patch_idx++ if defined $diffinfo;
  4760. # read and prepare patch information
  4761. $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
  4762. # compact combined diff output can have some patches skipped
  4763. # find which patch (using pathname of result) we are at now;
  4764. if ($is_combined) {
  4765. while ($to_name ne $diffinfo->{'to_file'}) {
  4766. print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
  4767. format_diff_cc_simplified($diffinfo, @hash_parents) .
  4768. "</div>\n"; # class="patch"
  4769. $patch_idx++;
  4770. $patch_number++;
  4771. last if $patch_idx > $#$difftree;
  4772. $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
  4773. }
  4774. }
  4775. # modifies %from, %to hashes
  4776. parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
  4777. # this is first patch for raw difftree line with $patch_idx index
  4778. # we index @$difftree array from 0, but number patches from 1
  4779. print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
  4780. }
  4781. # git diff header
  4782. #assert($patch_line =~ m/^diff /) if DEBUG;
  4783. #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
  4784. $patch_number++;
  4785. # print "git diff" header
  4786. print format_git_diff_header_line($patch_line, $diffinfo,
  4787. \%from, \%to);
  4788. # print extended diff header
  4789. print "<div class=\"diff extended_header\">\n";
  4790. EXTENDED_HEADER:
  4791. while ($patch_line = <$fd>) {
  4792. chomp $patch_line;
  4793. last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
  4794. print format_extended_diff_header_line($patch_line, $diffinfo,
  4795. \%from, \%to);
  4796. }
  4797. print "</div>\n"; # class="diff extended_header"
  4798. # from-file/to-file diff header
  4799. if (! $patch_line) {
  4800. print "</div>\n"; # class="patch"
  4801. last PATCH;
  4802. }
  4803. next PATCH if ($patch_line =~ m/^diff /);
  4804. #assert($patch_line =~ m/^---/) if DEBUG;
  4805. my $last_patch_line = $patch_line;
  4806. $patch_line = <$fd>;
  4807. chomp $patch_line;
  4808. #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
  4809. print format_diff_from_to_header($last_patch_line, $patch_line,
  4810. $diffinfo, \%from, \%to,
  4811. @hash_parents);
  4812. # the patch itself
  4813. LINE:
  4814. while ($patch_line = <$fd>) {
  4815. chomp $patch_line;
  4816. next PATCH if ($patch_line =~ m/^diff /);
  4817. my $class = diff_line_class($patch_line, \%from, \%to);
  4818. if ($class eq 'chunk_header') {
  4819. print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
  4820. @chunk = ();
  4821. }
  4822. push @chunk, [ $class, $patch_line ];
  4823. }
  4824. } continue {
  4825. if (@chunk) {
  4826. print_diff_chunk($diff_style, scalar @hash_parents, \%from, \%to, @chunk);
  4827. @chunk = ();
  4828. }
  4829. print "</div>\n"; # class="patch"
  4830. }
  4831. # for compact combined (--cc) format, with chunk and patch simplification
  4832. # the patchset might be empty, but there might be unprocessed raw lines
  4833. for (++$patch_idx if $patch_number > 0;
  4834. $patch_idx < @$difftree;
  4835. ++$patch_idx) {
  4836. # read and prepare patch information
  4837. $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
  4838. # generate anchor for "patch" links in difftree / whatchanged part
  4839. print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
  4840. format_diff_cc_simplified($diffinfo, @hash_parents) .
  4841. "</div>\n"; # class="patch"
  4842. $patch_number++;
  4843. }
  4844. if ($patch_number == 0) {
  4845. if (@hash_parents > 1) {
  4846. print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
  4847. } else {
  4848. print "<div class=\"diff nodifferences\">No differences found</div>\n";
  4849. }
  4850. }
  4851. print "</div>\n"; # class="patchset"
  4852. }
  4853. # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  4854. sub git_project_search_form {
  4855. my ($searchtext, $search_use_regexp) = @_;
  4856. my $limit = '';
  4857. if ($project_filter) {
  4858. $limit = " in '$project_filter/'";
  4859. }
  4860. print "<div class=\"projsearch\">\n";
  4861. print $cgi->start_form(-method => 'get', -action => $my_uri) .
  4862. $cgi->hidden(-name => 'a', -value => 'project_list') . "\n";
  4863. print $cgi->hidden(-name => 'pf', -value => $project_filter). "\n"
  4864. if (defined $project_filter);
  4865. print $cgi->textfield(-name => 's', -value => $searchtext,
  4866. -title => "Search project by name and description$limit",
  4867. -size => 60) . "\n" .
  4868. "<span title=\"Extended regular expression\">" .
  4869. $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
  4870. -checked => $search_use_regexp) .
  4871. "</span>\n" .
  4872. $cgi->submit(-name => 'btnS', -value => 'Search') .
  4873. $cgi->end_form() . "\n" .
  4874. $cgi->a({-href => href(project => undef, searchtext => undef,
  4875. project_filter => $project_filter)},
  4876. esc_html("List all projects$limit")) . "<br />\n";
  4877. print "</div>\n";
  4878. }
  4879. # entry for given @keys needs filling if at least one of keys in list
  4880. # is not present in %$project_info
  4881. sub project_info_needs_filling {
  4882. my ($project_info, @keys) = @_;
  4883. # return List::MoreUtils::any { !exists $project_info->{$_} } @keys;
  4884. foreach my $key (@keys) {
  4885. if (!exists $project_info->{$key}) {
  4886. return 1;
  4887. }
  4888. }
  4889. return;
  4890. }
  4891. # fills project list info (age, description, owner, category, forks, etc.)
  4892. # for each project in the list, removing invalid projects from
  4893. # returned list, or fill only specified info.
  4894. #
  4895. # Invalid projects are removed from the returned list if and only if you
  4896. # ask 'age' or 'age_string' to be filled, because they are the only fields
  4897. # that run unconditionally git command that requires repository, and
  4898. # therefore do always check if project repository is invalid.
  4899. #
  4900. # USAGE:
  4901. # * fill_project_list_info(\@project_list, 'descr_long', 'ctags')
  4902. # ensures that 'descr_long' and 'ctags' fields are filled
  4903. # * @project_list = fill_project_list_info(\@project_list)
  4904. # ensures that all fields are filled (and invalid projects removed)
  4905. #
  4906. # NOTE: modifies $projlist, but does not remove entries from it
  4907. sub fill_project_list_info {
  4908. my ($projlist, @wanted_keys) = @_;
  4909. my @projects;
  4910. my $filter_set = sub { return @_; };
  4911. if (@wanted_keys) {
  4912. my %wanted_keys = map { $_ => 1 } @wanted_keys;
  4913. $filter_set = sub { return grep { $wanted_keys{$_} } @_; };
  4914. }
  4915. my $show_ctags = gitweb_check_feature('ctags');
  4916. PROJECT:
  4917. foreach my $pr (@$projlist) {
  4918. if (project_info_needs_filling($pr, $filter_set->('age', 'age_string'))) {
  4919. my (@activity) = git_get_last_activity($pr->{'path'});
  4920. unless (@activity) {
  4921. next PROJECT;
  4922. }
  4923. ($pr->{'age'}, $pr->{'age_string'}) = @activity;
  4924. }
  4925. if (project_info_needs_filling($pr, $filter_set->('descr', 'descr_long'))) {
  4926. my $descr = git_get_project_description($pr->{'path'}) || "";
  4927. $descr = to_utf8($descr);
  4928. $pr->{'descr_long'} = $descr;
  4929. $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
  4930. }
  4931. if (project_info_needs_filling($pr, $filter_set->('owner'))) {
  4932. $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
  4933. }
  4934. if ($show_ctags &&
  4935. project_info_needs_filling($pr, $filter_set->('ctags'))) {
  4936. $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
  4937. }
  4938. if ($projects_list_group_categories &&
  4939. project_info_needs_filling($pr, $filter_set->('category'))) {
  4940. my $cat = git_get_project_category($pr->{'path'}) ||
  4941. $project_list_default_category;
  4942. $pr->{'category'} = to_utf8($cat);
  4943. }
  4944. push @projects, $pr;
  4945. }
  4946. return @projects;
  4947. }
  4948. sub sort_projects_list {
  4949. my ($projlist, $order) = @_;
  4950. sub order_str {
  4951. my $key = shift;
  4952. return sub { $a->{$key} cmp $b->{$key} };
  4953. }
  4954. sub order_num_then_undef {
  4955. my $key = shift;
  4956. return sub {
  4957. defined $a->{$key} ?
  4958. (defined $b->{$key} ? $a->{$key} <=> $b->{$key} : -1) :
  4959. (defined $b->{$key} ? 1 : 0)
  4960. };
  4961. }
  4962. my %orderings = (
  4963. project => order_str('path'),
  4964. descr => order_str('descr_long'),
  4965. owner => order_str('owner'),
  4966. age => order_num_then_undef('age'),
  4967. );
  4968. my $ordering = $orderings{$order};
  4969. return defined $ordering ? sort $ordering @$projlist : @$projlist;
  4970. }
  4971. # returns a hash of categories, containing the list of project
  4972. # belonging to each category
  4973. sub build_projlist_by_category {
  4974. my ($projlist, $from, $to) = @_;
  4975. my %categories;
  4976. $from = 0 unless defined $from;
  4977. $to = $#$projlist if (!defined $to || $#$projlist < $to);
  4978. for (my $i = $from; $i <= $to; $i++) {
  4979. my $pr = $projlist->[$i];
  4980. push @{$categories{ $pr->{'category'} }}, $pr;
  4981. }
  4982. return wantarray ? %categories : \%categories;
  4983. }
  4984. # print 'sort by' <th> element, generating 'sort by $name' replay link
  4985. # if that order is not selected
  4986. sub print_sort_th {
  4987. print format_sort_th(@_);
  4988. }
  4989. sub format_sort_th {
  4990. my ($name, $order, $header) = @_;
  4991. my $sort_th = "";
  4992. $header ||= ucfirst($name);
  4993. if ($order eq $name) {
  4994. $sort_th .= "<th>$header</th>\n";
  4995. } else {
  4996. $sort_th .= "<th>" .
  4997. $cgi->a({-href => href(-replay=>1, order=>$name),
  4998. -class => "header"}, $header) .
  4999. "</th>\n";
  5000. }
  5001. return $sort_th;
  5002. }
  5003. sub git_project_list_rows {
  5004. my ($projlist, $from, $to, $check_forks) = @_;
  5005. $from = 0 unless defined $from;
  5006. $to = $#$projlist if (!defined $to || $#$projlist < $to);
  5007. my $alternate = 1;
  5008. for (my $i = $from; $i <= $to; $i++) {
  5009. my $pr = $projlist->[$i];
  5010. if ($alternate) {
  5011. print "<tr class=\"dark\">\n";
  5012. } else {
  5013. print "<tr class=\"light\">\n";
  5014. }
  5015. $alternate ^= 1;
  5016. if ($check_forks) {
  5017. print "<td>";
  5018. if ($pr->{'forks'}) {
  5019. my $nforks = scalar @{$pr->{'forks'}};
  5020. if ($nforks > 0) {
  5021. print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
  5022. -title => "$nforks forks"}, "+");
  5023. } else {
  5024. print $cgi->span({-title => "$nforks forks"}, "+");
  5025. }
  5026. }
  5027. print "</td>\n";
  5028. }
  5029. print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
  5030. -class => "list"},
  5031. esc_html_match_hl($pr->{'path'}, $search_regexp)) .
  5032. "</td>\n" .
  5033. "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
  5034. -class => "list",
  5035. -title => $pr->{'descr_long'}},
  5036. $search_regexp
  5037. ? esc_html_match_hl_chopped($pr->{'descr_long'},
  5038. $pr->{'descr'}, $search_regexp)
  5039. : esc_html($pr->{'descr'})) .
  5040. "</td>\n";
  5041. unless ($omit_owner) {
  5042. print "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
  5043. }
  5044. unless ($omit_age_column) {
  5045. print "<td class=\"". age_class($pr->{'age'}) . "\">" .
  5046. (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n";
  5047. }
  5048. print"<td class=\"link\">" .
  5049. $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
  5050. $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
  5051. $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
  5052. $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
  5053. ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
  5054. "</td>\n" .
  5055. "</tr>\n";
  5056. }
  5057. }
  5058. sub git_project_list_body {
  5059. # actually uses global variable $project
  5060. my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
  5061. my @projects = @$projlist;
  5062. my $check_forks = gitweb_check_feature('forks');
  5063. my $show_ctags = gitweb_check_feature('ctags');
  5064. my $tagfilter = $show_ctags ? $input_params{'ctag'} : undef;
  5065. $check_forks = undef
  5066. if ($tagfilter || $search_regexp);
  5067. # filtering out forks before filling info allows to do less work
  5068. @projects = filter_forks_from_projects_list(\@projects)
  5069. if ($check_forks);
  5070. # search_projects_list pre-fills required info
  5071. @projects = search_projects_list(\@projects,
  5072. 'search_regexp' => $search_regexp,
  5073. 'tagfilter' => $tagfilter)
  5074. if ($tagfilter || $search_regexp);
  5075. # fill the rest
  5076. my @all_fields = ('descr', 'descr_long', 'ctags', 'category');
  5077. push @all_fields, ('age', 'age_string') unless($omit_age_column);
  5078. push @all_fields, 'owner' unless($omit_owner);
  5079. @projects = fill_project_list_info(\@projects, @all_fields);
  5080. $order ||= $default_projects_order;
  5081. $from = 0 unless defined $from;
  5082. $to = $#projects if (!defined $to || $#projects < $to);
  5083. # short circuit
  5084. if ($from > $to) {
  5085. print "<center>\n".
  5086. "<b>No such projects found</b><br />\n".
  5087. "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
  5088. "</center>\n<br />\n";
  5089. return;
  5090. }
  5091. @projects = sort_projects_list(\@projects, $order);
  5092. if ($show_ctags) {
  5093. my $ctags = git_gather_all_ctags(\@projects);
  5094. my $cloud = git_populate_project_tagcloud($ctags);
  5095. print git_show_project_tagcloud($cloud, 64);
  5096. }
  5097. print "<table class=\"project_list\">\n";
  5098. unless ($no_header) {
  5099. print "<tr>\n";
  5100. if ($check_forks) {
  5101. print "<th></th>\n";
  5102. }
  5103. print_sort_th('project', $order, 'Project');
  5104. print_sort_th('descr', $order, 'Description');
  5105. print_sort_th('owner', $order, 'Owner') unless $omit_owner;
  5106. print_sort_th('age', $order, 'Last Change') unless $omit_age_column;
  5107. print "<th></th>\n" . # for links
  5108. "</tr>\n";
  5109. }
  5110. if ($projects_list_group_categories) {
  5111. # only display categories with projects in the $from-$to window
  5112. @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
  5113. my %categories = build_projlist_by_category(\@projects, $from, $to);
  5114. foreach my $cat (sort keys %categories) {
  5115. unless ($cat eq "") {
  5116. print "<tr>\n";
  5117. if ($check_forks) {
  5118. print "<td></td>\n";
  5119. }
  5120. print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
  5121. print "</tr>\n";
  5122. }
  5123. git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
  5124. }
  5125. } else {
  5126. git_project_list_rows(\@projects, $from, $to, $check_forks);
  5127. }
  5128. if (defined $extra) {
  5129. print "<tr>\n";
  5130. if ($check_forks) {
  5131. print "<td></td>\n";
  5132. }
  5133. print "<td colspan=\"5\">$extra</td>\n" .
  5134. "</tr>\n";
  5135. }
  5136. print "</table>\n";
  5137. }
  5138. sub git_log_body {
  5139. # uses global variable $project
  5140. my ($commitlist, $from, $to, $refs, $extra) = @_;
  5141. $from = 0 unless defined $from;
  5142. $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
  5143. for (my $i = 0; $i <= $to; $i++) {
  5144. my %co = %{$commitlist->[$i]};
  5145. next if !%co;
  5146. my $commit = $co{'id'};
  5147. my $ref = format_ref_marker($refs, $commit);
  5148. git_print_header_div('commit',
  5149. "<span class=\"age\">$co{'age_string'}</span>" .
  5150. esc_html($co{'title'}) . $ref,
  5151. $commit);
  5152. print "<div class=\"title_text\">\n" .
  5153. "<div class=\"log_link\">\n" .
  5154. $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
  5155. " | " .
  5156. $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
  5157. " | " .
  5158. $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
  5159. "<br/>\n" .
  5160. "</div>\n";
  5161. git_print_authorship(\%co, -tag => 'span');
  5162. print "<br/>\n</div>\n";
  5163. print "<div class=\"log_body\">\n";
  5164. git_print_log($co{'comment'}, -final_empty_line=> 1);
  5165. print "</div>\n";
  5166. }
  5167. if ($extra) {
  5168. print "<div class=\"page_nav\">\n";
  5169. print "$extra\n";
  5170. print "</div>\n";
  5171. }
  5172. }
  5173. sub git_shortlog_body {
  5174. # uses global variable $project
  5175. my ($commitlist, $from, $to, $refs, $extra) = @_;
  5176. $from = 0 unless defined $from;
  5177. $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
  5178. print "<table class=\"shortlog\">\n";
  5179. my $alternate = 1;
  5180. for (my $i = $from; $i <= $to; $i++) {
  5181. my %co = %{$commitlist->[$i]};
  5182. my $commit = $co{'id'};
  5183. my $ref = format_ref_marker($refs, $commit);
  5184. if ($alternate) {
  5185. print "<tr class=\"dark\">\n";
  5186. } else {
  5187. print "<tr class=\"light\">\n";
  5188. }
  5189. $alternate ^= 1;
  5190. # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
  5191. print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
  5192. format_author_html('td', \%co, 10) . "<td>";
  5193. print format_subject_html($co{'title'}, $co{'title_short'},
  5194. href(action=>"commit", hash=>$commit), $ref);
  5195. print "</td>\n" .
  5196. "<td class=\"link\">" .
  5197. $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
  5198. $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
  5199. $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
  5200. my $snapshot_links = format_snapshot_links($commit);
  5201. if (defined $snapshot_links) {
  5202. print " | " . $snapshot_links;
  5203. }
  5204. print "</td>\n" .
  5205. "</tr>\n";
  5206. }
  5207. if (defined $extra) {
  5208. print "<tr>\n" .
  5209. "<td colspan=\"4\">$extra</td>\n" .
  5210. "</tr>\n";
  5211. }
  5212. print "</table>\n";
  5213. }
  5214. sub git_history_body {
  5215. # Warning: assumes constant type (blob or tree) during history
  5216. my ($commitlist, $from, $to, $refs, $extra,
  5217. $file_name, $file_hash, $ftype) = @_;
  5218. $from = 0 unless defined $from;
  5219. $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
  5220. print "<table class=\"history\">\n";
  5221. my $alternate = 1;
  5222. for (my $i = $from; $i <= $to; $i++) {
  5223. my %co = %{$commitlist->[$i]};
  5224. if (!%co) {
  5225. next;
  5226. }
  5227. my $commit = $co{'id'};
  5228. my $ref = format_ref_marker($refs, $commit);
  5229. if ($alternate) {
  5230. print "<tr class=\"dark\">\n";
  5231. } else {
  5232. print "<tr class=\"light\">\n";
  5233. }
  5234. $alternate ^= 1;
  5235. print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
  5236. # shortlog: format_author_html('td', \%co, 10)
  5237. format_author_html('td', \%co, 15, 3) . "<td>";
  5238. # originally git_history used chop_str($co{'title'}, 50)
  5239. print format_subject_html($co{'title'}, $co{'title_short'},
  5240. href(action=>"commit", hash=>$commit), $ref);
  5241. print "</td>\n" .
  5242. "<td class=\"link\">" .
  5243. $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
  5244. $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
  5245. if ($ftype eq 'blob') {
  5246. print " | " .
  5247. $cgi->a({-href => href(action=>"blob_plain", hash_base=>$commit, file_name=>$file_name)}, "raw");
  5248. my $blob_current = $file_hash;
  5249. my $blob_parent = git_get_hash_by_path($commit, $file_name);
  5250. if (defined $blob_current && defined $blob_parent &&
  5251. $blob_current ne $blob_parent) {
  5252. print " | " .
  5253. $cgi->a({-href => href(action=>"blobdiff",
  5254. hash=>$blob_current, hash_parent=>$blob_parent,
  5255. hash_base=>$hash_base, hash_parent_base=>$commit,
  5256. file_name=>$file_name)},
  5257. "diff to current");
  5258. }
  5259. }
  5260. print "</td>\n" .
  5261. "</tr>\n";
  5262. }
  5263. if (defined $extra) {
  5264. print "<tr>\n" .
  5265. "<td colspan=\"4\">$extra</td>\n" .
  5266. "</tr>\n";
  5267. }
  5268. print "</table>\n";
  5269. }
  5270. sub git_tags_body {
  5271. # uses global variable $project
  5272. my ($taglist, $from, $to, $extra) = @_;
  5273. $from = 0 unless defined $from;
  5274. $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
  5275. print "<table class=\"tags\">\n";
  5276. my $alternate = 1;
  5277. for (my $i = $from; $i <= $to; $i++) {
  5278. my $entry = $taglist->[$i];
  5279. my %tag = %$entry;
  5280. my $comment = $tag{'subject'};
  5281. my $comment_short;
  5282. if (defined $comment) {
  5283. $comment_short = chop_str($comment, 30, 5);
  5284. }
  5285. if ($alternate) {
  5286. print "<tr class=\"dark\">\n";
  5287. } else {
  5288. print "<tr class=\"light\">\n";
  5289. }
  5290. $alternate ^= 1;
  5291. if (defined $tag{'age'}) {
  5292. print "<td><i>$tag{'age'}</i></td>\n";
  5293. } else {
  5294. print "<td></td>\n";
  5295. }
  5296. print "<td>" .
  5297. $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
  5298. -class => "list name"}, esc_html($tag{'name'})) .
  5299. "</td>\n" .
  5300. "<td>";
  5301. if (defined $comment) {
  5302. print format_subject_html($comment, $comment_short,
  5303. href(action=>"tag", hash=>$tag{'id'}));
  5304. }
  5305. print "</td>\n" .
  5306. "<td class=\"selflink\">";
  5307. if ($tag{'type'} eq "tag") {
  5308. print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
  5309. } else {
  5310. print "&nbsp;";
  5311. }
  5312. print "</td>\n" .
  5313. "<td class=\"link\">" . " | " .
  5314. $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
  5315. if ($tag{'reftype'} eq "commit") {
  5316. print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
  5317. " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
  5318. } elsif ($tag{'reftype'} eq "blob") {
  5319. print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
  5320. }
  5321. print "</td>\n" .
  5322. "</tr>";
  5323. }
  5324. if (defined $extra) {
  5325. print "<tr>\n" .
  5326. "<td colspan=\"5\">$extra</td>\n" .
  5327. "</tr>\n";
  5328. }
  5329. print "</table>\n";
  5330. }
  5331. sub git_heads_body {
  5332. # uses global variable $project
  5333. my ($headlist, $head_at, $from, $to, $extra) = @_;
  5334. $from = 0 unless defined $from;
  5335. $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
  5336. print "<table class=\"heads\">\n";
  5337. my $alternate = 1;
  5338. for (my $i = $from; $i <= $to; $i++) {
  5339. my $entry = $headlist->[$i];
  5340. my %ref = %$entry;
  5341. my $curr = defined $head_at && $ref{'id'} eq $head_at;
  5342. if ($alternate) {
  5343. print "<tr class=\"dark\">\n";
  5344. } else {
  5345. print "<tr class=\"light\">\n";
  5346. }
  5347. $alternate ^= 1;
  5348. print "<td><i>$ref{'age'}</i></td>\n" .
  5349. ($curr ? "<td class=\"current_head\">" : "<td>") .
  5350. $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
  5351. -class => "list name"},esc_html($ref{'name'})) .
  5352. "</td>\n" .
  5353. "<td class=\"link\">" .
  5354. $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
  5355. $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
  5356. $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
  5357. "</td>\n" .
  5358. "</tr>";
  5359. }
  5360. if (defined $extra) {
  5361. print "<tr>\n" .
  5362. "<td colspan=\"3\">$extra</td>\n" .
  5363. "</tr>\n";
  5364. }
  5365. print "</table>\n";
  5366. }
  5367. # Display a single remote block
  5368. sub git_remote_block {
  5369. my ($remote, $rdata, $limit, $head) = @_;
  5370. my $heads = $rdata->{'heads'};
  5371. my $fetch = $rdata->{'fetch'};
  5372. my $push = $rdata->{'push'};
  5373. my $urls_table = "<table class=\"projects_list\">\n" ;
  5374. if (defined $fetch) {
  5375. if ($fetch eq $push) {
  5376. $urls_table .= format_repo_url("URL", $fetch);
  5377. } else {
  5378. $urls_table .= format_repo_url("Fetch URL", $fetch);
  5379. $urls_table .= format_repo_url("Push URL", $push) if defined $push;
  5380. }
  5381. } elsif (defined $push) {
  5382. $urls_table .= format_repo_url("Push URL", $push);
  5383. } else {
  5384. $urls_table .= format_repo_url("", "No remote URL");
  5385. }
  5386. $urls_table .= "</table>\n";
  5387. my $dots;
  5388. if (defined $limit && $limit < @$heads) {
  5389. $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
  5390. }
  5391. print $urls_table;
  5392. git_heads_body($heads, $head, 0, $limit, $dots);
  5393. }
  5394. # Display a list of remote names with the respective fetch and push URLs
  5395. sub git_remotes_list {
  5396. my ($remotedata, $limit) = @_;
  5397. print "<table class=\"heads\">\n";
  5398. my $alternate = 1;
  5399. my @remotes = sort keys %$remotedata;
  5400. my $limited = $limit && $limit < @remotes;
  5401. $#remotes = $limit - 1 if $limited;
  5402. while (my $remote = shift @remotes) {
  5403. my $rdata = $remotedata->{$remote};
  5404. my $fetch = $rdata->{'fetch'};
  5405. my $push = $rdata->{'push'};
  5406. if ($alternate) {
  5407. print "<tr class=\"dark\">\n";
  5408. } else {
  5409. print "<tr class=\"light\">\n";
  5410. }
  5411. $alternate ^= 1;
  5412. print "<td>" .
  5413. $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
  5414. -class=> "list name"},esc_html($remote)) .
  5415. "</td>";
  5416. print "<td class=\"link\">" .
  5417. (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
  5418. " | " .
  5419. (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
  5420. "</td>";
  5421. print "</tr>\n";
  5422. }
  5423. if ($limited) {
  5424. print "<tr>\n" .
  5425. "<td colspan=\"3\">" .
  5426. $cgi->a({-href => href(action=>"remotes")}, "...") .
  5427. "</td>\n" . "</tr>\n";
  5428. }
  5429. print "</table>";
  5430. }
  5431. # Display remote heads grouped by remote, unless there are too many
  5432. # remotes, in which case we only display the remote names
  5433. sub git_remotes_body {
  5434. my ($remotedata, $limit, $head) = @_;
  5435. if ($limit and $limit < keys %$remotedata) {
  5436. git_remotes_list($remotedata, $limit);
  5437. } else {
  5438. fill_remote_heads($remotedata);
  5439. while (my ($remote, $rdata) = each %$remotedata) {
  5440. git_print_section({-class=>"remote", -id=>$remote},
  5441. ["remotes", $remote, $remote], sub {
  5442. git_remote_block($remote, $rdata, $limit, $head);
  5443. });
  5444. }
  5445. }
  5446. }
  5447. sub git_search_message {
  5448. my %co = @_;
  5449. my $greptype;
  5450. if ($searchtype eq 'commit') {
  5451. $greptype = "--grep=";
  5452. } elsif ($searchtype eq 'author') {
  5453. $greptype = "--author=";
  5454. } elsif ($searchtype eq 'committer') {
  5455. $greptype = "--committer=";
  5456. }
  5457. $greptype .= $searchtext;
  5458. my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
  5459. $greptype, '--regexp-ignore-case',
  5460. $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
  5461. my $paging_nav = '';
  5462. if ($page > 0) {
  5463. $paging_nav .=
  5464. $cgi->a({-href => href(-replay=>1, page=>undef)},
  5465. "first") .
  5466. " &sdot; " .
  5467. $cgi->a({-href => href(-replay=>1, page=>$page-1),
  5468. -accesskey => "p", -title => "Alt-p"}, "prev");
  5469. } else {
  5470. $paging_nav .= "first &sdot; prev";
  5471. }
  5472. my $next_link = '';
  5473. if ($#commitlist >= 100) {
  5474. $next_link =
  5475. $cgi->a({-href => href(-replay=>1, page=>$page+1),
  5476. -accesskey => "n", -title => "Alt-n"}, "next");
  5477. $paging_nav .= " &sdot; $next_link";
  5478. } else {
  5479. $paging_nav .= " &sdot; next";
  5480. }
  5481. git_header_html();
  5482. git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
  5483. git_print_header_div('commit', esc_html($co{'title'}), $hash);
  5484. if ($page == 0 && !@commitlist) {
  5485. print "<p>No match.</p>\n";
  5486. } else {
  5487. git_search_grep_body(\@commitlist, 0, 99, $next_link);
  5488. }
  5489. git_footer_html();
  5490. }
  5491. sub git_search_changes {
  5492. my %co = @_;
  5493. local $/ = "\n";
  5494. open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
  5495. '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
  5496. ($search_use_regexp ? '--pickaxe-regex' : ())
  5497. or die_error(500, "Open git-log failed");
  5498. git_header_html();
  5499. git_print_page_nav('','', $hash,$co{'tree'},$hash);
  5500. git_print_header_div('commit', esc_html($co{'title'}), $hash);
  5501. print "<table class=\"pickaxe search\">\n";
  5502. my $alternate = 1;
  5503. undef %co;
  5504. my @files;
  5505. while (my $line = <$fd>) {
  5506. chomp $line;
  5507. next unless $line;
  5508. my %set = parse_difftree_raw_line($line);
  5509. if (defined $set{'commit'}) {
  5510. # finish previous commit
  5511. if (%co) {
  5512. print "</td>\n" .
  5513. "<td class=\"link\">" .
  5514. $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
  5515. "commit") .
  5516. " | " .
  5517. $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
  5518. hash_base=>$co{'id'})},
  5519. "tree") .
  5520. "</td>\n" .
  5521. "</tr>\n";
  5522. }
  5523. if ($alternate) {
  5524. print "<tr class=\"dark\">\n";
  5525. } else {
  5526. print "<tr class=\"light\">\n";
  5527. }
  5528. $alternate ^= 1;
  5529. %co = parse_commit($set{'commit'});
  5530. my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
  5531. print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
  5532. "<td><i>$author</i></td>\n" .
  5533. "<td>" .
  5534. $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
  5535. -class => "list subject"},
  5536. chop_and_escape_str($co{'title'}, 50) . "<br/>");
  5537. } elsif (defined $set{'to_id'}) {
  5538. next if is_deleted(\%set);
  5539. print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
  5540. hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
  5541. -class => "list"},
  5542. "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
  5543. "<br/>\n";
  5544. }
  5545. }
  5546. close $fd;
  5547. # finish last commit (warning: repetition!)
  5548. if (%co) {
  5549. print "</td>\n" .
  5550. "<td class=\"link\">" .
  5551. $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},
  5552. "commit") .
  5553. " | " .
  5554. $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},
  5555. hash_base=>$co{'id'})},
  5556. "tree") .
  5557. "</td>\n" .
  5558. "</tr>\n";
  5559. }
  5560. print "</table>\n";
  5561. git_footer_html();
  5562. }
  5563. sub git_search_files {
  5564. my %co = @_;
  5565. local $/ = "\n";
  5566. open my $fd, "-|", git_cmd(), 'grep', '-n', '-z',
  5567. $search_use_regexp ? ('-E', '-i') : '-F',
  5568. $searchtext, $co{'tree'}
  5569. or die_error(500, "Open git-grep failed");
  5570. git_header_html();
  5571. git_print_page_nav('','', $hash,$co{'tree'},$hash);
  5572. git_print_header_div('commit', esc_html($co{'title'}), $hash);
  5573. print "<table class=\"grep_search\">\n";
  5574. my $alternate = 1;
  5575. my $matches = 0;
  5576. my $lastfile = '';
  5577. my $file_href;
  5578. while (my $line = <$fd>) {
  5579. chomp $line;
  5580. my ($file, $lno, $ltext, $binary);
  5581. last if ($matches++ > 1000);
  5582. if ($line =~ /^Binary file (.+) matches$/) {
  5583. $file = $1;
  5584. $binary = 1;
  5585. } else {
  5586. ($file, $lno, $ltext) = split(/\0/, $line, 3);
  5587. $file =~ s/^$co{'tree'}://;
  5588. }
  5589. if ($file ne $lastfile) {
  5590. $lastfile and print "</td></tr>\n";
  5591. if ($alternate++) {
  5592. print "<tr class=\"dark\">\n";
  5593. } else {
  5594. print "<tr class=\"light\">\n";
  5595. }
  5596. $file_href = href(action=>"blob", hash_base=>$co{'id'},
  5597. file_name=>$file);
  5598. print "<td class=\"list\">".
  5599. $cgi->a({-href => $file_href, -class => "list"}, esc_path($file));
  5600. print "</td><td>\n";
  5601. $lastfile = $file;
  5602. }
  5603. if ($binary) {
  5604. print "<div class=\"binary\">Binary file</div>\n";
  5605. } else {
  5606. $ltext = untabify($ltext);
  5607. if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
  5608. $ltext = esc_html($1, -nbsp=>1);
  5609. $ltext .= '<span class="match">';
  5610. $ltext .= esc_html($2, -nbsp=>1);
  5611. $ltext .= '</span>';
  5612. $ltext .= esc_html($3, -nbsp=>1);
  5613. } else {
  5614. $ltext = esc_html($ltext, -nbsp=>1);
  5615. }
  5616. print "<div class=\"pre\">" .
  5617. $cgi->a({-href => $file_href.'#l'.$lno,
  5618. -class => "linenr"}, sprintf('%4i', $lno)) .
  5619. ' ' . $ltext . "</div>\n";
  5620. }
  5621. }
  5622. if ($lastfile) {
  5623. print "</td></tr>\n";
  5624. if ($matches > 1000) {
  5625. print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
  5626. }
  5627. } else {
  5628. print "<div class=\"diff nodifferences\">No matches found</div>\n";
  5629. }
  5630. close $fd;
  5631. print "</table>\n";
  5632. git_footer_html();
  5633. }
  5634. sub git_search_grep_body {
  5635. my ($commitlist, $from, $to, $extra) = @_;
  5636. $from = 0 unless defined $from;
  5637. $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
  5638. print "<table class=\"commit_search\">\n";
  5639. my $alternate = 1;
  5640. for (my $i = $from; $i <= $to; $i++) {
  5641. my %co = %{$commitlist->[$i]};
  5642. if (!%co) {
  5643. next;
  5644. }
  5645. my $commit = $co{'id'};
  5646. if ($alternate) {
  5647. print "<tr class=\"dark\">\n";
  5648. } else {
  5649. print "<tr class=\"light\">\n";
  5650. }
  5651. $alternate ^= 1;
  5652. print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
  5653. format_author_html('td', \%co, 15, 5) .
  5654. "<td>" .
  5655. $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
  5656. -class => "list subject"},
  5657. chop_and_escape_str($co{'title'}, 50) . "<br/>");
  5658. my $comment = $co{'comment'};
  5659. foreach my $line (@$comment) {
  5660. if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
  5661. my ($lead, $match, $trail) = ($1, $2, $3);
  5662. $match = chop_str($match, 70, 5, 'center');
  5663. my $contextlen = int((80 - length($match))/2);
  5664. $contextlen = 30 if ($contextlen > 30);
  5665. $lead = chop_str($lead, $contextlen, 10, 'left');
  5666. $trail = chop_str($trail, $contextlen, 10, 'right');
  5667. $lead = esc_html($lead);
  5668. $match = esc_html($match);
  5669. $trail = esc_html($trail);
  5670. print "$lead<span class=\"match\">$match</span>$trail<br />";
  5671. }
  5672. }
  5673. print "</td>\n" .
  5674. "<td class=\"link\">" .
  5675. $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
  5676. " | " .
  5677. $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
  5678. " | " .
  5679. $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
  5680. print "</td>\n" .
  5681. "</tr>\n";
  5682. }
  5683. if (defined $extra) {
  5684. print "<tr>\n" .
  5685. "<td colspan=\"3\">$extra</td>\n" .
  5686. "</tr>\n";
  5687. }
  5688. print "</table>\n";
  5689. }
  5690. ## ======================================================================
  5691. ## ======================================================================
  5692. ## actions
  5693. sub git_project_list {
  5694. my $order = $input_params{'order'};
  5695. if (defined $order && $order !~ m/none|project|descr|owner|age/) {
  5696. die_error(400, "Unknown order parameter");
  5697. }
  5698. my @list = git_get_projects_list($project_filter, $strict_export);
  5699. if (!@list) {
  5700. die_error(404, "No projects found");
  5701. }
  5702. git_header_html();
  5703. if (defined $home_text && -f $home_text) {
  5704. print "<div class=\"index_include\">\n";
  5705. insert_file($home_text);
  5706. print "</div>\n";
  5707. }
  5708. git_project_search_form($searchtext, $search_use_regexp);
  5709. git_project_list_body(\@list, $order);
  5710. git_footer_html();
  5711. }
  5712. sub git_forks {
  5713. my $order = $input_params{'order'};
  5714. if (defined $order && $order !~ m/none|project|descr|owner|age/) {
  5715. die_error(400, "Unknown order parameter");
  5716. }
  5717. my $filter = $project;
  5718. $filter =~ s/\.git$//;
  5719. my @list = git_get_projects_list($filter);
  5720. if (!@list) {
  5721. die_error(404, "No forks found");
  5722. }
  5723. git_header_html();
  5724. git_print_page_nav('','');
  5725. git_print_header_div('summary', "$project forks");
  5726. git_project_list_body(\@list, $order);
  5727. git_footer_html();
  5728. }
  5729. sub git_project_index {
  5730. my @projects = git_get_projects_list($project_filter, $strict_export);
  5731. if (!@projects) {
  5732. die_error(404, "No projects found");
  5733. }
  5734. print $cgi->header(
  5735. -type => 'text/plain',
  5736. -charset => 'utf-8',
  5737. -content_disposition => 'inline; filename="index.aux"');
  5738. foreach my $pr (@projects) {
  5739. if (!exists $pr->{'owner'}) {
  5740. $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
  5741. }
  5742. my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
  5743. # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
  5744. $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
  5745. $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
  5746. $path =~ s/ /\+/g;
  5747. $owner =~ s/ /\+/g;
  5748. print "$path $owner\n";
  5749. }
  5750. }
  5751. sub git_summary {
  5752. my $descr = git_get_project_description($project) || "none";
  5753. my %co = parse_commit("HEAD");
  5754. my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
  5755. my $head = $co{'id'};
  5756. my $remote_heads = gitweb_check_feature('remote_heads');
  5757. my $owner = git_get_project_owner($project);
  5758. my $refs = git_get_references();
  5759. # These get_*_list functions return one more to allow us to see if
  5760. # there are more ...
  5761. my @taglist = git_get_tags_list(16);
  5762. my @headlist = git_get_heads_list(16);
  5763. my %remotedata = $remote_heads ? git_get_remotes_list() : ();
  5764. my @forklist;
  5765. my $check_forks = gitweb_check_feature('forks');
  5766. if ($check_forks) {
  5767. # find forks of a project
  5768. my $filter = $project;
  5769. $filter =~ s/\.git$//;
  5770. @forklist = git_get_projects_list($filter);
  5771. # filter out forks of forks
  5772. @forklist = filter_forks_from_projects_list(\@forklist)
  5773. if (@forklist);
  5774. }
  5775. git_header_html();
  5776. git_print_page_nav('summary','', $head);
  5777. print "<div class=\"title\">&nbsp;</div>\n";
  5778. print "<table class=\"projects_list\">\n" .
  5779. "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
  5780. if ($owner and not $omit_owner) {
  5781. print "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
  5782. }
  5783. if (defined $cd{'rfc2822'}) {
  5784. print "<tr id=\"metadata_lchange\"><td>last change</td>" .
  5785. "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
  5786. }
  5787. # use per project git URL list in $projectroot/$project/cloneurl
  5788. # or make project git URL from git base URL and project name
  5789. my $url_tag = "URL";
  5790. my @url_list = git_get_project_url_list($project);
  5791. @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
  5792. foreach my $git_url (@url_list) {
  5793. next unless $git_url;
  5794. print format_repo_url($url_tag, $git_url);
  5795. $url_tag = "";
  5796. }
  5797. # Tag cloud
  5798. my $show_ctags = gitweb_check_feature('ctags');
  5799. if ($show_ctags) {
  5800. my $ctags = git_get_project_ctags($project);
  5801. if (%$ctags) {
  5802. # without ability to add tags, don't show if there are none
  5803. my $cloud = git_populate_project_tagcloud($ctags);
  5804. print "<tr id=\"metadata_ctags\">" .
  5805. "<td>content tags</td>" .
  5806. "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
  5807. "</tr>\n";
  5808. }
  5809. }
  5810. print "</table>\n";
  5811. # If XSS prevention is on, we don't include README.html.
  5812. # TODO: Allow a readme in some safe format.
  5813. if (!$prevent_xss && -s "$projectroot/$project/README.html") {
  5814. print "<div class=\"title\">readme</div>\n" .
  5815. "<div class=\"readme\">\n";
  5816. insert_file("$projectroot/$project/README.html");
  5817. print "\n</div>\n"; # class="readme"
  5818. }
  5819. # we need to request one more than 16 (0..15) to check if
  5820. # those 16 are all
  5821. my @commitlist = $head ? parse_commits($head, 17) : ();
  5822. if (@commitlist) {
  5823. git_print_header_div('shortlog');
  5824. git_shortlog_body(\@commitlist, 0, 15, $refs,
  5825. $#commitlist <= 15 ? undef :
  5826. $cgi->a({-href => href(action=>"shortlog")}, "..."));
  5827. }
  5828. if (@taglist) {
  5829. git_print_header_div('tags');
  5830. git_tags_body(\@taglist, 0, 15,
  5831. $#taglist <= 15 ? undef :
  5832. $cgi->a({-href => href(action=>"tags")}, "..."));
  5833. }
  5834. if (@headlist) {
  5835. git_print_header_div('heads');
  5836. git_heads_body(\@headlist, $head, 0, 15,
  5837. $#headlist <= 15 ? undef :
  5838. $cgi->a({-href => href(action=>"heads")}, "..."));
  5839. }
  5840. if (%remotedata) {
  5841. git_print_header_div('remotes');
  5842. git_remotes_body(\%remotedata, 15, $head);
  5843. }
  5844. if (@forklist) {
  5845. git_print_header_div('forks');
  5846. git_project_list_body(\@forklist, 'age', 0, 15,
  5847. $#forklist <= 15 ? undef :
  5848. $cgi->a({-href => href(action=>"forks")}, "..."),
  5849. 'no_header');
  5850. }
  5851. git_footer_html();
  5852. }
  5853. sub git_tag {
  5854. my %tag = parse_tag($hash);
  5855. if (! %tag) {
  5856. die_error(404, "Unknown tag object");
  5857. }
  5858. my $head = git_get_head_hash($project);
  5859. git_header_html();
  5860. git_print_page_nav('','', $head,undef,$head);
  5861. git_print_header_div('commit', esc_html($tag{'name'}), $hash);
  5862. print "<div class=\"title_text\">\n" .
  5863. "<table class=\"object_header\">\n" .
  5864. "<tr>\n" .
  5865. "<td>object</td>\n" .
  5866. "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
  5867. $tag{'object'}) . "</td>\n" .
  5868. "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
  5869. $tag{'type'}) . "</td>\n" .
  5870. "</tr>\n";
  5871. if (defined($tag{'author'})) {
  5872. git_print_authorship_rows(\%tag, 'author');
  5873. }
  5874. print "</table>\n\n" .
  5875. "</div>\n";
  5876. print "<div class=\"page_body\">";
  5877. my $comment = $tag{'comment'};
  5878. foreach my $line (@$comment) {
  5879. chomp $line;
  5880. print esc_html($line, -nbsp=>1) . "<br/>\n";
  5881. }
  5882. print "</div>\n";
  5883. git_footer_html();
  5884. }
  5885. sub git_blame_common {
  5886. my $format = shift || 'porcelain';
  5887. if ($format eq 'porcelain' && $input_params{'javascript'}) {
  5888. $format = 'incremental';
  5889. $action = 'blame_incremental'; # for page title etc
  5890. }
  5891. # permissions
  5892. gitweb_check_feature('blame')
  5893. or die_error(403, "Blame view not allowed");
  5894. # error checking
  5895. die_error(400, "No file name given") unless $file_name;
  5896. $hash_base ||= git_get_head_hash($project);
  5897. die_error(404, "Couldn't find base commit") unless $hash_base;
  5898. my %co = parse_commit($hash_base)
  5899. or die_error(404, "Commit not found");
  5900. my $ftype = "blob";
  5901. if (!defined $hash) {
  5902. $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
  5903. or die_error(404, "Error looking up file");
  5904. } else {
  5905. $ftype = git_get_type($hash);
  5906. if ($ftype !~ "blob") {
  5907. die_error(400, "Object is not a blob");
  5908. }
  5909. }
  5910. my $fd;
  5911. if ($format eq 'incremental') {
  5912. # get file contents (as base)
  5913. open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
  5914. or die_error(500, "Open git-cat-file failed");
  5915. } elsif ($format eq 'data') {
  5916. # run git-blame --incremental
  5917. open $fd, "-|", git_cmd(), "blame", "--incremental",
  5918. $hash_base, "--", $file_name
  5919. or die_error(500, "Open git-blame --incremental failed");
  5920. } else {
  5921. # run git-blame --porcelain
  5922. open $fd, "-|", git_cmd(), "blame", '-p',
  5923. $hash_base, '--', $file_name
  5924. or die_error(500, "Open git-blame --porcelain failed");
  5925. }
  5926. binmode $fd, ':utf8';
  5927. # incremental blame data returns early
  5928. if ($format eq 'data') {
  5929. print $cgi->header(
  5930. -type=>"text/plain", -charset => "utf-8",
  5931. -status=> "200 OK");
  5932. local $| = 1; # output autoflush
  5933. while (my $line = <$fd>) {
  5934. print to_utf8($line);
  5935. }
  5936. close $fd
  5937. or print "ERROR $!\n";
  5938. print 'END';
  5939. if (defined $t0 && gitweb_check_feature('timed')) {
  5940. print ' '.
  5941. tv_interval($t0, [ gettimeofday() ]).
  5942. ' '.$number_of_git_cmds;
  5943. }
  5944. print "\n";
  5945. return;
  5946. }
  5947. # page header
  5948. git_header_html();
  5949. my $formats_nav =
  5950. $cgi->a({-href => href(action=>"blob", -replay=>1)},
  5951. "blob") .
  5952. " | ";
  5953. if ($format eq 'incremental') {
  5954. $formats_nav .=
  5955. $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
  5956. "blame") . " (non-incremental)";
  5957. } else {
  5958. $formats_nav .=
  5959. $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
  5960. "blame") . " (incremental)";
  5961. }
  5962. $formats_nav .=
  5963. " | " .
  5964. $cgi->a({-href => href(action=>"history", -replay=>1)},
  5965. "history") .
  5966. " | " .
  5967. $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
  5968. "HEAD");
  5969. git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
  5970. git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
  5971. git_print_page_path($file_name, $ftype, $hash_base);
  5972. # page body
  5973. if ($format eq 'incremental') {
  5974. print "<noscript>\n<div class=\"error\"><center><b>\n".
  5975. "This page requires JavaScript to run.\n Use ".
  5976. $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
  5977. 'this page').
  5978. " instead.\n".
  5979. "</b></center></div>\n</noscript>\n";
  5980. print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
  5981. }
  5982. print qq!<div class="page_body">\n!;
  5983. print qq!<div id="progress_info">... / ...</div>\n!
  5984. if ($format eq 'incremental');
  5985. print qq!<table id="blame_table" class="blame" width="100%">\n!.
  5986. #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
  5987. qq!<thead>\n!.
  5988. qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
  5989. qq!</thead>\n!.
  5990. qq!<tbody>\n!;
  5991. my @rev_color = qw(light dark);
  5992. my $num_colors = scalar(@rev_color);
  5993. my $current_color = 0;
  5994. if ($format eq 'incremental') {
  5995. my $color_class = $rev_color[$current_color];
  5996. #contents of a file
  5997. my $linenr = 0;
  5998. LINE:
  5999. while (my $line = <$fd>) {
  6000. chomp $line;
  6001. $linenr++;
  6002. print qq!<tr id="l$linenr" class="$color_class">!.
  6003. qq!<td class="sha1"><a href=""> </a></td>!.
  6004. qq!<td class="linenr">!.
  6005. qq!<a class="linenr" href="">$linenr</a></td>!;
  6006. print qq!<td class="pre">! . esc_html($line) . "</td>\n";
  6007. print qq!</tr>\n!;
  6008. }
  6009. } else { # porcelain, i.e. ordinary blame
  6010. my %metainfo = (); # saves information about commits
  6011. # blame data
  6012. LINE:
  6013. while (my $line = <$fd>) {
  6014. chomp $line;
  6015. # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
  6016. # no <lines in group> for subsequent lines in group of lines
  6017. my ($full_rev, $orig_lineno, $lineno, $group_size) =
  6018. ($line =~ /^($oid_regex) (\d+) (\d+)(?: (\d+))?$/);
  6019. if (!exists $metainfo{$full_rev}) {
  6020. $metainfo{$full_rev} = { 'nprevious' => 0 };
  6021. }
  6022. my $meta = $metainfo{$full_rev};
  6023. my $data;
  6024. while ($data = <$fd>) {
  6025. chomp $data;
  6026. last if ($data =~ s/^\t//); # contents of line
  6027. if ($data =~ /^(\S+)(?: (.*))?$/) {
  6028. $meta->{$1} = $2 unless exists $meta->{$1};
  6029. }
  6030. if ($data =~ /^previous /) {
  6031. $meta->{'nprevious'}++;
  6032. }
  6033. }
  6034. my $short_rev = substr($full_rev, 0, 8);
  6035. my $author = $meta->{'author'};
  6036. my %date =
  6037. parse_date($meta->{'author-time'}, $meta->{'author-tz'});
  6038. my $date = $date{'iso-tz'};
  6039. if ($group_size) {
  6040. $current_color = ($current_color + 1) % $num_colors;
  6041. }
  6042. my $tr_class = $rev_color[$current_color];
  6043. $tr_class .= ' boundary' if (exists $meta->{'boundary'});
  6044. $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
  6045. $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
  6046. print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
  6047. if ($group_size) {
  6048. print "<td class=\"sha1\"";
  6049. print " title=\"". esc_html($author) . ", $date\"";
  6050. print " rowspan=\"$group_size\"" if ($group_size > 1);
  6051. print ">";
  6052. print $cgi->a({-href => href(action=>"commit",
  6053. hash=>$full_rev,
  6054. file_name=>$file_name)},
  6055. esc_html($short_rev));
  6056. if ($group_size >= 2) {
  6057. my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
  6058. if (@author_initials) {
  6059. print "<br />" .
  6060. esc_html(join('', @author_initials));
  6061. # or join('.', ...)
  6062. }
  6063. }
  6064. print "</td>\n";
  6065. }
  6066. # 'previous' <sha1 of parent commit> <filename at commit>
  6067. if (exists $meta->{'previous'} &&
  6068. $meta->{'previous'} =~ /^($oid_regex) (.*)$/) {
  6069. $meta->{'parent'} = $1;
  6070. $meta->{'file_parent'} = unquote($2);
  6071. }
  6072. my $linenr_commit =
  6073. exists($meta->{'parent'}) ?
  6074. $meta->{'parent'} : $full_rev;
  6075. my $linenr_filename =
  6076. exists($meta->{'file_parent'}) ?
  6077. $meta->{'file_parent'} : unquote($meta->{'filename'});
  6078. my $blamed = href(action => 'blame',
  6079. file_name => $linenr_filename,
  6080. hash_base => $linenr_commit);
  6081. print "<td class=\"linenr\">";
  6082. print $cgi->a({ -href => "$blamed#l$orig_lineno",
  6083. -class => "linenr" },
  6084. esc_html($lineno));
  6085. print "</td>";
  6086. print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
  6087. print "</tr>\n";
  6088. } # end while
  6089. }
  6090. # footer
  6091. print "</tbody>\n".
  6092. "</table>\n"; # class="blame"
  6093. print "</div>\n"; # class="blame_body"
  6094. close $fd
  6095. or print "Reading blob failed\n";
  6096. git_footer_html();
  6097. }
  6098. sub git_blame {
  6099. git_blame_common();
  6100. }
  6101. sub git_blame_incremental {
  6102. git_blame_common('incremental');
  6103. }
  6104. sub git_blame_data {
  6105. git_blame_common('data');
  6106. }
  6107. sub git_tags {
  6108. my $head = git_get_head_hash($project);
  6109. git_header_html();
  6110. git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
  6111. git_print_header_div('summary', $project);
  6112. my @tagslist = git_get_tags_list();
  6113. if (@tagslist) {
  6114. git_tags_body(\@tagslist);
  6115. }
  6116. git_footer_html();
  6117. }
  6118. sub git_heads {
  6119. my $head = git_get_head_hash($project);
  6120. git_header_html();
  6121. git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
  6122. git_print_header_div('summary', $project);
  6123. my @headslist = git_get_heads_list();
  6124. if (@headslist) {
  6125. git_heads_body(\@headslist, $head);
  6126. }
  6127. git_footer_html();
  6128. }
  6129. # used both for single remote view and for list of all the remotes
  6130. sub git_remotes {
  6131. gitweb_check_feature('remote_heads')
  6132. or die_error(403, "Remote heads view is disabled");
  6133. my $head = git_get_head_hash($project);
  6134. my $remote = $input_params{'hash'};
  6135. my $remotedata = git_get_remotes_list($remote);
  6136. die_error(500, "Unable to get remote information") unless defined $remotedata;
  6137. unless (%$remotedata) {
  6138. die_error(404, defined $remote ?
  6139. "Remote $remote not found" :
  6140. "No remotes found");
  6141. }
  6142. git_header_html(undef, undef, -action_extra => $remote);
  6143. git_print_page_nav('', '', $head, undef, $head,
  6144. format_ref_views($remote ? '' : 'remotes'));
  6145. fill_remote_heads($remotedata);
  6146. if (defined $remote) {
  6147. git_print_header_div('remotes', "$remote remote for $project");
  6148. git_remote_block($remote, $remotedata->{$remote}, undef, $head);
  6149. } else {
  6150. git_print_header_div('summary', "$project remotes");
  6151. git_remotes_body($remotedata, undef, $head);
  6152. }
  6153. git_footer_html();
  6154. }
  6155. sub git_blob_plain {
  6156. my $type = shift;
  6157. my $expires;
  6158. if (!defined $hash) {
  6159. if (defined $file_name) {
  6160. my $base = $hash_base || git_get_head_hash($project);
  6161. $hash = git_get_hash_by_path($base, $file_name, "blob")
  6162. or die_error(404, "Cannot find file");
  6163. } else {
  6164. die_error(400, "No file name defined");
  6165. }
  6166. } elsif ($hash =~ m/^$oid_regex$/) {
  6167. # blobs defined by non-textual hash id's can be cached
  6168. $expires = "+1d";
  6169. }
  6170. open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
  6171. or die_error(500, "Open git-cat-file blob '$hash' failed");
  6172. # content-type (can include charset)
  6173. $type = blob_contenttype($fd, $file_name, $type);
  6174. # "save as" filename, even when no $file_name is given
  6175. my $save_as = "$hash";
  6176. if (defined $file_name) {
  6177. $save_as = $file_name;
  6178. } elsif ($type =~ m/^text\//) {
  6179. $save_as .= '.txt';
  6180. }
  6181. # With XSS prevention on, blobs of all types except a few known safe
  6182. # ones are served with "Content-Disposition: attachment" to make sure
  6183. # they don't run in our security domain. For certain image types,
  6184. # blob view writes an <img> tag referring to blob_plain view, and we
  6185. # want to be sure not to break that by serving the image as an
  6186. # attachment (though Firefox 3 doesn't seem to care).
  6187. my $sandbox = $prevent_xss &&
  6188. $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
  6189. # serve text/* as text/plain
  6190. if ($prevent_xss &&
  6191. ($type =~ m!^text/[a-z]+\b(.*)$! ||
  6192. ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
  6193. my $rest = $1;
  6194. $rest = defined $rest ? $rest : '';
  6195. $type = "text/plain$rest";
  6196. }
  6197. print $cgi->header(
  6198. -type => $type,
  6199. -expires => $expires,
  6200. -content_disposition =>
  6201. ($sandbox ? 'attachment' : 'inline')
  6202. . '; filename="' . $save_as . '"');
  6203. local $/ = undef;
  6204. local *FCGI::Stream::PRINT = $FCGI_Stream_PRINT_raw;
  6205. binmode STDOUT, ':raw';
  6206. print <$fd>;
  6207. binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
  6208. close $fd;
  6209. }
  6210. sub git_blob {
  6211. my $expires;
  6212. if (!defined $hash) {
  6213. if (defined $file_name) {
  6214. my $base = $hash_base || git_get_head_hash($project);
  6215. $hash = git_get_hash_by_path($base, $file_name, "blob")
  6216. or die_error(404, "Cannot find file");
  6217. } else {
  6218. die_error(400, "No file name defined");
  6219. }
  6220. } elsif ($hash =~ m/^$oid_regex$/) {
  6221. # blobs defined by non-textual hash id's can be cached
  6222. $expires = "+1d";
  6223. }
  6224. my $have_blame = gitweb_check_feature('blame');
  6225. open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
  6226. or die_error(500, "Couldn't cat $file_name, $hash");
  6227. my $mimetype = blob_mimetype($fd, $file_name);
  6228. # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
  6229. if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
  6230. close $fd;
  6231. return git_blob_plain($mimetype);
  6232. }
  6233. # we can have blame only for text/* mimetype
  6234. $have_blame &&= ($mimetype =~ m!^text/!);
  6235. my $highlight = gitweb_check_feature('highlight');
  6236. my $syntax = guess_file_syntax($highlight, $file_name);
  6237. $fd = run_highlighter($fd, $highlight, $syntax);
  6238. git_header_html(undef, $expires);
  6239. my $formats_nav = '';
  6240. if (defined $hash_base && (my %co = parse_commit($hash_base))) {
  6241. if (defined $file_name) {
  6242. if ($have_blame) {
  6243. $formats_nav .=
  6244. $cgi->a({-href => href(action=>"blame", -replay=>1)},
  6245. "blame") .
  6246. " | ";
  6247. }
  6248. $formats_nav .=
  6249. $cgi->a({-href => href(action=>"history", -replay=>1)},
  6250. "history") .
  6251. " | " .
  6252. $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
  6253. "raw") .
  6254. " | " .
  6255. $cgi->a({-href => href(action=>"blob",
  6256. hash_base=>"HEAD", file_name=>$file_name)},
  6257. "HEAD");
  6258. } else {
  6259. $formats_nav .=
  6260. $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
  6261. "raw");
  6262. }
  6263. git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
  6264. git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
  6265. } else {
  6266. print "<div class=\"page_nav\">\n" .
  6267. "<br/><br/></div>\n" .
  6268. "<div class=\"title\">".esc_html($hash)."</div>\n";
  6269. }
  6270. git_print_page_path($file_name, "blob", $hash_base);
  6271. print "<div class=\"page_body\">\n";
  6272. if ($mimetype =~ m!^image/!) {
  6273. print qq!<img class="blob" type="!.esc_attr($mimetype).qq!"!;
  6274. if ($file_name) {
  6275. print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
  6276. }
  6277. print qq! src="! .
  6278. esc_attr(href(action=>"blob_plain", hash=>$hash,
  6279. hash_base=>$hash_base, file_name=>$file_name)) .
  6280. qq!" />\n!;
  6281. } else {
  6282. my $nr;
  6283. while (my $line = <$fd>) {
  6284. chomp $line;
  6285. $nr++;
  6286. $line = untabify($line);
  6287. printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
  6288. $nr, esc_attr(href(-replay => 1)), $nr, $nr,
  6289. $highlight ? sanitize($line) : esc_html($line, -nbsp=>1);
  6290. }
  6291. }
  6292. close $fd
  6293. or print "Reading blob failed.\n";
  6294. print "</div>";
  6295. git_footer_html();
  6296. }
  6297. sub git_tree {
  6298. if (!defined $hash_base) {
  6299. $hash_base = "HEAD";
  6300. }
  6301. if (!defined $hash) {
  6302. if (defined $file_name) {
  6303. $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
  6304. } else {
  6305. $hash = $hash_base;
  6306. }
  6307. }
  6308. die_error(404, "No such tree") unless defined($hash);
  6309. my $show_sizes = gitweb_check_feature('show-sizes');
  6310. my $have_blame = gitweb_check_feature('blame');
  6311. my @entries = ();
  6312. {
  6313. local $/ = "\0";
  6314. open my $fd, "-|", git_cmd(), "ls-tree", '-z',
  6315. ($show_sizes ? '-l' : ()), @extra_options, $hash
  6316. or die_error(500, "Open git-ls-tree failed");
  6317. @entries = map { chomp; $_ } <$fd>;
  6318. close $fd
  6319. or die_error(404, "Reading tree failed");
  6320. }
  6321. my $refs = git_get_references();
  6322. my $ref = format_ref_marker($refs, $hash_base);
  6323. git_header_html();
  6324. my $basedir = '';
  6325. if (defined $hash_base && (my %co = parse_commit($hash_base))) {
  6326. my @views_nav = ();
  6327. if (defined $file_name) {
  6328. push @views_nav,
  6329. $cgi->a({-href => href(action=>"history", -replay=>1)},
  6330. "history"),
  6331. $cgi->a({-href => href(action=>"tree",
  6332. hash_base=>"HEAD", file_name=>$file_name)},
  6333. "HEAD"),
  6334. }
  6335. my $snapshot_links = format_snapshot_links($hash);
  6336. if (defined $snapshot_links) {
  6337. # FIXME: Should be available when we have no hash base as well.
  6338. push @views_nav, $snapshot_links;
  6339. }
  6340. git_print_page_nav('tree','', $hash_base, undef, undef,
  6341. join(' | ', @views_nav));
  6342. git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
  6343. } else {
  6344. undef $hash_base;
  6345. print "<div class=\"page_nav\">\n";
  6346. print "<br/><br/></div>\n";
  6347. print "<div class=\"title\">".esc_html($hash)."</div>\n";
  6348. }
  6349. if (defined $file_name) {
  6350. $basedir = $file_name;
  6351. if ($basedir ne '' && substr($basedir, -1) ne '/') {
  6352. $basedir .= '/';
  6353. }
  6354. git_print_page_path($file_name, 'tree', $hash_base);
  6355. }
  6356. print "<div class=\"page_body\">\n";
  6357. print "<table class=\"tree\">\n";
  6358. my $alternate = 1;
  6359. # '..' (top directory) link if possible
  6360. if (defined $hash_base &&
  6361. defined $file_name && $file_name =~ m![^/]+$!) {
  6362. if ($alternate) {
  6363. print "<tr class=\"dark\">\n";
  6364. } else {
  6365. print "<tr class=\"light\">\n";
  6366. }
  6367. $alternate ^= 1;
  6368. my $up = $file_name;
  6369. $up =~ s!/?[^/]+$!!;
  6370. undef $up unless $up;
  6371. # based on git_print_tree_entry
  6372. print '<td class="mode">' . mode_str('040000') . "</td>\n";
  6373. print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
  6374. print '<td class="list">';
  6375. print $cgi->a({-href => href(action=>"tree",
  6376. hash_base=>$hash_base,
  6377. file_name=>$up)},
  6378. "..");
  6379. print "</td>\n";
  6380. print "<td class=\"link\"></td>\n";
  6381. print "</tr>\n";
  6382. }
  6383. foreach my $line (@entries) {
  6384. my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
  6385. if ($alternate) {
  6386. print "<tr class=\"dark\">\n";
  6387. } else {
  6388. print "<tr class=\"light\">\n";
  6389. }
  6390. $alternate ^= 1;
  6391. git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
  6392. print "</tr>\n";
  6393. }
  6394. print "</table>\n" .
  6395. "</div>";
  6396. git_footer_html();
  6397. }
  6398. sub sanitize_for_filename {
  6399. my $name = shift;
  6400. $name =~ s!/!-!g;
  6401. $name =~ s/[^[:alnum:]_.-]//g;
  6402. return $name;
  6403. }
  6404. sub snapshot_name {
  6405. my ($project, $hash) = @_;
  6406. # path/to/project.git -> project
  6407. # path/to/project/.git -> project
  6408. my $name = to_utf8($project);
  6409. $name =~ s,([^/])/*\.git$,$1,;
  6410. $name = sanitize_for_filename(basename($name));
  6411. my $ver = $hash;
  6412. if ($hash =~ /^[0-9a-fA-F]+$/) {
  6413. # shorten SHA-1 hash
  6414. my $full_hash = git_get_full_hash($project, $hash);
  6415. if ($full_hash =~ /^$hash/ && length($hash) > 7) {
  6416. $ver = git_get_short_hash($project, $hash);
  6417. }
  6418. } elsif ($hash =~ m!^refs/tags/(.*)$!) {
  6419. # tags don't need shortened SHA-1 hash
  6420. $ver = $1;
  6421. } else {
  6422. # branches and other need shortened SHA-1 hash
  6423. my $strip_refs = join '|', map { quotemeta } get_branch_refs();
  6424. if ($hash =~ m!^refs/($strip_refs|remotes)/(.*)$!) {
  6425. my $ref_dir = (defined $1) ? $1 : '';
  6426. $ver = $2;
  6427. $ref_dir = sanitize_for_filename($ref_dir);
  6428. # for refs neither in heads nor remotes we want to
  6429. # add a ref dir to archive name
  6430. if ($ref_dir ne '' and $ref_dir ne 'heads' and $ref_dir ne 'remotes') {
  6431. $ver = $ref_dir . '-' . $ver;
  6432. }
  6433. }
  6434. $ver .= '-' . git_get_short_hash($project, $hash);
  6435. }
  6436. # special case of sanitization for filename - we change
  6437. # slashes to dots instead of dashes
  6438. # in case of hierarchical branch names
  6439. $ver =~ s!/!.!g;
  6440. $ver =~ s/[^[:alnum:]_.-]//g;
  6441. # name = project-version_string
  6442. $name = "$name-$ver";
  6443. return wantarray ? ($name, $name) : $name;
  6444. }
  6445. sub exit_if_unmodified_since {
  6446. my ($latest_epoch) = @_;
  6447. our $cgi;
  6448. my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
  6449. if (defined $if_modified) {
  6450. my $since;
  6451. if (eval { require HTTP::Date; 1; }) {
  6452. $since = HTTP::Date::str2time($if_modified);
  6453. } elsif (eval { require Time::ParseDate; 1; }) {
  6454. $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
  6455. }
  6456. if (defined $since && $latest_epoch <= $since) {
  6457. my %latest_date = parse_date($latest_epoch);
  6458. print $cgi->header(
  6459. -last_modified => $latest_date{'rfc2822'},
  6460. -status => '304 Not Modified');
  6461. goto DONE_GITWEB;
  6462. }
  6463. }
  6464. }
  6465. sub git_snapshot {
  6466. my $format = $input_params{'snapshot_format'};
  6467. if (!@snapshot_fmts) {
  6468. die_error(403, "Snapshots not allowed");
  6469. }
  6470. # default to first supported snapshot format
  6471. $format ||= $snapshot_fmts[0];
  6472. if ($format !~ m/^[a-z0-9]+$/) {
  6473. die_error(400, "Invalid snapshot format parameter");
  6474. } elsif (!exists($known_snapshot_formats{$format})) {
  6475. die_error(400, "Unknown snapshot format");
  6476. } elsif ($known_snapshot_formats{$format}{'disabled'}) {
  6477. die_error(403, "Snapshot format not allowed");
  6478. } elsif (!grep($_ eq $format, @snapshot_fmts)) {
  6479. die_error(403, "Unsupported snapshot format");
  6480. }
  6481. my $type = git_get_type("$hash^{}");
  6482. if (!$type) {
  6483. die_error(404, 'Object does not exist');
  6484. } elsif ($type eq 'blob') {
  6485. die_error(400, 'Object is not a tree-ish');
  6486. }
  6487. my ($name, $prefix) = snapshot_name($project, $hash);
  6488. my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
  6489. my %co = parse_commit($hash);
  6490. exit_if_unmodified_since($co{'committer_epoch'}) if %co;
  6491. my $cmd = quote_command(
  6492. git_cmd(), 'archive',
  6493. "--format=$known_snapshot_formats{$format}{'format'}",
  6494. "--prefix=$prefix/", $hash);
  6495. if (exists $known_snapshot_formats{$format}{'compressor'}) {
  6496. $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
  6497. }
  6498. $filename =~ s/(["\\])/\\$1/g;
  6499. my %latest_date;
  6500. if (%co) {
  6501. %latest_date = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
  6502. }
  6503. print $cgi->header(
  6504. -type => $known_snapshot_formats{$format}{'type'},
  6505. -content_disposition => 'inline; filename="' . $filename . '"',
  6506. %co ? (-last_modified => $latest_date{'rfc2822'}) : (),
  6507. -status => '200 OK');
  6508. open my $fd, "-|", $cmd
  6509. or die_error(500, "Execute git-archive failed");
  6510. local *FCGI::Stream::PRINT = $FCGI_Stream_PRINT_raw;
  6511. binmode STDOUT, ':raw';
  6512. print <$fd>;
  6513. binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
  6514. close $fd;
  6515. }
  6516. sub git_log_generic {
  6517. my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
  6518. my $head = git_get_head_hash($project);
  6519. if (!defined $base) {
  6520. $base = $head;
  6521. }
  6522. if (!defined $page) {
  6523. $page = 0;
  6524. }
  6525. my $refs = git_get_references();
  6526. my $commit_hash = $base;
  6527. if (defined $parent) {
  6528. $commit_hash = "$parent..$base";
  6529. }
  6530. my @commitlist =
  6531. parse_commits($commit_hash, 101, (100 * $page),
  6532. defined $file_name ? ($file_name, "--full-history") : ());
  6533. my $ftype;
  6534. if (!defined $file_hash && defined $file_name) {
  6535. # some commits could have deleted file in question,
  6536. # and not have it in tree, but one of them has to have it
  6537. for (my $i = 0; $i < @commitlist; $i++) {
  6538. $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
  6539. last if defined $file_hash;
  6540. }
  6541. }
  6542. if (defined $file_hash) {
  6543. $ftype = git_get_type($file_hash);
  6544. }
  6545. if (defined $file_name && !defined $ftype) {
  6546. die_error(500, "Unknown type of object");
  6547. }
  6548. my %co;
  6549. if (defined $file_name) {
  6550. %co = parse_commit($base)
  6551. or die_error(404, "Unknown commit object");
  6552. }
  6553. my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
  6554. my $next_link = '';
  6555. if ($#commitlist >= 100) {
  6556. $next_link =
  6557. $cgi->a({-href => href(-replay=>1, page=>$page+1),
  6558. -accesskey => "n", -title => "Alt-n"}, "next");
  6559. }
  6560. my $patch_max = gitweb_get_feature('patches');
  6561. if ($patch_max && !defined $file_name) {
  6562. if ($patch_max < 0 || @commitlist <= $patch_max) {
  6563. $paging_nav .= " &sdot; " .
  6564. $cgi->a({-href => href(action=>"patches", -replay=>1)},
  6565. "patches");
  6566. }
  6567. }
  6568. git_header_html();
  6569. git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
  6570. if (defined $file_name) {
  6571. git_print_header_div('commit', esc_html($co{'title'}), $base);
  6572. } else {
  6573. git_print_header_div('summary', $project)
  6574. }
  6575. git_print_page_path($file_name, $ftype, $hash_base)
  6576. if (defined $file_name);
  6577. $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
  6578. $file_name, $file_hash, $ftype);
  6579. git_footer_html();
  6580. }
  6581. sub git_log {
  6582. git_log_generic('log', \&git_log_body,
  6583. $hash, $hash_parent);
  6584. }
  6585. sub git_commit {
  6586. $hash ||= $hash_base || "HEAD";
  6587. my %co = parse_commit($hash)
  6588. or die_error(404, "Unknown commit object");
  6589. my $parent = $co{'parent'};
  6590. my $parents = $co{'parents'}; # listref
  6591. # we need to prepare $formats_nav before any parameter munging
  6592. my $formats_nav;
  6593. if (!defined $parent) {
  6594. # --root commitdiff
  6595. $formats_nav .= '(initial)';
  6596. } elsif (@$parents == 1) {
  6597. # single parent commit
  6598. $formats_nav .=
  6599. '(parent: ' .
  6600. $cgi->a({-href => href(action=>"commit",
  6601. hash=>$parent)},
  6602. esc_html(substr($parent, 0, 7))) .
  6603. ')';
  6604. } else {
  6605. # merge commit
  6606. $formats_nav .=
  6607. '(merge: ' .
  6608. join(' ', map {
  6609. $cgi->a({-href => href(action=>"commit",
  6610. hash=>$_)},
  6611. esc_html(substr($_, 0, 7)));
  6612. } @$parents ) .
  6613. ')';
  6614. }
  6615. if (gitweb_check_feature('patches') && @$parents <= 1) {
  6616. $formats_nav .= " | " .
  6617. $cgi->a({-href => href(action=>"patch", -replay=>1)},
  6618. "patch");
  6619. }
  6620. if (!defined $parent) {
  6621. $parent = "--root";
  6622. }
  6623. my @difftree;
  6624. open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
  6625. @diff_opts,
  6626. (@$parents <= 1 ? $parent : '-c'),
  6627. $hash, "--"
  6628. or die_error(500, "Open git-diff-tree failed");
  6629. @difftree = map { chomp; $_ } <$fd>;
  6630. close $fd or die_error(404, "Reading git-diff-tree failed");
  6631. # non-textual hash id's can be cached
  6632. my $expires;
  6633. if ($hash =~ m/^$oid_regex$/) {
  6634. $expires = "+1d";
  6635. }
  6636. my $refs = git_get_references();
  6637. my $ref = format_ref_marker($refs, $co{'id'});
  6638. git_header_html(undef, $expires);
  6639. git_print_page_nav('commit', '',
  6640. $hash, $co{'tree'}, $hash,
  6641. $formats_nav);
  6642. if (defined $co{'parent'}) {
  6643. git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
  6644. } else {
  6645. git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
  6646. }
  6647. print "<div class=\"title_text\">\n" .
  6648. "<table class=\"object_header\">\n";
  6649. git_print_authorship_rows(\%co);
  6650. print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
  6651. print "<tr>" .
  6652. "<td>tree</td>" .
  6653. "<td class=\"sha1\">" .
  6654. $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
  6655. class => "list"}, $co{'tree'}) .
  6656. "</td>" .
  6657. "<td class=\"link\">" .
  6658. $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
  6659. "tree");
  6660. my $snapshot_links = format_snapshot_links($hash);
  6661. if (defined $snapshot_links) {
  6662. print " | " . $snapshot_links;
  6663. }
  6664. print "</td>" .
  6665. "</tr>\n";
  6666. foreach my $par (@$parents) {
  6667. print "<tr>" .
  6668. "<td>parent</td>" .
  6669. "<td class=\"sha1\">" .
  6670. $cgi->a({-href => href(action=>"commit", hash=>$par),
  6671. class => "list"}, $par) .
  6672. "</td>" .
  6673. "<td class=\"link\">" .
  6674. $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
  6675. " | " .
  6676. $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
  6677. "</td>" .
  6678. "</tr>\n";
  6679. }
  6680. print "</table>".
  6681. "</div>\n";
  6682. print "<div class=\"page_body\">\n";
  6683. git_print_log($co{'comment'});
  6684. print "</div>\n";
  6685. git_difftree_body(\@difftree, $hash, @$parents);
  6686. git_footer_html();
  6687. }
  6688. sub git_object {
  6689. # object is defined by:
  6690. # - hash or hash_base alone
  6691. # - hash_base and file_name
  6692. my $type;
  6693. # - hash or hash_base alone
  6694. if ($hash || ($hash_base && !defined $file_name)) {
  6695. my $object_id = $hash || $hash_base;
  6696. open my $fd, "-|", quote_command(
  6697. git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
  6698. or die_error(404, "Object does not exist");
  6699. $type = <$fd>;
  6700. defined $type && chomp $type;
  6701. close $fd
  6702. or die_error(404, "Object does not exist");
  6703. # - hash_base and file_name
  6704. } elsif ($hash_base && defined $file_name) {
  6705. $file_name =~ s,/+$,,;
  6706. system(git_cmd(), "cat-file", '-e', $hash_base) == 0
  6707. or die_error(404, "Base object does not exist");
  6708. # here errors should not happen
  6709. open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
  6710. or die_error(500, "Open git-ls-tree failed");
  6711. my $line = <$fd>;
  6712. close $fd;
  6713. #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
  6714. unless ($line && $line =~ m/^([0-9]+) (.+) ($oid_regex)\t/) {
  6715. die_error(404, "File or directory for given base does not exist");
  6716. }
  6717. $type = $2;
  6718. $hash = $3;
  6719. } else {
  6720. die_error(400, "Not enough information to find object");
  6721. }
  6722. print $cgi->redirect(-uri => href(action=>$type, -full=>1,
  6723. hash=>$hash, hash_base=>$hash_base,
  6724. file_name=>$file_name),
  6725. -status => '302 Found');
  6726. }
  6727. sub git_blobdiff {
  6728. my $format = shift || 'html';
  6729. my $diff_style = $input_params{'diff_style'} || 'inline';
  6730. my $fd;
  6731. my @difftree;
  6732. my %diffinfo;
  6733. my $expires;
  6734. # preparing $fd and %diffinfo for git_patchset_body
  6735. # new style URI
  6736. if (defined $hash_base && defined $hash_parent_base) {
  6737. if (defined $file_name) {
  6738. # read raw output
  6739. open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
  6740. $hash_parent_base, $hash_base,
  6741. "--", (defined $file_parent ? $file_parent : ()), $file_name
  6742. or die_error(500, "Open git-diff-tree failed");
  6743. @difftree = map { chomp; $_ } <$fd>;
  6744. close $fd
  6745. or die_error(404, "Reading git-diff-tree failed");
  6746. @difftree
  6747. or die_error(404, "Blob diff not found");
  6748. } elsif (defined $hash &&
  6749. $hash =~ $oid_regex) {
  6750. # try to find filename from $hash
  6751. # read filtered raw output
  6752. open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
  6753. $hash_parent_base, $hash_base, "--"
  6754. or die_error(500, "Open git-diff-tree failed");
  6755. @difftree =
  6756. # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
  6757. # $hash == to_id
  6758. grep { /^:[0-7]{6} [0-7]{6} $oid_regex $hash/ }
  6759. map { chomp; $_ } <$fd>;
  6760. close $fd
  6761. or die_error(404, "Reading git-diff-tree failed");
  6762. @difftree
  6763. or die_error(404, "Blob diff not found");
  6764. } else {
  6765. die_error(400, "Missing one of the blob diff parameters");
  6766. }
  6767. if (@difftree > 1) {
  6768. die_error(400, "Ambiguous blob diff specification");
  6769. }
  6770. %diffinfo = parse_difftree_raw_line($difftree[0]);
  6771. $file_parent ||= $diffinfo{'from_file'} || $file_name;
  6772. $file_name ||= $diffinfo{'to_file'};
  6773. $hash_parent ||= $diffinfo{'from_id'};
  6774. $hash ||= $diffinfo{'to_id'};
  6775. # non-textual hash id's can be cached
  6776. if ($hash_base =~ m/^$oid_regex$/ &&
  6777. $hash_parent_base =~ m/^$oid_regex$/) {
  6778. $expires = '+1d';
  6779. }
  6780. # open patch output
  6781. open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
  6782. '-p', ($format eq 'html' ? "--full-index" : ()),
  6783. $hash_parent_base, $hash_base,
  6784. "--", (defined $file_parent ? $file_parent : ()), $file_name
  6785. or die_error(500, "Open git-diff-tree failed");
  6786. }
  6787. # old/legacy style URI -- not generated anymore since 1.4.3.
  6788. if (!%diffinfo) {
  6789. die_error('404 Not Found', "Missing one of the blob diff parameters")
  6790. }
  6791. # header
  6792. if ($format eq 'html') {
  6793. my $formats_nav =
  6794. $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
  6795. "raw");
  6796. $formats_nav .= diff_style_nav($diff_style);
  6797. git_header_html(undef, $expires);
  6798. if (defined $hash_base && (my %co = parse_commit($hash_base))) {
  6799. git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
  6800. git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
  6801. } else {
  6802. print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
  6803. print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
  6804. }
  6805. if (defined $file_name) {
  6806. git_print_page_path($file_name, "blob", $hash_base);
  6807. } else {
  6808. print "<div class=\"page_path\"></div>\n";
  6809. }
  6810. } elsif ($format eq 'plain') {
  6811. print $cgi->header(
  6812. -type => 'text/plain',
  6813. -charset => 'utf-8',
  6814. -expires => $expires,
  6815. -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
  6816. print "X-Git-Url: " . $cgi->self_url() . "\n\n";
  6817. } else {
  6818. die_error(400, "Unknown blobdiff format");
  6819. }
  6820. # patch
  6821. if ($format eq 'html') {
  6822. print "<div class=\"page_body\">\n";
  6823. git_patchset_body($fd, $diff_style,
  6824. [ \%diffinfo ], $hash_base, $hash_parent_base);
  6825. close $fd;
  6826. print "</div>\n"; # class="page_body"
  6827. git_footer_html();
  6828. } else {
  6829. while (my $line = <$fd>) {
  6830. $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
  6831. $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
  6832. print $line;
  6833. last if $line =~ m!^\+\+\+!;
  6834. }
  6835. local $/ = undef;
  6836. print <$fd>;
  6837. close $fd;
  6838. }
  6839. }
  6840. sub git_blobdiff_plain {
  6841. git_blobdiff('plain');
  6842. }
  6843. # assumes that it is added as later part of already existing navigation,
  6844. # so it returns "| foo | bar" rather than just "foo | bar"
  6845. sub diff_style_nav {
  6846. my ($diff_style, $is_combined) = @_;
  6847. $diff_style ||= 'inline';
  6848. return "" if ($is_combined);
  6849. my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
  6850. my %styles = @styles;
  6851. @styles =
  6852. @styles[ map { $_ * 2 } 0..$#styles/2 ];
  6853. return join '',
  6854. map { " | ".$_ }
  6855. map {
  6856. $_ eq $diff_style ? $styles{$_} :
  6857. $cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
  6858. } @styles;
  6859. }
  6860. sub git_commitdiff {
  6861. my %params = @_;
  6862. my $format = $params{-format} || 'html';
  6863. my $diff_style = $input_params{'diff_style'} || 'inline';
  6864. my ($patch_max) = gitweb_get_feature('patches');
  6865. if ($format eq 'patch') {
  6866. die_error(403, "Patch view not allowed") unless $patch_max;
  6867. }
  6868. $hash ||= $hash_base || "HEAD";
  6869. my %co = parse_commit($hash)
  6870. or die_error(404, "Unknown commit object");
  6871. # choose format for commitdiff for merge
  6872. if (! defined $hash_parent && @{$co{'parents'}} > 1) {
  6873. $hash_parent = '--cc';
  6874. }
  6875. # we need to prepare $formats_nav before almost any parameter munging
  6876. my $formats_nav;
  6877. if ($format eq 'html') {
  6878. $formats_nav =
  6879. $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
  6880. "raw");
  6881. if ($patch_max && @{$co{'parents'}} <= 1) {
  6882. $formats_nav .= " | " .
  6883. $cgi->a({-href => href(action=>"patch", -replay=>1)},
  6884. "patch");
  6885. }
  6886. $formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
  6887. if (defined $hash_parent &&
  6888. $hash_parent ne '-c' && $hash_parent ne '--cc') {
  6889. # commitdiff with two commits given
  6890. my $hash_parent_short = $hash_parent;
  6891. if ($hash_parent =~ m/^$oid_regex$/) {
  6892. $hash_parent_short = substr($hash_parent, 0, 7);
  6893. }
  6894. $formats_nav .=
  6895. ' (from';
  6896. for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
  6897. if ($co{'parents'}[$i] eq $hash_parent) {
  6898. $formats_nav .= ' parent ' . ($i+1);
  6899. last;
  6900. }
  6901. }
  6902. $formats_nav .= ': ' .
  6903. $cgi->a({-href => href(-replay=>1,
  6904. hash=>$hash_parent, hash_base=>undef)},
  6905. esc_html($hash_parent_short)) .
  6906. ')';
  6907. } elsif (!$co{'parent'}) {
  6908. # --root commitdiff
  6909. $formats_nav .= ' (initial)';
  6910. } elsif (scalar @{$co{'parents'}} == 1) {
  6911. # single parent commit
  6912. $formats_nav .=
  6913. ' (parent: ' .
  6914. $cgi->a({-href => href(-replay=>1,
  6915. hash=>$co{'parent'}, hash_base=>undef)},
  6916. esc_html(substr($co{'parent'}, 0, 7))) .
  6917. ')';
  6918. } else {
  6919. # merge commit
  6920. if ($hash_parent eq '--cc') {
  6921. $formats_nav .= ' | ' .
  6922. $cgi->a({-href => href(-replay=>1,
  6923. hash=>$hash, hash_parent=>'-c')},
  6924. 'combined');
  6925. } else { # $hash_parent eq '-c'
  6926. $formats_nav .= ' | ' .
  6927. $cgi->a({-href => href(-replay=>1,
  6928. hash=>$hash, hash_parent=>'--cc')},
  6929. 'compact');
  6930. }
  6931. $formats_nav .=
  6932. ' (merge: ' .
  6933. join(' ', map {
  6934. $cgi->a({-href => href(-replay=>1,
  6935. hash=>$_, hash_base=>undef)},
  6936. esc_html(substr($_, 0, 7)));
  6937. } @{$co{'parents'}} ) .
  6938. ')';
  6939. }
  6940. }
  6941. my $hash_parent_param = $hash_parent;
  6942. if (!defined $hash_parent_param) {
  6943. # --cc for multiple parents, --root for parentless
  6944. $hash_parent_param =
  6945. @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
  6946. }
  6947. # read commitdiff
  6948. my $fd;
  6949. my @difftree;
  6950. if ($format eq 'html') {
  6951. open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
  6952. "--no-commit-id", "--patch-with-raw", "--full-index",
  6953. $hash_parent_param, $hash, "--"
  6954. or die_error(500, "Open git-diff-tree failed");
  6955. while (my $line = <$fd>) {
  6956. chomp $line;
  6957. # empty line ends raw part of diff-tree output
  6958. last unless $line;
  6959. push @difftree, scalar parse_difftree_raw_line($line);
  6960. }
  6961. } elsif ($format eq 'plain') {
  6962. open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
  6963. '-p', $hash_parent_param, $hash, "--"
  6964. or die_error(500, "Open git-diff-tree failed");
  6965. } elsif ($format eq 'patch') {
  6966. # For commit ranges, we limit the output to the number of
  6967. # patches specified in the 'patches' feature.
  6968. # For single commits, we limit the output to a single patch,
  6969. # diverging from the git-format-patch default.
  6970. my @commit_spec = ();
  6971. if ($hash_parent) {
  6972. if ($patch_max > 0) {
  6973. push @commit_spec, "-$patch_max";
  6974. }
  6975. push @commit_spec, '-n', "$hash_parent..$hash";
  6976. } else {
  6977. if ($params{-single}) {
  6978. push @commit_spec, '-1';
  6979. } else {
  6980. if ($patch_max > 0) {
  6981. push @commit_spec, "-$patch_max";
  6982. }
  6983. push @commit_spec, "-n";
  6984. }
  6985. push @commit_spec, '--root', $hash;
  6986. }
  6987. open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
  6988. '--encoding=utf8', '--stdout', @commit_spec
  6989. or die_error(500, "Open git-format-patch failed");
  6990. } else {
  6991. die_error(400, "Unknown commitdiff format");
  6992. }
  6993. # non-textual hash id's can be cached
  6994. my $expires;
  6995. if ($hash =~ m/^$oid_regex$/) {
  6996. $expires = "+1d";
  6997. }
  6998. # write commit message
  6999. if ($format eq 'html') {
  7000. my $refs = git_get_references();
  7001. my $ref = format_ref_marker($refs, $co{'id'});
  7002. git_header_html(undef, $expires);
  7003. git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
  7004. git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
  7005. print "<div class=\"title_text\">\n" .
  7006. "<table class=\"object_header\">\n";
  7007. git_print_authorship_rows(\%co);
  7008. print "</table>".
  7009. "</div>\n";
  7010. print "<div class=\"page_body\">\n";
  7011. if (@{$co{'comment'}} > 1) {
  7012. print "<div class=\"log\">\n";
  7013. git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
  7014. print "</div>\n"; # class="log"
  7015. }
  7016. } elsif ($format eq 'plain') {
  7017. my $refs = git_get_references("tags");
  7018. my $tagname = git_get_rev_name_tags($hash);
  7019. my $filename = basename($project) . "-$hash.patch";
  7020. print $cgi->header(
  7021. -type => 'text/plain',
  7022. -charset => 'utf-8',
  7023. -expires => $expires,
  7024. -content_disposition => 'inline; filename="' . "$filename" . '"');
  7025. my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
  7026. print "From: " . to_utf8($co{'author'}) . "\n";
  7027. print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
  7028. print "Subject: " . to_utf8($co{'title'}) . "\n";
  7029. print "X-Git-Tag: $tagname\n" if $tagname;
  7030. print "X-Git-Url: " . $cgi->self_url() . "\n\n";
  7031. foreach my $line (@{$co{'comment'}}) {
  7032. print to_utf8($line) . "\n";
  7033. }
  7034. print "---\n\n";
  7035. } elsif ($format eq 'patch') {
  7036. my $filename = basename($project) . "-$hash.patch";
  7037. print $cgi->header(
  7038. -type => 'text/plain',
  7039. -charset => 'utf-8',
  7040. -expires => $expires,
  7041. -content_disposition => 'inline; filename="' . "$filename" . '"');
  7042. }
  7043. # write patch
  7044. if ($format eq 'html') {
  7045. my $use_parents = !defined $hash_parent ||
  7046. $hash_parent eq '-c' || $hash_parent eq '--cc';
  7047. git_difftree_body(\@difftree, $hash,
  7048. $use_parents ? @{$co{'parents'}} : $hash_parent);
  7049. print "<br/>\n";
  7050. git_patchset_body($fd, $diff_style,
  7051. \@difftree, $hash,
  7052. $use_parents ? @{$co{'parents'}} : $hash_parent);
  7053. close $fd;
  7054. print "</div>\n"; # class="page_body"
  7055. git_footer_html();
  7056. } elsif ($format eq 'plain') {
  7057. local $/ = undef;
  7058. print <$fd>;
  7059. close $fd
  7060. or print "Reading git-diff-tree failed\n";
  7061. } elsif ($format eq 'patch') {
  7062. local $/ = undef;
  7063. print <$fd>;
  7064. close $fd
  7065. or print "Reading git-format-patch failed\n";
  7066. }
  7067. }
  7068. sub git_commitdiff_plain {
  7069. git_commitdiff(-format => 'plain');
  7070. }
  7071. # format-patch-style patches
  7072. sub git_patch {
  7073. git_commitdiff(-format => 'patch', -single => 1);
  7074. }
  7075. sub git_patches {
  7076. git_commitdiff(-format => 'patch');
  7077. }
  7078. sub git_history {
  7079. git_log_generic('history', \&git_history_body,
  7080. $hash_base, $hash_parent_base,
  7081. $file_name, $hash);
  7082. }
  7083. sub git_search {
  7084. $searchtype ||= 'commit';
  7085. # check if appropriate features are enabled
  7086. gitweb_check_feature('search')
  7087. or die_error(403, "Search is disabled");
  7088. if ($searchtype eq 'pickaxe') {
  7089. # pickaxe may take all resources of your box and run for several minutes
  7090. # with every query - so decide by yourself how public you make this feature
  7091. gitweb_check_feature('pickaxe')
  7092. or die_error(403, "Pickaxe search is disabled");
  7093. }
  7094. if ($searchtype eq 'grep') {
  7095. # grep search might be potentially CPU-intensive, too
  7096. gitweb_check_feature('grep')
  7097. or die_error(403, "Grep search is disabled");
  7098. }
  7099. if (!defined $searchtext) {
  7100. die_error(400, "Text field is empty");
  7101. }
  7102. if (!defined $hash) {
  7103. $hash = git_get_head_hash($project);
  7104. }
  7105. my %co = parse_commit($hash);
  7106. if (!%co) {
  7107. die_error(404, "Unknown commit object");
  7108. }
  7109. if (!defined $page) {
  7110. $page = 0;
  7111. }
  7112. if ($searchtype eq 'commit' ||
  7113. $searchtype eq 'author' ||
  7114. $searchtype eq 'committer') {
  7115. git_search_message(%co);
  7116. } elsif ($searchtype eq 'pickaxe') {
  7117. git_search_changes(%co);
  7118. } elsif ($searchtype eq 'grep') {
  7119. git_search_files(%co);
  7120. } else {
  7121. die_error(400, "Unknown search type");
  7122. }
  7123. }
  7124. sub git_search_help {
  7125. git_header_html();
  7126. git_print_page_nav('','', $hash,$hash,$hash);
  7127. print <<EOT;
  7128. <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
  7129. regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
  7130. the pattern entered is recognized as the POSIX extended
  7131. <a href="https://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
  7132. insensitive).</p>
  7133. <dl>
  7134. <dt><b>commit</b></dt>
  7135. <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
  7136. EOT
  7137. my $have_grep = gitweb_check_feature('grep');
  7138. if ($have_grep) {
  7139. print <<EOT;
  7140. <dt><b>grep</b></dt>
  7141. <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
  7142. a different one) are searched for the given pattern. On large trees, this search can take
  7143. a while and put some strain on the server, so please use it with some consideration. Note that
  7144. due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
  7145. case-sensitive.</dd>
  7146. EOT
  7147. }
  7148. print <<EOT;
  7149. <dt><b>author</b></dt>
  7150. <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
  7151. <dt><b>committer</b></dt>
  7152. <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
  7153. EOT
  7154. my $have_pickaxe = gitweb_check_feature('pickaxe');
  7155. if ($have_pickaxe) {
  7156. print <<EOT;
  7157. <dt><b>pickaxe</b></dt>
  7158. <dd>All commits that caused the string to appear or disappear from any file (changes that
  7159. added, removed or "modified" the string) will be listed. This search can take a while and
  7160. takes a lot of strain on the server, so please use it wisely. Note that since you may be
  7161. interested even in changes just changing the case as well, this search is case sensitive.</dd>
  7162. EOT
  7163. }
  7164. print "</dl>\n";
  7165. git_footer_html();
  7166. }
  7167. sub git_shortlog {
  7168. git_log_generic('shortlog', \&git_shortlog_body,
  7169. $hash, $hash_parent);
  7170. }
  7171. ## ......................................................................
  7172. ## feeds (RSS, Atom; OPML)
  7173. sub git_feed {
  7174. my $format = shift || 'atom';
  7175. my $have_blame = gitweb_check_feature('blame');
  7176. # Atom: http://www.atomenabled.org/developers/syndication/
  7177. # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
  7178. if ($format ne 'rss' && $format ne 'atom') {
  7179. die_error(400, "Unknown web feed format");
  7180. }
  7181. # log/feed of current (HEAD) branch, log of given branch, history of file/directory
  7182. my $head = $hash || 'HEAD';
  7183. my @commitlist = parse_commits($head, 150, 0, $file_name);
  7184. my %latest_commit;
  7185. my %latest_date;
  7186. my $content_type = "application/$format+xml";
  7187. if (defined $cgi->http('HTTP_ACCEPT') &&
  7188. $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
  7189. # browser (feed reader) prefers text/xml
  7190. $content_type = 'text/xml';
  7191. }
  7192. if (defined($commitlist[0])) {
  7193. %latest_commit = %{$commitlist[0]};
  7194. my $latest_epoch = $latest_commit{'committer_epoch'};
  7195. exit_if_unmodified_since($latest_epoch);
  7196. %latest_date = parse_date($latest_epoch, $latest_commit{'committer_tz'});
  7197. }
  7198. print $cgi->header(
  7199. -type => $content_type,
  7200. -charset => 'utf-8',
  7201. %latest_date ? (-last_modified => $latest_date{'rfc2822'}) : (),
  7202. -status => '200 OK');
  7203. # Optimization: skip generating the body if client asks only
  7204. # for Last-Modified date.
  7205. return if ($cgi->request_method() eq 'HEAD');
  7206. # header variables
  7207. my $title = "$site_name - $project/$action";
  7208. my $feed_type = 'log';
  7209. if (defined $hash) {
  7210. $title .= " - '$hash'";
  7211. $feed_type = 'branch log';
  7212. if (defined $file_name) {
  7213. $title .= " :: $file_name";
  7214. $feed_type = 'history';
  7215. }
  7216. } elsif (defined $file_name) {
  7217. $title .= " - $file_name";
  7218. $feed_type = 'history';
  7219. }
  7220. $title .= " $feed_type";
  7221. $title = esc_html($title);
  7222. my $descr = git_get_project_description($project);
  7223. if (defined $descr) {
  7224. $descr = esc_html($descr);
  7225. } else {
  7226. $descr = "$project " .
  7227. ($format eq 'rss' ? 'RSS' : 'Atom') .
  7228. " feed";
  7229. }
  7230. my $owner = git_get_project_owner($project);
  7231. $owner = esc_html($owner);
  7232. #header
  7233. my $alt_url;
  7234. if (defined $file_name) {
  7235. $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
  7236. } elsif (defined $hash) {
  7237. $alt_url = href(-full=>1, action=>"log", hash=>$hash);
  7238. } else {
  7239. $alt_url = href(-full=>1, action=>"summary");
  7240. }
  7241. $alt_url = esc_attr($alt_url);
  7242. print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
  7243. if ($format eq 'rss') {
  7244. print <<XML;
  7245. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  7246. <channel>
  7247. XML
  7248. print "<title>$title</title>\n" .
  7249. "<link>$alt_url</link>\n" .
  7250. "<description>$descr</description>\n" .
  7251. "<language>en</language>\n" .
  7252. # project owner is responsible for 'editorial' content
  7253. "<managingEditor>$owner</managingEditor>\n";
  7254. if (defined $logo || defined $favicon) {
  7255. # prefer the logo to the favicon, since RSS
  7256. # doesn't allow both
  7257. my $img = esc_url($logo || $favicon);
  7258. print "<image>\n" .
  7259. "<url>$img</url>\n" .
  7260. "<title>$title</title>\n" .
  7261. "<link>$alt_url</link>\n" .
  7262. "</image>\n";
  7263. }
  7264. if (%latest_date) {
  7265. print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
  7266. print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
  7267. }
  7268. print "<generator>gitweb v.$version/$git_version</generator>\n";
  7269. } elsif ($format eq 'atom') {
  7270. print <<XML;
  7271. <feed xmlns="http://www.w3.org/2005/Atom">
  7272. XML
  7273. print "<title>$title</title>\n" .
  7274. "<subtitle>$descr</subtitle>\n" .
  7275. '<link rel="alternate" type="text/html" href="' .
  7276. $alt_url . '" />' . "\n" .
  7277. '<link rel="self" type="' . $content_type . '" href="' .
  7278. $cgi->self_url() . '" />' . "\n" .
  7279. "<id>" . esc_url(href(-full=>1)) . "</id>\n" .
  7280. # use project owner for feed author
  7281. "<author><name>$owner</name></author>\n";
  7282. if (defined $favicon) {
  7283. print "<icon>" . esc_url($favicon) . "</icon>\n";
  7284. }
  7285. if (defined $logo) {
  7286. # not twice as wide as tall: 72 x 27 pixels
  7287. print "<logo>" . esc_url($logo) . "</logo>\n";
  7288. }
  7289. if (! %latest_date) {
  7290. # dummy date to keep the feed valid until commits trickle in:
  7291. print "<updated>1970-01-01T00:00:00Z</updated>\n";
  7292. } else {
  7293. print "<updated>$latest_date{'iso-8601'}</updated>\n";
  7294. }
  7295. print "<generator version='$version/$git_version'>gitweb</generator>\n";
  7296. }
  7297. # contents
  7298. for (my $i = 0; $i <= $#commitlist; $i++) {
  7299. my %co = %{$commitlist[$i]};
  7300. my $commit = $co{'id'};
  7301. # we read 150, we always show 30 and the ones more recent than 48 hours
  7302. if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
  7303. last;
  7304. }
  7305. my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
  7306. # get list of changed files
  7307. open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
  7308. $co{'parent'} || "--root",
  7309. $co{'id'}, "--", (defined $file_name ? $file_name : ())
  7310. or next;
  7311. my @difftree = map { chomp; $_ } <$fd>;
  7312. close $fd
  7313. or next;
  7314. # print element (entry, item)
  7315. my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
  7316. if ($format eq 'rss') {
  7317. print "<item>\n" .
  7318. "<title>" . esc_html($co{'title'}) . "</title>\n" .
  7319. "<author>" . esc_html($co{'author'}) . "</author>\n" .
  7320. "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
  7321. "<guid isPermaLink=\"true\">$co_url</guid>\n" .
  7322. "<link>" . esc_html($co_url) . "</link>\n" .
  7323. "<description>" . esc_html($co{'title'}) . "</description>\n" .
  7324. "<content:encoded>" .
  7325. "<![CDATA[\n";
  7326. } elsif ($format eq 'atom') {
  7327. print "<entry>\n" .
  7328. "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
  7329. "<updated>$cd{'iso-8601'}</updated>\n" .
  7330. "<author>\n" .
  7331. " <name>" . esc_html($co{'author_name'}) . "</name>\n";
  7332. if ($co{'author_email'}) {
  7333. print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
  7334. }
  7335. print "</author>\n" .
  7336. # use committer for contributor
  7337. "<contributor>\n" .
  7338. " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
  7339. if ($co{'committer_email'}) {
  7340. print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
  7341. }
  7342. print "</contributor>\n" .
  7343. "<published>$cd{'iso-8601'}</published>\n" .
  7344. "<link rel=\"alternate\" type=\"text/html\" href=\"" . esc_attr($co_url) . "\" />\n" .
  7345. "<id>" . esc_html($co_url) . "</id>\n" .
  7346. "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
  7347. "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
  7348. }
  7349. my $comment = $co{'comment'};
  7350. print "<pre>\n";
  7351. foreach my $line (@$comment) {
  7352. $line = esc_html($line);
  7353. print "$line\n";
  7354. }
  7355. print "</pre><ul>\n";
  7356. foreach my $difftree_line (@difftree) {
  7357. my %difftree = parse_difftree_raw_line($difftree_line);
  7358. next if !$difftree{'from_id'};
  7359. my $file = $difftree{'file'} || $difftree{'to_file'};
  7360. print "<li>" .
  7361. "[" .
  7362. $cgi->a({-href => href(-full=>1, action=>"blobdiff",
  7363. hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
  7364. hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
  7365. file_name=>$file, file_parent=>$difftree{'from_file'}),
  7366. -title => "diff"}, 'D');
  7367. if ($have_blame) {
  7368. print $cgi->a({-href => href(-full=>1, action=>"blame",
  7369. file_name=>$file, hash_base=>$commit),
  7370. -title => "blame"}, 'B');
  7371. }
  7372. # if this is not a feed of a file history
  7373. if (!defined $file_name || $file_name ne $file) {
  7374. print $cgi->a({-href => href(-full=>1, action=>"history",
  7375. file_name=>$file, hash=>$commit),
  7376. -title => "history"}, 'H');
  7377. }
  7378. $file = esc_path($file);
  7379. print "] ".
  7380. "$file</li>\n";
  7381. }
  7382. if ($format eq 'rss') {
  7383. print "</ul>]]>\n" .
  7384. "</content:encoded>\n" .
  7385. "</item>\n";
  7386. } elsif ($format eq 'atom') {
  7387. print "</ul>\n</div>\n" .
  7388. "</content>\n" .
  7389. "</entry>\n";
  7390. }
  7391. }
  7392. # end of feed
  7393. if ($format eq 'rss') {
  7394. print "</channel>\n</rss>\n";
  7395. } elsif ($format eq 'atom') {
  7396. print "</feed>\n";
  7397. }
  7398. }
  7399. sub git_rss {
  7400. git_feed('rss');
  7401. }
  7402. sub git_atom {
  7403. git_feed('atom');
  7404. }
  7405. sub git_opml {
  7406. my @list = git_get_projects_list($project_filter, $strict_export);
  7407. if (!@list) {
  7408. die_error(404, "No projects found");
  7409. }
  7410. print $cgi->header(
  7411. -type => 'text/xml',
  7412. -charset => 'utf-8',
  7413. -content_disposition => 'inline; filename="opml.xml"');
  7414. my $title = esc_html($site_name);
  7415. my $filter = " within subdirectory ";
  7416. if (defined $project_filter) {
  7417. $filter .= esc_html($project_filter);
  7418. } else {
  7419. $filter = "";
  7420. }
  7421. print <<XML;
  7422. <?xml version="1.0" encoding="utf-8"?>
  7423. <opml version="1.0">
  7424. <head>
  7425. <title>$title OPML Export$filter</title>
  7426. </head>
  7427. <body>
  7428. <outline text="git RSS feeds">
  7429. XML
  7430. foreach my $pr (@list) {
  7431. my %proj = %$pr;
  7432. my $head = git_get_head_hash($proj{'path'});
  7433. if (!defined $head) {
  7434. next;
  7435. }
  7436. $git_dir = "$projectroot/$proj{'path'}";
  7437. my %co = parse_commit($head);
  7438. if (!%co) {
  7439. next;
  7440. }
  7441. my $path = esc_html(chop_str($proj{'path'}, 25, 5));
  7442. my $rss = esc_attr(href('project' => $proj{'path'}, 'action' => 'rss', -full => 1));
  7443. my $html = esc_attr(href('project' => $proj{'path'}, 'action' => 'summary', -full => 1));
  7444. print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
  7445. }
  7446. print <<XML;
  7447. </outline>
  7448. </body>
  7449. </opml>
  7450. XML
  7451. }