diff.c 191 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949
  1. /*
  2. * Copyright (C) 2005 Junio C Hamano
  3. */
  4. #include "cache.h"
  5. #include "config.h"
  6. #include "tempfile.h"
  7. #include "quote.h"
  8. #include "diff.h"
  9. #include "diffcore.h"
  10. #include "delta.h"
  11. #include "xdiff-interface.h"
  12. #include "color.h"
  13. #include "attr.h"
  14. #include "run-command.h"
  15. #include "utf8.h"
  16. #include "object-store.h"
  17. #include "userdiff.h"
  18. #include "submodule-config.h"
  19. #include "submodule.h"
  20. #include "hashmap.h"
  21. #include "ll-merge.h"
  22. #include "string-list.h"
  23. #include "strvec.h"
  24. #include "graph.h"
  25. #include "packfile.h"
  26. #include "parse-options.h"
  27. #include "help.h"
  28. #include "promisor-remote.h"
  29. #ifdef NO_FAST_WORKING_DIRECTORY
  30. #define FAST_WORKING_DIRECTORY 0
  31. #else
  32. #define FAST_WORKING_DIRECTORY 1
  33. #endif
  34. static int diff_detect_rename_default;
  35. static int diff_indent_heuristic = 1;
  36. static int diff_rename_limit_default = 400;
  37. static int diff_suppress_blank_empty;
  38. static int diff_use_color_default = -1;
  39. static int diff_color_moved_default;
  40. static int diff_color_moved_ws_default;
  41. static int diff_context_default = 3;
  42. static int diff_interhunk_context_default;
  43. static const char *diff_word_regex_cfg;
  44. static const char *external_diff_cmd_cfg;
  45. static const char *diff_order_file_cfg;
  46. int diff_auto_refresh_index = 1;
  47. static int diff_mnemonic_prefix;
  48. static int diff_no_prefix;
  49. static int diff_relative;
  50. static int diff_stat_graph_width;
  51. static int diff_dirstat_permille_default = 30;
  52. static struct diff_options default_diff_options;
  53. static long diff_algorithm;
  54. static unsigned ws_error_highlight_default = WSEH_NEW;
  55. static char diff_colors[][COLOR_MAXLEN] = {
  56. GIT_COLOR_RESET,
  57. GIT_COLOR_NORMAL, /* CONTEXT */
  58. GIT_COLOR_BOLD, /* METAINFO */
  59. GIT_COLOR_CYAN, /* FRAGINFO */
  60. GIT_COLOR_RED, /* OLD */
  61. GIT_COLOR_GREEN, /* NEW */
  62. GIT_COLOR_YELLOW, /* COMMIT */
  63. GIT_COLOR_BG_RED, /* WHITESPACE */
  64. GIT_COLOR_NORMAL, /* FUNCINFO */
  65. GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
  66. GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
  67. GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
  68. GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
  69. GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
  70. GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
  71. GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
  72. GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
  73. GIT_COLOR_FAINT, /* CONTEXT_DIM */
  74. GIT_COLOR_FAINT_RED, /* OLD_DIM */
  75. GIT_COLOR_FAINT_GREEN, /* NEW_DIM */
  76. GIT_COLOR_BOLD, /* CONTEXT_BOLD */
  77. GIT_COLOR_BOLD_RED, /* OLD_BOLD */
  78. GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */
  79. };
  80. static const char *color_diff_slots[] = {
  81. [DIFF_CONTEXT] = "context",
  82. [DIFF_METAINFO] = "meta",
  83. [DIFF_FRAGINFO] = "frag",
  84. [DIFF_FILE_OLD] = "old",
  85. [DIFF_FILE_NEW] = "new",
  86. [DIFF_COMMIT] = "commit",
  87. [DIFF_WHITESPACE] = "whitespace",
  88. [DIFF_FUNCINFO] = "func",
  89. [DIFF_FILE_OLD_MOVED] = "oldMoved",
  90. [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative",
  91. [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed",
  92. [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed",
  93. [DIFF_FILE_NEW_MOVED] = "newMoved",
  94. [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative",
  95. [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed",
  96. [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed",
  97. [DIFF_CONTEXT_DIM] = "contextDimmed",
  98. [DIFF_FILE_OLD_DIM] = "oldDimmed",
  99. [DIFF_FILE_NEW_DIM] = "newDimmed",
  100. [DIFF_CONTEXT_BOLD] = "contextBold",
  101. [DIFF_FILE_OLD_BOLD] = "oldBold",
  102. [DIFF_FILE_NEW_BOLD] = "newBold",
  103. };
  104. define_list_config_array_extra(color_diff_slots, {"plain"});
  105. static int parse_diff_color_slot(const char *var)
  106. {
  107. if (!strcasecmp(var, "plain"))
  108. return DIFF_CONTEXT;
  109. return LOOKUP_CONFIG(color_diff_slots, var);
  110. }
  111. static int parse_dirstat_params(struct diff_options *options, const char *params_string,
  112. struct strbuf *errmsg)
  113. {
  114. char *params_copy = xstrdup(params_string);
  115. struct string_list params = STRING_LIST_INIT_NODUP;
  116. int ret = 0;
  117. int i;
  118. if (*params_copy)
  119. string_list_split_in_place(&params, params_copy, ',', -1);
  120. for (i = 0; i < params.nr; i++) {
  121. const char *p = params.items[i].string;
  122. if (!strcmp(p, "changes")) {
  123. options->flags.dirstat_by_line = 0;
  124. options->flags.dirstat_by_file = 0;
  125. } else if (!strcmp(p, "lines")) {
  126. options->flags.dirstat_by_line = 1;
  127. options->flags.dirstat_by_file = 0;
  128. } else if (!strcmp(p, "files")) {
  129. options->flags.dirstat_by_line = 0;
  130. options->flags.dirstat_by_file = 1;
  131. } else if (!strcmp(p, "noncumulative")) {
  132. options->flags.dirstat_cumulative = 0;
  133. } else if (!strcmp(p, "cumulative")) {
  134. options->flags.dirstat_cumulative = 1;
  135. } else if (isdigit(*p)) {
  136. char *end;
  137. int permille = strtoul(p, &end, 10) * 10;
  138. if (*end == '.' && isdigit(*++end)) {
  139. /* only use first digit */
  140. permille += *end - '0';
  141. /* .. and ignore any further digits */
  142. while (isdigit(*++end))
  143. ; /* nothing */
  144. }
  145. if (!*end)
  146. options->dirstat_permille = permille;
  147. else {
  148. strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
  149. p);
  150. ret++;
  151. }
  152. } else {
  153. strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
  154. ret++;
  155. }
  156. }
  157. string_list_clear(&params, 0);
  158. free(params_copy);
  159. return ret;
  160. }
  161. static int parse_submodule_params(struct diff_options *options, const char *value)
  162. {
  163. if (!strcmp(value, "log"))
  164. options->submodule_format = DIFF_SUBMODULE_LOG;
  165. else if (!strcmp(value, "short"))
  166. options->submodule_format = DIFF_SUBMODULE_SHORT;
  167. else if (!strcmp(value, "diff"))
  168. options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF;
  169. /*
  170. * Please update $__git_diff_submodule_formats in
  171. * git-completion.bash when you add new formats.
  172. */
  173. else
  174. return -1;
  175. return 0;
  176. }
  177. int git_config_rename(const char *var, const char *value)
  178. {
  179. if (!value)
  180. return DIFF_DETECT_RENAME;
  181. if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
  182. return DIFF_DETECT_COPY;
  183. return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
  184. }
  185. long parse_algorithm_value(const char *value)
  186. {
  187. if (!value)
  188. return -1;
  189. else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
  190. return 0;
  191. else if (!strcasecmp(value, "minimal"))
  192. return XDF_NEED_MINIMAL;
  193. else if (!strcasecmp(value, "patience"))
  194. return XDF_PATIENCE_DIFF;
  195. else if (!strcasecmp(value, "histogram"))
  196. return XDF_HISTOGRAM_DIFF;
  197. /*
  198. * Please update $__git_diff_algorithms in git-completion.bash
  199. * when you add new algorithms.
  200. */
  201. return -1;
  202. }
  203. static int parse_one_token(const char **arg, const char *token)
  204. {
  205. const char *rest;
  206. if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
  207. *arg = rest;
  208. return 1;
  209. }
  210. return 0;
  211. }
  212. static int parse_ws_error_highlight(const char *arg)
  213. {
  214. const char *orig_arg = arg;
  215. unsigned val = 0;
  216. while (*arg) {
  217. if (parse_one_token(&arg, "none"))
  218. val = 0;
  219. else if (parse_one_token(&arg, "default"))
  220. val = WSEH_NEW;
  221. else if (parse_one_token(&arg, "all"))
  222. val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
  223. else if (parse_one_token(&arg, "new"))
  224. val |= WSEH_NEW;
  225. else if (parse_one_token(&arg, "old"))
  226. val |= WSEH_OLD;
  227. else if (parse_one_token(&arg, "context"))
  228. val |= WSEH_CONTEXT;
  229. else {
  230. return -1 - (int)(arg - orig_arg);
  231. }
  232. if (*arg)
  233. arg++;
  234. }
  235. return val;
  236. }
  237. /*
  238. * These are to give UI layer defaults.
  239. * The core-level commands such as git-diff-files should
  240. * never be affected by the setting of diff.renames
  241. * the user happens to have in the configuration file.
  242. */
  243. void init_diff_ui_defaults(void)
  244. {
  245. diff_detect_rename_default = DIFF_DETECT_RENAME;
  246. }
  247. int git_diff_heuristic_config(const char *var, const char *value, void *cb)
  248. {
  249. if (!strcmp(var, "diff.indentheuristic"))
  250. diff_indent_heuristic = git_config_bool(var, value);
  251. return 0;
  252. }
  253. static int parse_color_moved(const char *arg)
  254. {
  255. switch (git_parse_maybe_bool(arg)) {
  256. case 0:
  257. return COLOR_MOVED_NO;
  258. case 1:
  259. return COLOR_MOVED_DEFAULT;
  260. default:
  261. break;
  262. }
  263. if (!strcmp(arg, "no"))
  264. return COLOR_MOVED_NO;
  265. else if (!strcmp(arg, "plain"))
  266. return COLOR_MOVED_PLAIN;
  267. else if (!strcmp(arg, "blocks"))
  268. return COLOR_MOVED_BLOCKS;
  269. else if (!strcmp(arg, "zebra"))
  270. return COLOR_MOVED_ZEBRA;
  271. else if (!strcmp(arg, "default"))
  272. return COLOR_MOVED_DEFAULT;
  273. else if (!strcmp(arg, "dimmed-zebra"))
  274. return COLOR_MOVED_ZEBRA_DIM;
  275. else if (!strcmp(arg, "dimmed_zebra"))
  276. return COLOR_MOVED_ZEBRA_DIM;
  277. else
  278. return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
  279. }
  280. static unsigned parse_color_moved_ws(const char *arg)
  281. {
  282. int ret = 0;
  283. struct string_list l = STRING_LIST_INIT_DUP;
  284. struct string_list_item *i;
  285. string_list_split(&l, arg, ',', -1);
  286. for_each_string_list_item(i, &l) {
  287. struct strbuf sb = STRBUF_INIT;
  288. strbuf_addstr(&sb, i->string);
  289. strbuf_trim(&sb);
  290. if (!strcmp(sb.buf, "no"))
  291. ret = 0;
  292. else if (!strcmp(sb.buf, "ignore-space-change"))
  293. ret |= XDF_IGNORE_WHITESPACE_CHANGE;
  294. else if (!strcmp(sb.buf, "ignore-space-at-eol"))
  295. ret |= XDF_IGNORE_WHITESPACE_AT_EOL;
  296. else if (!strcmp(sb.buf, "ignore-all-space"))
  297. ret |= XDF_IGNORE_WHITESPACE;
  298. else if (!strcmp(sb.buf, "allow-indentation-change"))
  299. ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE;
  300. else {
  301. ret |= COLOR_MOVED_WS_ERROR;
  302. error(_("unknown color-moved-ws mode '%s', possible values are 'ignore-space-change', 'ignore-space-at-eol', 'ignore-all-space', 'allow-indentation-change'"), sb.buf);
  303. }
  304. strbuf_release(&sb);
  305. }
  306. if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) &&
  307. (ret & XDF_WHITESPACE_FLAGS)) {
  308. error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
  309. ret |= COLOR_MOVED_WS_ERROR;
  310. }
  311. string_list_clear(&l, 0);
  312. return ret;
  313. }
  314. int git_diff_ui_config(const char *var, const char *value, void *cb)
  315. {
  316. if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
  317. diff_use_color_default = git_config_colorbool(var, value);
  318. return 0;
  319. }
  320. if (!strcmp(var, "diff.colormoved")) {
  321. int cm = parse_color_moved(value);
  322. if (cm < 0)
  323. return -1;
  324. diff_color_moved_default = cm;
  325. return 0;
  326. }
  327. if (!strcmp(var, "diff.colormovedws")) {
  328. unsigned cm = parse_color_moved_ws(value);
  329. if (cm & COLOR_MOVED_WS_ERROR)
  330. return -1;
  331. diff_color_moved_ws_default = cm;
  332. return 0;
  333. }
  334. if (!strcmp(var, "diff.context")) {
  335. diff_context_default = git_config_int(var, value);
  336. if (diff_context_default < 0)
  337. return -1;
  338. return 0;
  339. }
  340. if (!strcmp(var, "diff.interhunkcontext")) {
  341. diff_interhunk_context_default = git_config_int(var, value);
  342. if (diff_interhunk_context_default < 0)
  343. return -1;
  344. return 0;
  345. }
  346. if (!strcmp(var, "diff.renames")) {
  347. diff_detect_rename_default = git_config_rename(var, value);
  348. return 0;
  349. }
  350. if (!strcmp(var, "diff.autorefreshindex")) {
  351. diff_auto_refresh_index = git_config_bool(var, value);
  352. return 0;
  353. }
  354. if (!strcmp(var, "diff.mnemonicprefix")) {
  355. diff_mnemonic_prefix = git_config_bool(var, value);
  356. return 0;
  357. }
  358. if (!strcmp(var, "diff.noprefix")) {
  359. diff_no_prefix = git_config_bool(var, value);
  360. return 0;
  361. }
  362. if (!strcmp(var, "diff.relative")) {
  363. diff_relative = git_config_bool(var, value);
  364. return 0;
  365. }
  366. if (!strcmp(var, "diff.statgraphwidth")) {
  367. diff_stat_graph_width = git_config_int(var, value);
  368. return 0;
  369. }
  370. if (!strcmp(var, "diff.external"))
  371. return git_config_string(&external_diff_cmd_cfg, var, value);
  372. if (!strcmp(var, "diff.wordregex"))
  373. return git_config_string(&diff_word_regex_cfg, var, value);
  374. if (!strcmp(var, "diff.orderfile"))
  375. return git_config_pathname(&diff_order_file_cfg, var, value);
  376. if (!strcmp(var, "diff.ignoresubmodules"))
  377. handle_ignore_submodules_arg(&default_diff_options, value);
  378. if (!strcmp(var, "diff.submodule")) {
  379. if (parse_submodule_params(&default_diff_options, value))
  380. warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
  381. value);
  382. return 0;
  383. }
  384. if (!strcmp(var, "diff.algorithm")) {
  385. diff_algorithm = parse_algorithm_value(value);
  386. if (diff_algorithm < 0)
  387. return -1;
  388. return 0;
  389. }
  390. if (git_color_config(var, value, cb) < 0)
  391. return -1;
  392. return git_diff_basic_config(var, value, cb);
  393. }
  394. int git_diff_basic_config(const char *var, const char *value, void *cb)
  395. {
  396. const char *name;
  397. if (!strcmp(var, "diff.renamelimit")) {
  398. diff_rename_limit_default = git_config_int(var, value);
  399. return 0;
  400. }
  401. if (userdiff_config(var, value) < 0)
  402. return -1;
  403. if (skip_prefix(var, "diff.color.", &name) ||
  404. skip_prefix(var, "color.diff.", &name)) {
  405. int slot = parse_diff_color_slot(name);
  406. if (slot < 0)
  407. return 0;
  408. if (!value)
  409. return config_error_nonbool(var);
  410. return color_parse(value, diff_colors[slot]);
  411. }
  412. if (!strcmp(var, "diff.wserrorhighlight")) {
  413. int val = parse_ws_error_highlight(value);
  414. if (val < 0)
  415. return -1;
  416. ws_error_highlight_default = val;
  417. return 0;
  418. }
  419. /* like GNU diff's --suppress-blank-empty option */
  420. if (!strcmp(var, "diff.suppressblankempty") ||
  421. /* for backwards compatibility */
  422. !strcmp(var, "diff.suppress-blank-empty")) {
  423. diff_suppress_blank_empty = git_config_bool(var, value);
  424. return 0;
  425. }
  426. if (!strcmp(var, "diff.dirstat")) {
  427. struct strbuf errmsg = STRBUF_INIT;
  428. default_diff_options.dirstat_permille = diff_dirstat_permille_default;
  429. if (parse_dirstat_params(&default_diff_options, value, &errmsg))
  430. warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
  431. errmsg.buf);
  432. strbuf_release(&errmsg);
  433. diff_dirstat_permille_default = default_diff_options.dirstat_permille;
  434. return 0;
  435. }
  436. if (git_diff_heuristic_config(var, value, cb) < 0)
  437. return -1;
  438. return git_default_config(var, value, cb);
  439. }
  440. static char *quote_two(const char *one, const char *two)
  441. {
  442. int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ);
  443. int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ);
  444. struct strbuf res = STRBUF_INIT;
  445. if (need_one + need_two) {
  446. strbuf_addch(&res, '"');
  447. quote_c_style(one, &res, NULL, CQUOTE_NODQ);
  448. quote_c_style(two, &res, NULL, CQUOTE_NODQ);
  449. strbuf_addch(&res, '"');
  450. } else {
  451. strbuf_addstr(&res, one);
  452. strbuf_addstr(&res, two);
  453. }
  454. return strbuf_detach(&res, NULL);
  455. }
  456. static const char *external_diff(void)
  457. {
  458. static const char *external_diff_cmd = NULL;
  459. static int done_preparing = 0;
  460. if (done_preparing)
  461. return external_diff_cmd;
  462. external_diff_cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
  463. if (!external_diff_cmd)
  464. external_diff_cmd = external_diff_cmd_cfg;
  465. done_preparing = 1;
  466. return external_diff_cmd;
  467. }
  468. /*
  469. * Keep track of files used for diffing. Sometimes such an entry
  470. * refers to a temporary file, sometimes to an existing file, and
  471. * sometimes to "/dev/null".
  472. */
  473. static struct diff_tempfile {
  474. /*
  475. * filename external diff should read from, or NULL if this
  476. * entry is currently not in use:
  477. */
  478. const char *name;
  479. char hex[GIT_MAX_HEXSZ + 1];
  480. char mode[10];
  481. /*
  482. * If this diff_tempfile instance refers to a temporary file,
  483. * this tempfile object is used to manage its lifetime.
  484. */
  485. struct tempfile *tempfile;
  486. } diff_temp[2];
  487. struct emit_callback {
  488. int color_diff;
  489. unsigned ws_rule;
  490. int blank_at_eof_in_preimage;
  491. int blank_at_eof_in_postimage;
  492. int lno_in_preimage;
  493. int lno_in_postimage;
  494. const char **label_path;
  495. struct diff_words_data *diff_words;
  496. struct diff_options *opt;
  497. struct strbuf *header;
  498. };
  499. static int count_lines(const char *data, int size)
  500. {
  501. int count, ch, completely_empty = 1, nl_just_seen = 0;
  502. count = 0;
  503. while (0 < size--) {
  504. ch = *data++;
  505. if (ch == '\n') {
  506. count++;
  507. nl_just_seen = 1;
  508. completely_empty = 0;
  509. }
  510. else {
  511. nl_just_seen = 0;
  512. completely_empty = 0;
  513. }
  514. }
  515. if (completely_empty)
  516. return 0;
  517. if (!nl_just_seen)
  518. count++; /* no trailing newline */
  519. return count;
  520. }
  521. static int fill_mmfile(struct repository *r, mmfile_t *mf,
  522. struct diff_filespec *one)
  523. {
  524. if (!DIFF_FILE_VALID(one)) {
  525. mf->ptr = (char *)""; /* does not matter */
  526. mf->size = 0;
  527. return 0;
  528. }
  529. else if (diff_populate_filespec(r, one, NULL))
  530. return -1;
  531. mf->ptr = one->data;
  532. mf->size = one->size;
  533. return 0;
  534. }
  535. /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
  536. static unsigned long diff_filespec_size(struct repository *r,
  537. struct diff_filespec *one)
  538. {
  539. struct diff_populate_filespec_options dpf_options = {
  540. .check_size_only = 1,
  541. };
  542. if (!DIFF_FILE_VALID(one))
  543. return 0;
  544. diff_populate_filespec(r, one, &dpf_options);
  545. return one->size;
  546. }
  547. static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
  548. {
  549. char *ptr = mf->ptr;
  550. long size = mf->size;
  551. int cnt = 0;
  552. if (!size)
  553. return cnt;
  554. ptr += size - 1; /* pointing at the very end */
  555. if (*ptr != '\n')
  556. ; /* incomplete line */
  557. else
  558. ptr--; /* skip the last LF */
  559. while (mf->ptr < ptr) {
  560. char *prev_eol;
  561. for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
  562. if (*prev_eol == '\n')
  563. break;
  564. if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
  565. break;
  566. cnt++;
  567. ptr = prev_eol - 1;
  568. }
  569. return cnt;
  570. }
  571. static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
  572. struct emit_callback *ecbdata)
  573. {
  574. int l1, l2, at;
  575. unsigned ws_rule = ecbdata->ws_rule;
  576. l1 = count_trailing_blank(mf1, ws_rule);
  577. l2 = count_trailing_blank(mf2, ws_rule);
  578. if (l2 <= l1) {
  579. ecbdata->blank_at_eof_in_preimage = 0;
  580. ecbdata->blank_at_eof_in_postimage = 0;
  581. return;
  582. }
  583. at = count_lines(mf1->ptr, mf1->size);
  584. ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
  585. at = count_lines(mf2->ptr, mf2->size);
  586. ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
  587. }
  588. static void emit_line_0(struct diff_options *o,
  589. const char *set_sign, const char *set, unsigned reverse, const char *reset,
  590. int first, const char *line, int len)
  591. {
  592. int has_trailing_newline, has_trailing_carriage_return;
  593. int needs_reset = 0; /* at the end of the line */
  594. FILE *file = o->file;
  595. fputs(diff_line_prefix(o), file);
  596. has_trailing_newline = (len > 0 && line[len-1] == '\n');
  597. if (has_trailing_newline)
  598. len--;
  599. has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
  600. if (has_trailing_carriage_return)
  601. len--;
  602. if (!len && !first)
  603. goto end_of_line;
  604. if (reverse && want_color(o->use_color)) {
  605. fputs(GIT_COLOR_REVERSE, file);
  606. needs_reset = 1;
  607. }
  608. if (set_sign) {
  609. fputs(set_sign, file);
  610. needs_reset = 1;
  611. }
  612. if (first)
  613. fputc(first, file);
  614. if (!len)
  615. goto end_of_line;
  616. if (set) {
  617. if (set_sign && set != set_sign)
  618. fputs(reset, file);
  619. fputs(set, file);
  620. needs_reset = 1;
  621. }
  622. fwrite(line, len, 1, file);
  623. needs_reset = 1; /* 'line' may contain color codes. */
  624. end_of_line:
  625. if (needs_reset)
  626. fputs(reset, file);
  627. if (has_trailing_carriage_return)
  628. fputc('\r', file);
  629. if (has_trailing_newline)
  630. fputc('\n', file);
  631. }
  632. static void emit_line(struct diff_options *o, const char *set, const char *reset,
  633. const char *line, int len)
  634. {
  635. emit_line_0(o, set, NULL, 0, reset, 0, line, len);
  636. }
  637. enum diff_symbol {
  638. DIFF_SYMBOL_BINARY_DIFF_HEADER,
  639. DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
  640. DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
  641. DIFF_SYMBOL_BINARY_DIFF_BODY,
  642. DIFF_SYMBOL_BINARY_DIFF_FOOTER,
  643. DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
  644. DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
  645. DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
  646. DIFF_SYMBOL_STATS_LINE,
  647. DIFF_SYMBOL_WORD_DIFF,
  648. DIFF_SYMBOL_STAT_SEP,
  649. DIFF_SYMBOL_SUMMARY,
  650. DIFF_SYMBOL_SUBMODULE_ADD,
  651. DIFF_SYMBOL_SUBMODULE_DEL,
  652. DIFF_SYMBOL_SUBMODULE_UNTRACKED,
  653. DIFF_SYMBOL_SUBMODULE_MODIFIED,
  654. DIFF_SYMBOL_SUBMODULE_HEADER,
  655. DIFF_SYMBOL_SUBMODULE_ERROR,
  656. DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
  657. DIFF_SYMBOL_REWRITE_DIFF,
  658. DIFF_SYMBOL_BINARY_FILES,
  659. DIFF_SYMBOL_HEADER,
  660. DIFF_SYMBOL_FILEPAIR_PLUS,
  661. DIFF_SYMBOL_FILEPAIR_MINUS,
  662. DIFF_SYMBOL_WORDS_PORCELAIN,
  663. DIFF_SYMBOL_WORDS,
  664. DIFF_SYMBOL_CONTEXT,
  665. DIFF_SYMBOL_CONTEXT_INCOMPLETE,
  666. DIFF_SYMBOL_PLUS,
  667. DIFF_SYMBOL_MINUS,
  668. DIFF_SYMBOL_NO_LF_EOF,
  669. DIFF_SYMBOL_CONTEXT_FRAGINFO,
  670. DIFF_SYMBOL_CONTEXT_MARKER,
  671. DIFF_SYMBOL_SEPARATOR
  672. };
  673. /*
  674. * Flags for content lines:
  675. * 0..12 are whitespace rules
  676. * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
  677. * 16 is marking if the line is blank at EOF
  678. */
  679. #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
  680. #define DIFF_SYMBOL_MOVED_LINE (1<<17)
  681. #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
  682. #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
  683. #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
  684. /*
  685. * This struct is used when we need to buffer the output of the diff output.
  686. *
  687. * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
  688. * into the pre/post image file. This pointer could be a union with the
  689. * line pointer. By storing an offset into the file instead of the literal line,
  690. * we can decrease the memory footprint for the buffered output. At first we
  691. * may want to only have indirection for the content lines, but we could also
  692. * enhance the state for emitting prefabricated lines, e.g. the similarity
  693. * score line or hunk/file headers would only need to store a number or path
  694. * and then the output can be constructed later on depending on state.
  695. */
  696. struct emitted_diff_symbol {
  697. const char *line;
  698. int len;
  699. int flags;
  700. int indent_off; /* Offset to first non-whitespace character */
  701. int indent_width; /* The visual width of the indentation */
  702. enum diff_symbol s;
  703. };
  704. #define EMITTED_DIFF_SYMBOL_INIT {NULL}
  705. struct emitted_diff_symbols {
  706. struct emitted_diff_symbol *buf;
  707. int nr, alloc;
  708. };
  709. #define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
  710. static void append_emitted_diff_symbol(struct diff_options *o,
  711. struct emitted_diff_symbol *e)
  712. {
  713. struct emitted_diff_symbol *f;
  714. ALLOC_GROW(o->emitted_symbols->buf,
  715. o->emitted_symbols->nr + 1,
  716. o->emitted_symbols->alloc);
  717. f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
  718. memcpy(f, e, sizeof(struct emitted_diff_symbol));
  719. f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
  720. }
  721. struct moved_entry {
  722. struct hashmap_entry ent;
  723. const struct emitted_diff_symbol *es;
  724. struct moved_entry *next_line;
  725. };
  726. struct moved_block {
  727. struct moved_entry *match;
  728. int wsd; /* The whitespace delta of this block */
  729. };
  730. static void moved_block_clear(struct moved_block *b)
  731. {
  732. memset(b, 0, sizeof(*b));
  733. }
  734. #define INDENT_BLANKLINE INT_MIN
  735. static void fill_es_indent_data(struct emitted_diff_symbol *es)
  736. {
  737. unsigned int off = 0, i;
  738. int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK;
  739. const char *s = es->line;
  740. const int len = es->len;
  741. /* skip any \v \f \r at start of indentation */
  742. while (s[off] == '\f' || s[off] == '\v' ||
  743. (s[off] == '\r' && off < len - 1))
  744. off++;
  745. /* calculate the visual width of indentation */
  746. while(1) {
  747. if (s[off] == ' ') {
  748. width++;
  749. off++;
  750. } else if (s[off] == '\t') {
  751. width += tab_width - (width % tab_width);
  752. while (s[++off] == '\t')
  753. width += tab_width;
  754. } else {
  755. break;
  756. }
  757. }
  758. /* check if this line is blank */
  759. for (i = off; i < len; i++)
  760. if (!isspace(s[i]))
  761. break;
  762. if (i == len) {
  763. es->indent_width = INDENT_BLANKLINE;
  764. es->indent_off = len;
  765. } else {
  766. es->indent_off = off;
  767. es->indent_width = width;
  768. }
  769. }
  770. static int compute_ws_delta(const struct emitted_diff_symbol *a,
  771. const struct emitted_diff_symbol *b,
  772. int *out)
  773. {
  774. int a_len = a->len,
  775. b_len = b->len,
  776. a_off = a->indent_off,
  777. a_width = a->indent_width,
  778. b_off = b->indent_off,
  779. b_width = b->indent_width;
  780. int delta;
  781. if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE) {
  782. *out = INDENT_BLANKLINE;
  783. return 1;
  784. }
  785. if (a->s == DIFF_SYMBOL_PLUS)
  786. delta = a_width - b_width;
  787. else
  788. delta = b_width - a_width;
  789. if (a_len - a_off != b_len - b_off ||
  790. memcmp(a->line + a_off, b->line + b_off, a_len - a_off))
  791. return 0;
  792. *out = delta;
  793. return 1;
  794. }
  795. static int cmp_in_block_with_wsd(const struct diff_options *o,
  796. const struct moved_entry *cur,
  797. const struct moved_entry *match,
  798. struct moved_block *pmb,
  799. int n)
  800. {
  801. struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
  802. int al = cur->es->len, bl = match->es->len, cl = l->len;
  803. const char *a = cur->es->line,
  804. *b = match->es->line,
  805. *c = l->line;
  806. int a_off = cur->es->indent_off,
  807. a_width = cur->es->indent_width,
  808. c_off = l->indent_off,
  809. c_width = l->indent_width;
  810. int delta;
  811. /*
  812. * We need to check if 'cur' is equal to 'match'. As those
  813. * are from the same (+/-) side, we do not need to adjust for
  814. * indent changes. However these were found using fuzzy
  815. * matching so we do have to check if they are equal. Here we
  816. * just check the lengths. We delay calling memcmp() to check
  817. * the contents until later as if the length comparison for a
  818. * and c fails we can avoid the call all together.
  819. */
  820. if (al != bl)
  821. return 1;
  822. /* If 'l' and 'cur' are both blank then they match. */
  823. if (a_width == INDENT_BLANKLINE && c_width == INDENT_BLANKLINE)
  824. return 0;
  825. /*
  826. * The indent changes of the block are known and stored in pmb->wsd;
  827. * however we need to check if the indent changes of the current line
  828. * match those of the current block and that the text of 'l' and 'cur'
  829. * after the indentation match.
  830. */
  831. if (cur->es->s == DIFF_SYMBOL_PLUS)
  832. delta = a_width - c_width;
  833. else
  834. delta = c_width - a_width;
  835. /*
  836. * If the previous lines of this block were all blank then set its
  837. * whitespace delta.
  838. */
  839. if (pmb->wsd == INDENT_BLANKLINE)
  840. pmb->wsd = delta;
  841. return !(delta == pmb->wsd && al - a_off == cl - c_off &&
  842. !memcmp(a, b, al) && !
  843. memcmp(a + a_off, c + c_off, al - a_off));
  844. }
  845. static int moved_entry_cmp(const void *hashmap_cmp_fn_data,
  846. const struct hashmap_entry *eptr,
  847. const struct hashmap_entry *entry_or_key,
  848. const void *keydata)
  849. {
  850. const struct diff_options *diffopt = hashmap_cmp_fn_data;
  851. const struct moved_entry *a, *b;
  852. unsigned flags = diffopt->color_moved_ws_handling
  853. & XDF_WHITESPACE_FLAGS;
  854. a = container_of(eptr, const struct moved_entry, ent);
  855. b = container_of(entry_or_key, const struct moved_entry, ent);
  856. if (diffopt->color_moved_ws_handling &
  857. COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
  858. /*
  859. * As there is not specific white space config given,
  860. * we'd need to check for a new block, so ignore all
  861. * white space. The setup of the white space
  862. * configuration for the next block is done else where
  863. */
  864. flags |= XDF_IGNORE_WHITESPACE;
  865. return !xdiff_compare_lines(a->es->line, a->es->len,
  866. b->es->line, b->es->len,
  867. flags);
  868. }
  869. static struct moved_entry *prepare_entry(struct diff_options *o,
  870. int line_no)
  871. {
  872. struct moved_entry *ret = xmalloc(sizeof(*ret));
  873. struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
  874. unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
  875. unsigned int hash = xdiff_hash_string(l->line, l->len, flags);
  876. hashmap_entry_init(&ret->ent, hash);
  877. ret->es = l;
  878. ret->next_line = NULL;
  879. return ret;
  880. }
  881. static void add_lines_to_move_detection(struct diff_options *o,
  882. struct hashmap *add_lines,
  883. struct hashmap *del_lines)
  884. {
  885. struct moved_entry *prev_line = NULL;
  886. int n;
  887. for (n = 0; n < o->emitted_symbols->nr; n++) {
  888. struct hashmap *hm;
  889. struct moved_entry *key;
  890. switch (o->emitted_symbols->buf[n].s) {
  891. case DIFF_SYMBOL_PLUS:
  892. hm = add_lines;
  893. break;
  894. case DIFF_SYMBOL_MINUS:
  895. hm = del_lines;
  896. break;
  897. default:
  898. prev_line = NULL;
  899. continue;
  900. }
  901. if (o->color_moved_ws_handling &
  902. COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
  903. fill_es_indent_data(&o->emitted_symbols->buf[n]);
  904. key = prepare_entry(o, n);
  905. if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
  906. prev_line->next_line = key;
  907. hashmap_add(hm, &key->ent);
  908. prev_line = key;
  909. }
  910. }
  911. static void pmb_advance_or_null(struct diff_options *o,
  912. struct moved_entry *match,
  913. struct hashmap *hm,
  914. struct moved_block *pmb,
  915. int pmb_nr)
  916. {
  917. int i;
  918. for (i = 0; i < pmb_nr; i++) {
  919. struct moved_entry *prev = pmb[i].match;
  920. struct moved_entry *cur = (prev && prev->next_line) ?
  921. prev->next_line : NULL;
  922. if (cur && !hm->cmpfn(o, &cur->ent, &match->ent, NULL)) {
  923. pmb[i].match = cur;
  924. } else {
  925. pmb[i].match = NULL;
  926. }
  927. }
  928. }
  929. static void pmb_advance_or_null_multi_match(struct diff_options *o,
  930. struct moved_entry *match,
  931. struct hashmap *hm,
  932. struct moved_block *pmb,
  933. int pmb_nr, int n)
  934. {
  935. int i;
  936. char *got_match = xcalloc(1, pmb_nr);
  937. hashmap_for_each_entry_from(hm, match, ent) {
  938. for (i = 0; i < pmb_nr; i++) {
  939. struct moved_entry *prev = pmb[i].match;
  940. struct moved_entry *cur = (prev && prev->next_line) ?
  941. prev->next_line : NULL;
  942. if (!cur)
  943. continue;
  944. if (!cmp_in_block_with_wsd(o, cur, match, &pmb[i], n))
  945. got_match[i] |= 1;
  946. }
  947. }
  948. for (i = 0; i < pmb_nr; i++) {
  949. if (got_match[i]) {
  950. /* Advance to the next line */
  951. pmb[i].match = pmb[i].match->next_line;
  952. } else {
  953. moved_block_clear(&pmb[i]);
  954. }
  955. }
  956. free(got_match);
  957. }
  958. static int shrink_potential_moved_blocks(struct moved_block *pmb,
  959. int pmb_nr)
  960. {
  961. int lp, rp;
  962. /* Shrink the set of potential block to the remaining running */
  963. for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
  964. while (lp < pmb_nr && pmb[lp].match)
  965. lp++;
  966. /* lp points at the first NULL now */
  967. while (rp > -1 && !pmb[rp].match)
  968. rp--;
  969. /* rp points at the last non-NULL */
  970. if (lp < pmb_nr && rp > -1 && lp < rp) {
  971. pmb[lp] = pmb[rp];
  972. memset(&pmb[rp], 0, sizeof(pmb[rp]));
  973. rp--;
  974. lp++;
  975. }
  976. }
  977. /* Remember the number of running sets */
  978. return rp + 1;
  979. }
  980. /*
  981. * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
  982. *
  983. * Otherwise, if the last block has fewer alphanumeric characters than
  984. * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
  985. * that block.
  986. *
  987. * The last block consists of the (n - block_length)'th line up to but not
  988. * including the nth line.
  989. *
  990. * Returns 0 if the last block is empty or is unset by this function, non zero
  991. * otherwise.
  992. *
  993. * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
  994. * Think of a way to unify them.
  995. */
  996. static int adjust_last_block(struct diff_options *o, int n, int block_length)
  997. {
  998. int i, alnum_count = 0;
  999. if (o->color_moved == COLOR_MOVED_PLAIN)
  1000. return block_length;
  1001. for (i = 1; i < block_length + 1; i++) {
  1002. const char *c = o->emitted_symbols->buf[n - i].line;
  1003. for (; *c; c++) {
  1004. if (!isalnum(*c))
  1005. continue;
  1006. alnum_count++;
  1007. if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
  1008. return 1;
  1009. }
  1010. }
  1011. for (i = 1; i < block_length + 1; i++)
  1012. o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
  1013. return 0;
  1014. }
  1015. /* Find blocks of moved code, delegate actual coloring decision to helper */
  1016. static void mark_color_as_moved(struct diff_options *o,
  1017. struct hashmap *add_lines,
  1018. struct hashmap *del_lines)
  1019. {
  1020. struct moved_block *pmb = NULL; /* potentially moved blocks */
  1021. int pmb_nr = 0, pmb_alloc = 0;
  1022. int n, flipped_block = 0, block_length = 0;
  1023. for (n = 0; n < o->emitted_symbols->nr; n++) {
  1024. struct hashmap *hm = NULL;
  1025. struct moved_entry *key;
  1026. struct moved_entry *match = NULL;
  1027. struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
  1028. enum diff_symbol last_symbol = 0;
  1029. switch (l->s) {
  1030. case DIFF_SYMBOL_PLUS:
  1031. hm = del_lines;
  1032. key = prepare_entry(o, n);
  1033. match = hashmap_get_entry(hm, key, ent, NULL);
  1034. free(key);
  1035. break;
  1036. case DIFF_SYMBOL_MINUS:
  1037. hm = add_lines;
  1038. key = prepare_entry(o, n);
  1039. match = hashmap_get_entry(hm, key, ent, NULL);
  1040. free(key);
  1041. break;
  1042. default:
  1043. flipped_block = 0;
  1044. }
  1045. if (!match) {
  1046. int i;
  1047. adjust_last_block(o, n, block_length);
  1048. for(i = 0; i < pmb_nr; i++)
  1049. moved_block_clear(&pmb[i]);
  1050. pmb_nr = 0;
  1051. block_length = 0;
  1052. flipped_block = 0;
  1053. last_symbol = l->s;
  1054. continue;
  1055. }
  1056. if (o->color_moved == COLOR_MOVED_PLAIN) {
  1057. last_symbol = l->s;
  1058. l->flags |= DIFF_SYMBOL_MOVED_LINE;
  1059. continue;
  1060. }
  1061. if (o->color_moved_ws_handling &
  1062. COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
  1063. pmb_advance_or_null_multi_match(o, match, hm, pmb, pmb_nr, n);
  1064. else
  1065. pmb_advance_or_null(o, match, hm, pmb, pmb_nr);
  1066. pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
  1067. if (pmb_nr == 0) {
  1068. /*
  1069. * The current line is the start of a new block.
  1070. * Setup the set of potential blocks.
  1071. */
  1072. hashmap_for_each_entry_from(hm, match, ent) {
  1073. ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
  1074. if (o->color_moved_ws_handling &
  1075. COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) {
  1076. if (compute_ws_delta(l, match->es,
  1077. &pmb[pmb_nr].wsd))
  1078. pmb[pmb_nr++].match = match;
  1079. } else {
  1080. pmb[pmb_nr].wsd = 0;
  1081. pmb[pmb_nr++].match = match;
  1082. }
  1083. }
  1084. if (adjust_last_block(o, n, block_length) &&
  1085. pmb_nr && last_symbol != l->s)
  1086. flipped_block = (flipped_block + 1) % 2;
  1087. else
  1088. flipped_block = 0;
  1089. block_length = 0;
  1090. }
  1091. if (pmb_nr) {
  1092. block_length++;
  1093. l->flags |= DIFF_SYMBOL_MOVED_LINE;
  1094. if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS)
  1095. l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
  1096. }
  1097. last_symbol = l->s;
  1098. }
  1099. adjust_last_block(o, n, block_length);
  1100. for(n = 0; n < pmb_nr; n++)
  1101. moved_block_clear(&pmb[n]);
  1102. free(pmb);
  1103. }
  1104. #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
  1105. (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
  1106. static void dim_moved_lines(struct diff_options *o)
  1107. {
  1108. int n;
  1109. for (n = 0; n < o->emitted_symbols->nr; n++) {
  1110. struct emitted_diff_symbol *prev = (n != 0) ?
  1111. &o->emitted_symbols->buf[n - 1] : NULL;
  1112. struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
  1113. struct emitted_diff_symbol *next =
  1114. (n < o->emitted_symbols->nr - 1) ?
  1115. &o->emitted_symbols->buf[n + 1] : NULL;
  1116. /* Not a plus or minus line? */
  1117. if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
  1118. continue;
  1119. /* Not a moved line? */
  1120. if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
  1121. continue;
  1122. /*
  1123. * If prev or next are not a plus or minus line,
  1124. * pretend they don't exist
  1125. */
  1126. if (prev && prev->s != DIFF_SYMBOL_PLUS &&
  1127. prev->s != DIFF_SYMBOL_MINUS)
  1128. prev = NULL;
  1129. if (next && next->s != DIFF_SYMBOL_PLUS &&
  1130. next->s != DIFF_SYMBOL_MINUS)
  1131. next = NULL;
  1132. /* Inside a block? */
  1133. if ((prev &&
  1134. (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
  1135. (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
  1136. (next &&
  1137. (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
  1138. (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
  1139. l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
  1140. continue;
  1141. }
  1142. /* Check if we are at an interesting bound: */
  1143. if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
  1144. (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
  1145. (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
  1146. continue;
  1147. if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
  1148. (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
  1149. (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
  1150. continue;
  1151. /*
  1152. * The boundary to prev and next are not interesting,
  1153. * so this line is not interesting as a whole
  1154. */
  1155. l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
  1156. }
  1157. }
  1158. static void emit_line_ws_markup(struct diff_options *o,
  1159. const char *set_sign, const char *set,
  1160. const char *reset,
  1161. int sign_index, const char *line, int len,
  1162. unsigned ws_rule, int blank_at_eof)
  1163. {
  1164. const char *ws = NULL;
  1165. int sign = o->output_indicators[sign_index];
  1166. if (o->ws_error_highlight & ws_rule) {
  1167. ws = diff_get_color_opt(o, DIFF_WHITESPACE);
  1168. if (!*ws)
  1169. ws = NULL;
  1170. }
  1171. if (!ws && !set_sign)
  1172. emit_line_0(o, set, NULL, 0, reset, sign, line, len);
  1173. else if (!ws) {
  1174. emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len);
  1175. } else if (blank_at_eof)
  1176. /* Blank line at EOF - paint '+' as well */
  1177. emit_line_0(o, ws, NULL, 0, reset, sign, line, len);
  1178. else {
  1179. /* Emit just the prefix, then the rest. */
  1180. emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset,
  1181. sign, "", 0);
  1182. ws_check_emit(line, len, ws_rule,
  1183. o->file, set, reset, ws);
  1184. }
  1185. }
  1186. static void emit_diff_symbol_from_struct(struct diff_options *o,
  1187. struct emitted_diff_symbol *eds)
  1188. {
  1189. static const char *nneof = " No newline at end of file\n";
  1190. const char *context, *reset, *set, *set_sign, *meta, *fraginfo;
  1191. struct strbuf sb = STRBUF_INIT;
  1192. enum diff_symbol s = eds->s;
  1193. const char *line = eds->line;
  1194. int len = eds->len;
  1195. unsigned flags = eds->flags;
  1196. switch (s) {
  1197. case DIFF_SYMBOL_NO_LF_EOF:
  1198. context = diff_get_color_opt(o, DIFF_CONTEXT);
  1199. reset = diff_get_color_opt(o, DIFF_RESET);
  1200. putc('\n', o->file);
  1201. emit_line_0(o, context, NULL, 0, reset, '\\',
  1202. nneof, strlen(nneof));
  1203. break;
  1204. case DIFF_SYMBOL_SUBMODULE_HEADER:
  1205. case DIFF_SYMBOL_SUBMODULE_ERROR:
  1206. case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
  1207. case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
  1208. case DIFF_SYMBOL_SUMMARY:
  1209. case DIFF_SYMBOL_STATS_LINE:
  1210. case DIFF_SYMBOL_BINARY_DIFF_BODY:
  1211. case DIFF_SYMBOL_CONTEXT_FRAGINFO:
  1212. emit_line(o, "", "", line, len);
  1213. break;
  1214. case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
  1215. case DIFF_SYMBOL_CONTEXT_MARKER:
  1216. context = diff_get_color_opt(o, DIFF_CONTEXT);
  1217. reset = diff_get_color_opt(o, DIFF_RESET);
  1218. emit_line(o, context, reset, line, len);
  1219. break;
  1220. case DIFF_SYMBOL_SEPARATOR:
  1221. fprintf(o->file, "%s%c",
  1222. diff_line_prefix(o),
  1223. o->line_termination);
  1224. break;
  1225. case DIFF_SYMBOL_CONTEXT:
  1226. set = diff_get_color_opt(o, DIFF_CONTEXT);
  1227. reset = diff_get_color_opt(o, DIFF_RESET);
  1228. set_sign = NULL;
  1229. if (o->flags.dual_color_diffed_diffs) {
  1230. char c = !len ? 0 : line[0];
  1231. if (c == '+')
  1232. set = diff_get_color_opt(o, DIFF_FILE_NEW);
  1233. else if (c == '@')
  1234. set = diff_get_color_opt(o, DIFF_FRAGINFO);
  1235. else if (c == '-')
  1236. set = diff_get_color_opt(o, DIFF_FILE_OLD);
  1237. }
  1238. emit_line_ws_markup(o, set_sign, set, reset,
  1239. OUTPUT_INDICATOR_CONTEXT, line, len,
  1240. flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
  1241. break;
  1242. case DIFF_SYMBOL_PLUS:
  1243. switch (flags & (DIFF_SYMBOL_MOVED_LINE |
  1244. DIFF_SYMBOL_MOVED_LINE_ALT |
  1245. DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
  1246. case DIFF_SYMBOL_MOVED_LINE |
  1247. DIFF_SYMBOL_MOVED_LINE_ALT |
  1248. DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
  1249. set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
  1250. break;
  1251. case DIFF_SYMBOL_MOVED_LINE |
  1252. DIFF_SYMBOL_MOVED_LINE_ALT:
  1253. set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
  1254. break;
  1255. case DIFF_SYMBOL_MOVED_LINE |
  1256. DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
  1257. set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
  1258. break;
  1259. case DIFF_SYMBOL_MOVED_LINE:
  1260. set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
  1261. break;
  1262. default:
  1263. set = diff_get_color_opt(o, DIFF_FILE_NEW);
  1264. }
  1265. reset = diff_get_color_opt(o, DIFF_RESET);
  1266. if (!o->flags.dual_color_diffed_diffs)
  1267. set_sign = NULL;
  1268. else {
  1269. char c = !len ? 0 : line[0];
  1270. set_sign = set;
  1271. if (c == '-')
  1272. set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD);
  1273. else if (c == '@')
  1274. set = diff_get_color_opt(o, DIFF_FRAGINFO);
  1275. else if (c == '+')
  1276. set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD);
  1277. else
  1278. set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD);
  1279. flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK;
  1280. }
  1281. emit_line_ws_markup(o, set_sign, set, reset,
  1282. OUTPUT_INDICATOR_NEW, line, len,
  1283. flags & DIFF_SYMBOL_CONTENT_WS_MASK,
  1284. flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
  1285. break;
  1286. case DIFF_SYMBOL_MINUS:
  1287. switch (flags & (DIFF_SYMBOL_MOVED_LINE |
  1288. DIFF_SYMBOL_MOVED_LINE_ALT |
  1289. DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
  1290. case DIFF_SYMBOL_MOVED_LINE |
  1291. DIFF_SYMBOL_MOVED_LINE_ALT |
  1292. DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
  1293. set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
  1294. break;
  1295. case DIFF_SYMBOL_MOVED_LINE |
  1296. DIFF_SYMBOL_MOVED_LINE_ALT:
  1297. set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
  1298. break;
  1299. case DIFF_SYMBOL_MOVED_LINE |
  1300. DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
  1301. set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
  1302. break;
  1303. case DIFF_SYMBOL_MOVED_LINE:
  1304. set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
  1305. break;
  1306. default:
  1307. set = diff_get_color_opt(o, DIFF_FILE_OLD);
  1308. }
  1309. reset = diff_get_color_opt(o, DIFF_RESET);
  1310. if (!o->flags.dual_color_diffed_diffs)
  1311. set_sign = NULL;
  1312. else {
  1313. char c = !len ? 0 : line[0];
  1314. set_sign = set;
  1315. if (c == '+')
  1316. set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM);
  1317. else if (c == '@')
  1318. set = diff_get_color_opt(o, DIFF_FRAGINFO);
  1319. else if (c == '-')
  1320. set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM);
  1321. else
  1322. set = diff_get_color_opt(o, DIFF_CONTEXT_DIM);
  1323. }
  1324. emit_line_ws_markup(o, set_sign, set, reset,
  1325. OUTPUT_INDICATOR_OLD, line, len,
  1326. flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
  1327. break;
  1328. case DIFF_SYMBOL_WORDS_PORCELAIN:
  1329. context = diff_get_color_opt(o, DIFF_CONTEXT);
  1330. reset = diff_get_color_opt(o, DIFF_RESET);
  1331. emit_line(o, context, reset, line, len);
  1332. fputs("~\n", o->file);
  1333. break;
  1334. case DIFF_SYMBOL_WORDS:
  1335. context = diff_get_color_opt(o, DIFF_CONTEXT);
  1336. reset = diff_get_color_opt(o, DIFF_RESET);
  1337. /*
  1338. * Skip the prefix character, if any. With
  1339. * diff_suppress_blank_empty, there may be
  1340. * none.
  1341. */
  1342. if (line[0] != '\n') {
  1343. line++;
  1344. len--;
  1345. }
  1346. emit_line(o, context, reset, line, len);
  1347. break;
  1348. case DIFF_SYMBOL_FILEPAIR_PLUS:
  1349. meta = diff_get_color_opt(o, DIFF_METAINFO);
  1350. reset = diff_get_color_opt(o, DIFF_RESET);
  1351. fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
  1352. line, reset,
  1353. strchr(line, ' ') ? "\t" : "");
  1354. break;
  1355. case DIFF_SYMBOL_FILEPAIR_MINUS:
  1356. meta = diff_get_color_opt(o, DIFF_METAINFO);
  1357. reset = diff_get_color_opt(o, DIFF_RESET);
  1358. fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
  1359. line, reset,
  1360. strchr(line, ' ') ? "\t" : "");
  1361. break;
  1362. case DIFF_SYMBOL_BINARY_FILES:
  1363. case DIFF_SYMBOL_HEADER:
  1364. fprintf(o->file, "%s", line);
  1365. break;
  1366. case DIFF_SYMBOL_BINARY_DIFF_HEADER:
  1367. fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
  1368. break;
  1369. case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
  1370. fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
  1371. break;
  1372. case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
  1373. fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
  1374. break;
  1375. case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
  1376. fputs(diff_line_prefix(o), o->file);
  1377. fputc('\n', o->file);
  1378. break;
  1379. case DIFF_SYMBOL_REWRITE_DIFF:
  1380. fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
  1381. reset = diff_get_color_opt(o, DIFF_RESET);
  1382. emit_line(o, fraginfo, reset, line, len);
  1383. break;
  1384. case DIFF_SYMBOL_SUBMODULE_ADD:
  1385. set = diff_get_color_opt(o, DIFF_FILE_NEW);
  1386. reset = diff_get_color_opt(o, DIFF_RESET);
  1387. emit_line(o, set, reset, line, len);
  1388. break;
  1389. case DIFF_SYMBOL_SUBMODULE_DEL:
  1390. set = diff_get_color_opt(o, DIFF_FILE_OLD);
  1391. reset = diff_get_color_opt(o, DIFF_RESET);
  1392. emit_line(o, set, reset, line, len);
  1393. break;
  1394. case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
  1395. fprintf(o->file, "%sSubmodule %s contains untracked content\n",
  1396. diff_line_prefix(o), line);
  1397. break;
  1398. case DIFF_SYMBOL_SUBMODULE_MODIFIED:
  1399. fprintf(o->file, "%sSubmodule %s contains modified content\n",
  1400. diff_line_prefix(o), line);
  1401. break;
  1402. case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
  1403. emit_line(o, "", "", " 0 files changed\n",
  1404. strlen(" 0 files changed\n"));
  1405. break;
  1406. case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
  1407. emit_line(o, "", "", " ...\n", strlen(" ...\n"));
  1408. break;
  1409. case DIFF_SYMBOL_WORD_DIFF:
  1410. fprintf(o->file, "%.*s", len, line);
  1411. break;
  1412. case DIFF_SYMBOL_STAT_SEP:
  1413. fputs(o->stat_sep, o->file);
  1414. break;
  1415. default:
  1416. BUG("unknown diff symbol");
  1417. }
  1418. strbuf_release(&sb);
  1419. }
  1420. static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
  1421. const char *line, int len, unsigned flags)
  1422. {
  1423. struct emitted_diff_symbol e = {line, len, flags, 0, 0, s};
  1424. if (o->emitted_symbols)
  1425. append_emitted_diff_symbol(o, &e);
  1426. else
  1427. emit_diff_symbol_from_struct(o, &e);
  1428. }
  1429. void diff_emit_submodule_del(struct diff_options *o, const char *line)
  1430. {
  1431. emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
  1432. }
  1433. void diff_emit_submodule_add(struct diff_options *o, const char *line)
  1434. {
  1435. emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
  1436. }
  1437. void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
  1438. {
  1439. emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
  1440. path, strlen(path), 0);
  1441. }
  1442. void diff_emit_submodule_modified(struct diff_options *o, const char *path)
  1443. {
  1444. emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
  1445. path, strlen(path), 0);
  1446. }
  1447. void diff_emit_submodule_header(struct diff_options *o, const char *header)
  1448. {
  1449. emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
  1450. header, strlen(header), 0);
  1451. }
  1452. void diff_emit_submodule_error(struct diff_options *o, const char *err)
  1453. {
  1454. emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
  1455. }
  1456. void diff_emit_submodule_pipethrough(struct diff_options *o,
  1457. const char *line, int len)
  1458. {
  1459. emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
  1460. }
  1461. static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
  1462. {
  1463. if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
  1464. ecbdata->blank_at_eof_in_preimage &&
  1465. ecbdata->blank_at_eof_in_postimage &&
  1466. ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
  1467. ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
  1468. return 0;
  1469. return ws_blank_line(line, len, ecbdata->ws_rule);
  1470. }
  1471. static void emit_add_line(struct emit_callback *ecbdata,
  1472. const char *line, int len)
  1473. {
  1474. unsigned flags = WSEH_NEW | ecbdata->ws_rule;
  1475. if (new_blank_line_at_eof(ecbdata, line, len))
  1476. flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
  1477. emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
  1478. }
  1479. static void emit_del_line(struct emit_callback *ecbdata,
  1480. const char *line, int len)
  1481. {
  1482. unsigned flags = WSEH_OLD | ecbdata->ws_rule;
  1483. emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
  1484. }
  1485. static void emit_context_line(struct emit_callback *ecbdata,
  1486. const char *line, int len)
  1487. {
  1488. unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
  1489. emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
  1490. }
  1491. static void emit_hunk_header(struct emit_callback *ecbdata,
  1492. const char *line, int len)
  1493. {
  1494. const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
  1495. const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
  1496. const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
  1497. const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
  1498. const char *reverse = ecbdata->color_diff ? GIT_COLOR_REVERSE : "";
  1499. static const char atat[2] = { '@', '@' };
  1500. const char *cp, *ep;
  1501. struct strbuf msgbuf = STRBUF_INIT;
  1502. int org_len = len;
  1503. int i = 1;
  1504. /*
  1505. * As a hunk header must begin with "@@ -<old>, +<new> @@",
  1506. * it always is at least 10 bytes long.
  1507. */
  1508. if (len < 10 ||
  1509. memcmp(line, atat, 2) ||
  1510. !(ep = memmem(line + 2, len - 2, atat, 2))) {
  1511. emit_diff_symbol(ecbdata->opt,
  1512. DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
  1513. return;
  1514. }
  1515. ep += 2; /* skip over @@ */
  1516. /* The hunk header in fraginfo color */
  1517. if (ecbdata->opt->flags.dual_color_diffed_diffs)
  1518. strbuf_addstr(&msgbuf, reverse);
  1519. strbuf_addstr(&msgbuf, frag);
  1520. if (ecbdata->opt->flags.suppress_hunk_header_line_count)
  1521. strbuf_add(&msgbuf, atat, sizeof(atat));
  1522. else
  1523. strbuf_add(&msgbuf, line, ep - line);
  1524. strbuf_addstr(&msgbuf, reset);
  1525. /*
  1526. * trailing "\r\n"
  1527. */
  1528. for ( ; i < 3; i++)
  1529. if (line[len - i] == '\r' || line[len - i] == '\n')
  1530. len--;
  1531. /* blank before the func header */
  1532. for (cp = ep; ep - line < len; ep++)
  1533. if (*ep != ' ' && *ep != '\t')
  1534. break;
  1535. if (ep != cp) {
  1536. strbuf_addstr(&msgbuf, context);
  1537. strbuf_add(&msgbuf, cp, ep - cp);
  1538. strbuf_addstr(&msgbuf, reset);
  1539. }
  1540. if (ep < line + len) {
  1541. strbuf_addstr(&msgbuf, func);
  1542. strbuf_add(&msgbuf, ep, line + len - ep);
  1543. strbuf_addstr(&msgbuf, reset);
  1544. }
  1545. strbuf_add(&msgbuf, line + len, org_len - len);
  1546. strbuf_complete_line(&msgbuf);
  1547. emit_diff_symbol(ecbdata->opt,
  1548. DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
  1549. strbuf_release(&msgbuf);
  1550. }
  1551. static struct diff_tempfile *claim_diff_tempfile(void)
  1552. {
  1553. int i;
  1554. for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
  1555. if (!diff_temp[i].name)
  1556. return diff_temp + i;
  1557. BUG("diff is failing to clean up its tempfiles");
  1558. }
  1559. static void remove_tempfile(void)
  1560. {
  1561. int i;
  1562. for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
  1563. if (is_tempfile_active(diff_temp[i].tempfile))
  1564. delete_tempfile(&diff_temp[i].tempfile);
  1565. diff_temp[i].name = NULL;
  1566. }
  1567. }
  1568. static void add_line_count(struct strbuf *out, int count)
  1569. {
  1570. switch (count) {
  1571. case 0:
  1572. strbuf_addstr(out, "0,0");
  1573. break;
  1574. case 1:
  1575. strbuf_addstr(out, "1");
  1576. break;
  1577. default:
  1578. strbuf_addf(out, "1,%d", count);
  1579. break;
  1580. }
  1581. }
  1582. static void emit_rewrite_lines(struct emit_callback *ecb,
  1583. int prefix, const char *data, int size)
  1584. {
  1585. const char *endp = NULL;
  1586. while (0 < size) {
  1587. int len;
  1588. endp = memchr(data, '\n', size);
  1589. len = endp ? (endp - data + 1) : size;
  1590. if (prefix != '+') {
  1591. ecb->lno_in_preimage++;
  1592. emit_del_line(ecb, data, len);
  1593. } else {
  1594. ecb->lno_in_postimage++;
  1595. emit_add_line(ecb, data, len);
  1596. }
  1597. size -= len;
  1598. data += len;
  1599. }
  1600. if (!endp)
  1601. emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
  1602. }
  1603. static void emit_rewrite_diff(const char *name_a,
  1604. const char *name_b,
  1605. struct diff_filespec *one,
  1606. struct diff_filespec *two,
  1607. struct userdiff_driver *textconv_one,
  1608. struct userdiff_driver *textconv_two,
  1609. struct diff_options *o)
  1610. {
  1611. int lc_a, lc_b;
  1612. static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
  1613. const char *a_prefix, *b_prefix;
  1614. char *data_one, *data_two;
  1615. size_t size_one, size_two;
  1616. struct emit_callback ecbdata;
  1617. struct strbuf out = STRBUF_INIT;
  1618. if (diff_mnemonic_prefix && o->flags.reverse_diff) {
  1619. a_prefix = o->b_prefix;
  1620. b_prefix = o->a_prefix;
  1621. } else {
  1622. a_prefix = o->a_prefix;
  1623. b_prefix = o->b_prefix;
  1624. }
  1625. name_a += (*name_a == '/');
  1626. name_b += (*name_b == '/');
  1627. strbuf_reset(&a_name);
  1628. strbuf_reset(&b_name);
  1629. quote_two_c_style(&a_name, a_prefix, name_a, 0);
  1630. quote_two_c_style(&b_name, b_prefix, name_b, 0);
  1631. size_one = fill_textconv(o->repo, textconv_one, one, &data_one);
  1632. size_two = fill_textconv(o->repo, textconv_two, two, &data_two);
  1633. memset(&ecbdata, 0, sizeof(ecbdata));
  1634. ecbdata.color_diff = want_color(o->use_color);
  1635. ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
  1636. ecbdata.opt = o;
  1637. if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
  1638. mmfile_t mf1, mf2;
  1639. mf1.ptr = (char *)data_one;
  1640. mf2.ptr = (char *)data_two;
  1641. mf1.size = size_one;
  1642. mf2.size = size_two;
  1643. check_blank_at_eof(&mf1, &mf2, &ecbdata);
  1644. }
  1645. ecbdata.lno_in_preimage = 1;
  1646. ecbdata.lno_in_postimage = 1;
  1647. lc_a = count_lines(data_one, size_one);
  1648. lc_b = count_lines(data_two, size_two);
  1649. emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
  1650. a_name.buf, a_name.len, 0);
  1651. emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
  1652. b_name.buf, b_name.len, 0);
  1653. strbuf_addstr(&out, "@@ -");
  1654. if (!o->irreversible_delete)
  1655. add_line_count(&out, lc_a);
  1656. else
  1657. strbuf_addstr(&out, "?,?");
  1658. strbuf_addstr(&out, " +");
  1659. add_line_count(&out, lc_b);
  1660. strbuf_addstr(&out, " @@\n");
  1661. emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
  1662. strbuf_release(&out);
  1663. if (lc_a && !o->irreversible_delete)
  1664. emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
  1665. if (lc_b)
  1666. emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
  1667. if (textconv_one)
  1668. free((char *)data_one);
  1669. if (textconv_two)
  1670. free((char *)data_two);
  1671. }
  1672. struct diff_words_buffer {
  1673. mmfile_t text;
  1674. unsigned long alloc;
  1675. struct diff_words_orig {
  1676. const char *begin, *end;
  1677. } *orig;
  1678. int orig_nr, orig_alloc;
  1679. };
  1680. static void diff_words_append(char *line, unsigned long len,
  1681. struct diff_words_buffer *buffer)
  1682. {
  1683. ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
  1684. line++;
  1685. len--;
  1686. memcpy(buffer->text.ptr + buffer->text.size, line, len);
  1687. buffer->text.size += len;
  1688. buffer->text.ptr[buffer->text.size] = '\0';
  1689. }
  1690. struct diff_words_style_elem {
  1691. const char *prefix;
  1692. const char *suffix;
  1693. const char *color; /* NULL; filled in by the setup code if
  1694. * color is enabled */
  1695. };
  1696. struct diff_words_style {
  1697. enum diff_words_type type;
  1698. struct diff_words_style_elem new_word, old_word, ctx;
  1699. const char *newline;
  1700. };
  1701. static struct diff_words_style diff_words_styles[] = {
  1702. { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
  1703. { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
  1704. { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
  1705. };
  1706. struct diff_words_data {
  1707. struct diff_words_buffer minus, plus;
  1708. const char *current_plus;
  1709. int last_minus;
  1710. struct diff_options *opt;
  1711. regex_t *word_regex;
  1712. enum diff_words_type type;
  1713. struct diff_words_style *style;
  1714. };
  1715. static int fn_out_diff_words_write_helper(struct diff_options *o,
  1716. struct diff_words_style_elem *st_el,
  1717. const char *newline,
  1718. size_t count, const char *buf)
  1719. {
  1720. int print = 0;
  1721. struct strbuf sb = STRBUF_INIT;
  1722. while (count) {
  1723. char *p = memchr(buf, '\n', count);
  1724. if (print)
  1725. strbuf_addstr(&sb, diff_line_prefix(o));
  1726. if (p != buf) {
  1727. const char *reset = st_el->color && *st_el->color ?
  1728. GIT_COLOR_RESET : NULL;
  1729. if (st_el->color && *st_el->color)
  1730. strbuf_addstr(&sb, st_el->color);
  1731. strbuf_addstr(&sb, st_el->prefix);
  1732. strbuf_add(&sb, buf, p ? p - buf : count);
  1733. strbuf_addstr(&sb, st_el->suffix);
  1734. if (reset)
  1735. strbuf_addstr(&sb, reset);
  1736. }
  1737. if (!p)
  1738. goto out;
  1739. strbuf_addstr(&sb, newline);
  1740. count -= p + 1 - buf;
  1741. buf = p + 1;
  1742. print = 1;
  1743. if (count) {
  1744. emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
  1745. sb.buf, sb.len, 0);
  1746. strbuf_reset(&sb);
  1747. }
  1748. }
  1749. out:
  1750. if (sb.len)
  1751. emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
  1752. sb.buf, sb.len, 0);
  1753. strbuf_release(&sb);
  1754. return 0;
  1755. }
  1756. /*
  1757. * '--color-words' algorithm can be described as:
  1758. *
  1759. * 1. collect the minus/plus lines of a diff hunk, divided into
  1760. * minus-lines and plus-lines;
  1761. *
  1762. * 2. break both minus-lines and plus-lines into words and
  1763. * place them into two mmfile_t with one word for each line;
  1764. *
  1765. * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
  1766. *
  1767. * And for the common parts of the both file, we output the plus side text.
  1768. * diff_words->current_plus is used to trace the current position of the plus file
  1769. * which printed. diff_words->last_minus is used to trace the last minus word
  1770. * printed.
  1771. *
  1772. * For '--graph' to work with '--color-words', we need to output the graph prefix
  1773. * on each line of color words output. Generally, there are two conditions on
  1774. * which we should output the prefix.
  1775. *
  1776. * 1. diff_words->last_minus == 0 &&
  1777. * diff_words->current_plus == diff_words->plus.text.ptr
  1778. *
  1779. * that is: the plus text must start as a new line, and if there is no minus
  1780. * word printed, a graph prefix must be printed.
  1781. *
  1782. * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
  1783. * *(diff_words->current_plus - 1) == '\n'
  1784. *
  1785. * that is: a graph prefix must be printed following a '\n'
  1786. */
  1787. static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
  1788. {
  1789. if ((diff_words->last_minus == 0 &&
  1790. diff_words->current_plus == diff_words->plus.text.ptr) ||
  1791. (diff_words->current_plus > diff_words->plus.text.ptr &&
  1792. *(diff_words->current_plus - 1) == '\n')) {
  1793. return 1;
  1794. } else {
  1795. return 0;
  1796. }
  1797. }
  1798. static void fn_out_diff_words_aux(void *priv,
  1799. long minus_first, long minus_len,
  1800. long plus_first, long plus_len,
  1801. const char *func, long funclen)
  1802. {
  1803. struct diff_words_data *diff_words = priv;
  1804. struct diff_words_style *style = diff_words->style;
  1805. const char *minus_begin, *minus_end, *plus_begin, *plus_end;
  1806. struct diff_options *opt = diff_words->opt;
  1807. const char *line_prefix;
  1808. assert(opt);
  1809. line_prefix = diff_line_prefix(opt);
  1810. /* POSIX requires that first be decremented by one if len == 0... */
  1811. if (minus_len) {
  1812. minus_begin = diff_words->minus.orig[minus_first].begin;
  1813. minus_end =
  1814. diff_words->minus.orig[minus_first + minus_len - 1].end;
  1815. } else
  1816. minus_begin = minus_end =
  1817. diff_words->minus.orig[minus_first].end;
  1818. if (plus_len) {
  1819. plus_begin = diff_words->plus.orig[plus_first].begin;
  1820. plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
  1821. } else
  1822. plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
  1823. if (color_words_output_graph_prefix(diff_words)) {
  1824. fputs(line_prefix, diff_words->opt->file);
  1825. }
  1826. if (diff_words->current_plus != plus_begin) {
  1827. fn_out_diff_words_write_helper(diff_words->opt,
  1828. &style->ctx, style->newline,
  1829. plus_begin - diff_words->current_plus,
  1830. diff_words->current_plus);
  1831. }
  1832. if (minus_begin != minus_end) {
  1833. fn_out_diff_words_write_helper(diff_words->opt,
  1834. &style->old_word, style->newline,
  1835. minus_end - minus_begin, minus_begin);
  1836. }
  1837. if (plus_begin != plus_end) {
  1838. fn_out_diff_words_write_helper(diff_words->opt,
  1839. &style->new_word, style->newline,
  1840. plus_end - plus_begin, plus_begin);
  1841. }
  1842. diff_words->current_plus = plus_end;
  1843. diff_words->last_minus = minus_first;
  1844. }
  1845. /* This function starts looking at *begin, and returns 0 iff a word was found. */
  1846. static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
  1847. int *begin, int *end)
  1848. {
  1849. if (word_regex && *begin < buffer->size) {
  1850. regmatch_t match[1];
  1851. if (!regexec_buf(word_regex, buffer->ptr + *begin,
  1852. buffer->size - *begin, 1, match, 0)) {
  1853. char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
  1854. '\n', match[0].rm_eo - match[0].rm_so);
  1855. *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
  1856. *begin += match[0].rm_so;
  1857. return *begin >= *end;
  1858. }
  1859. return -1;
  1860. }
  1861. /* find the next word */
  1862. while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
  1863. (*begin)++;
  1864. if (*begin >= buffer->size)
  1865. return -1;
  1866. /* find the end of the word */
  1867. *end = *begin + 1;
  1868. while (*end < buffer->size && !isspace(buffer->ptr[*end]))
  1869. (*end)++;
  1870. return 0;
  1871. }
  1872. /*
  1873. * This function splits the words in buffer->text, stores the list with
  1874. * newline separator into out, and saves the offsets of the original words
  1875. * in buffer->orig.
  1876. */
  1877. static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
  1878. regex_t *word_regex)
  1879. {
  1880. int i, j;
  1881. long alloc = 0;
  1882. out->size = 0;
  1883. out->ptr = NULL;
  1884. /* fake an empty "0th" word */
  1885. ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
  1886. buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
  1887. buffer->orig_nr = 1;
  1888. for (i = 0; i < buffer->text.size; i++) {
  1889. if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
  1890. return;
  1891. /* store original boundaries */
  1892. ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
  1893. buffer->orig_alloc);
  1894. buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
  1895. buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
  1896. buffer->orig_nr++;
  1897. /* store one word */
  1898. ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
  1899. memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
  1900. out->ptr[out->size + j - i] = '\n';
  1901. out->size += j - i + 1;
  1902. i = j - 1;
  1903. }
  1904. }
  1905. /* this executes the word diff on the accumulated buffers */
  1906. static void diff_words_show(struct diff_words_data *diff_words)
  1907. {
  1908. xpparam_t xpp;
  1909. xdemitconf_t xecfg;
  1910. mmfile_t minus, plus;
  1911. struct diff_words_style *style = diff_words->style;
  1912. struct diff_options *opt = diff_words->opt;
  1913. const char *line_prefix;
  1914. assert(opt);
  1915. line_prefix = diff_line_prefix(opt);
  1916. /* special case: only removal */
  1917. if (!diff_words->plus.text.size) {
  1918. emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
  1919. line_prefix, strlen(line_prefix), 0);
  1920. fn_out_diff_words_write_helper(diff_words->opt,
  1921. &style->old_word, style->newline,
  1922. diff_words->minus.text.size,
  1923. diff_words->minus.text.ptr);
  1924. diff_words->minus.text.size = 0;
  1925. return;
  1926. }
  1927. diff_words->current_plus = diff_words->plus.text.ptr;
  1928. diff_words->last_minus = 0;
  1929. memset(&xpp, 0, sizeof(xpp));
  1930. memset(&xecfg, 0, sizeof(xecfg));
  1931. diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
  1932. diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
  1933. xpp.flags = 0;
  1934. /* as only the hunk header will be parsed, we need a 0-context */
  1935. xecfg.ctxlen = 0;
  1936. if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL,
  1937. diff_words, &xpp, &xecfg))
  1938. die("unable to generate word diff");
  1939. free(minus.ptr);
  1940. free(plus.ptr);
  1941. if (diff_words->current_plus != diff_words->plus.text.ptr +
  1942. diff_words->plus.text.size) {
  1943. if (color_words_output_graph_prefix(diff_words))
  1944. emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
  1945. line_prefix, strlen(line_prefix), 0);
  1946. fn_out_diff_words_write_helper(diff_words->opt,
  1947. &style->ctx, style->newline,
  1948. diff_words->plus.text.ptr + diff_words->plus.text.size
  1949. - diff_words->current_plus, diff_words->current_plus);
  1950. }
  1951. diff_words->minus.text.size = diff_words->plus.text.size = 0;
  1952. }
  1953. /* In "color-words" mode, show word-diff of words accumulated in the buffer */
  1954. static void diff_words_flush(struct emit_callback *ecbdata)
  1955. {
  1956. struct diff_options *wo = ecbdata->diff_words->opt;
  1957. if (ecbdata->diff_words->minus.text.size ||
  1958. ecbdata->diff_words->plus.text.size)
  1959. diff_words_show(ecbdata->diff_words);
  1960. if (wo->emitted_symbols) {
  1961. struct diff_options *o = ecbdata->opt;
  1962. struct emitted_diff_symbols *wol = wo->emitted_symbols;
  1963. int i;
  1964. /*
  1965. * NEEDSWORK:
  1966. * Instead of appending each, concat all words to a line?
  1967. */
  1968. for (i = 0; i < wol->nr; i++)
  1969. append_emitted_diff_symbol(o, &wol->buf[i]);
  1970. for (i = 0; i < wol->nr; i++)
  1971. free((void *)wol->buf[i].line);
  1972. wol->nr = 0;
  1973. }
  1974. }
  1975. static void diff_filespec_load_driver(struct diff_filespec *one,
  1976. struct index_state *istate)
  1977. {
  1978. /* Use already-loaded driver */
  1979. if (one->driver)
  1980. return;
  1981. if (S_ISREG(one->mode))
  1982. one->driver = userdiff_find_by_path(istate, one->path);
  1983. /* Fallback to default settings */
  1984. if (!one->driver)
  1985. one->driver = userdiff_find_by_name("default");
  1986. }
  1987. static const char *userdiff_word_regex(struct diff_filespec *one,
  1988. struct index_state *istate)
  1989. {
  1990. diff_filespec_load_driver(one, istate);
  1991. return one->driver->word_regex;
  1992. }
  1993. static void init_diff_words_data(struct emit_callback *ecbdata,
  1994. struct diff_options *orig_opts,
  1995. struct diff_filespec *one,
  1996. struct diff_filespec *two)
  1997. {
  1998. int i;
  1999. struct diff_options *o = xmalloc(sizeof(struct diff_options));
  2000. memcpy(o, orig_opts, sizeof(struct diff_options));
  2001. ecbdata->diff_words =
  2002. xcalloc(1, sizeof(struct diff_words_data));
  2003. ecbdata->diff_words->type = o->word_diff;
  2004. ecbdata->diff_words->opt = o;
  2005. if (orig_opts->emitted_symbols)
  2006. o->emitted_symbols =
  2007. xcalloc(1, sizeof(struct emitted_diff_symbols));
  2008. if (!o->word_regex)
  2009. o->word_regex = userdiff_word_regex(one, o->repo->index);
  2010. if (!o->word_regex)
  2011. o->word_regex = userdiff_word_regex(two, o->repo->index);
  2012. if (!o->word_regex)
  2013. o->word_regex = diff_word_regex_cfg;
  2014. if (o->word_regex) {
  2015. ecbdata->diff_words->word_regex = (regex_t *)
  2016. xmalloc(sizeof(regex_t));
  2017. if (regcomp(ecbdata->diff_words->word_regex,
  2018. o->word_regex,
  2019. REG_EXTENDED | REG_NEWLINE))
  2020. die("invalid regular expression: %s",
  2021. o->word_regex);
  2022. }
  2023. for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
  2024. if (o->word_diff == diff_words_styles[i].type) {
  2025. ecbdata->diff_words->style =
  2026. &diff_words_styles[i];
  2027. break;
  2028. }
  2029. }
  2030. if (want_color(o->use_color)) {
  2031. struct diff_words_style *st = ecbdata->diff_words->style;
  2032. st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD);
  2033. st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW);
  2034. st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
  2035. }
  2036. }
  2037. static void free_diff_words_data(struct emit_callback *ecbdata)
  2038. {
  2039. if (ecbdata->diff_words) {
  2040. diff_words_flush(ecbdata);
  2041. free (ecbdata->diff_words->opt->emitted_symbols);
  2042. free (ecbdata->diff_words->opt);
  2043. free (ecbdata->diff_words->minus.text.ptr);
  2044. free (ecbdata->diff_words->minus.orig);
  2045. free (ecbdata->diff_words->plus.text.ptr);
  2046. free (ecbdata->diff_words->plus.orig);
  2047. if (ecbdata->diff_words->word_regex) {
  2048. regfree(ecbdata->diff_words->word_regex);
  2049. free(ecbdata->diff_words->word_regex);
  2050. }
  2051. FREE_AND_NULL(ecbdata->diff_words);
  2052. }
  2053. }
  2054. const char *diff_get_color(int diff_use_color, enum color_diff ix)
  2055. {
  2056. if (want_color(diff_use_color))
  2057. return diff_colors[ix];
  2058. return "";
  2059. }
  2060. const char *diff_line_prefix(struct diff_options *opt)
  2061. {
  2062. struct strbuf *msgbuf;
  2063. if (!opt->output_prefix)
  2064. return "";
  2065. msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
  2066. return msgbuf->buf;
  2067. }
  2068. static unsigned long sane_truncate_line(char *line, unsigned long len)
  2069. {
  2070. const char *cp;
  2071. unsigned long allot;
  2072. size_t l = len;
  2073. cp = line;
  2074. allot = l;
  2075. while (0 < l) {
  2076. (void) utf8_width(&cp, &l);
  2077. if (!cp)
  2078. break; /* truncated in the middle? */
  2079. }
  2080. return allot - l;
  2081. }
  2082. static void find_lno(const char *line, struct emit_callback *ecbdata)
  2083. {
  2084. const char *p;
  2085. ecbdata->lno_in_preimage = 0;
  2086. ecbdata->lno_in_postimage = 0;
  2087. p = strchr(line, '-');
  2088. if (!p)
  2089. return; /* cannot happen */
  2090. ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
  2091. p = strchr(p, '+');
  2092. if (!p)
  2093. return; /* cannot happen */
  2094. ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
  2095. }
  2096. static void fn_out_consume(void *priv, char *line, unsigned long len)
  2097. {
  2098. struct emit_callback *ecbdata = priv;
  2099. struct diff_options *o = ecbdata->opt;
  2100. o->found_changes = 1;
  2101. if (ecbdata->header) {
  2102. emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
  2103. ecbdata->header->buf, ecbdata->header->len, 0);
  2104. strbuf_reset(ecbdata->header);
  2105. ecbdata->header = NULL;
  2106. }
  2107. if (ecbdata->label_path[0]) {
  2108. emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
  2109. ecbdata->label_path[0],
  2110. strlen(ecbdata->label_path[0]), 0);
  2111. emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
  2112. ecbdata->label_path[1],
  2113. strlen(ecbdata->label_path[1]), 0);
  2114. ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
  2115. }
  2116. if (diff_suppress_blank_empty
  2117. && len == 2 && line[0] == ' ' && line[1] == '\n') {
  2118. line[0] = '\n';
  2119. len = 1;
  2120. }
  2121. if (line[0] == '@') {
  2122. if (ecbdata->diff_words)
  2123. diff_words_flush(ecbdata);
  2124. len = sane_truncate_line(line, len);
  2125. find_lno(line, ecbdata);
  2126. emit_hunk_header(ecbdata, line, len);
  2127. return;
  2128. }
  2129. if (ecbdata->diff_words) {
  2130. enum diff_symbol s =
  2131. ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
  2132. DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
  2133. if (line[0] == '-') {
  2134. diff_words_append(line, len,
  2135. &ecbdata->diff_words->minus);
  2136. return;
  2137. } else if (line[0] == '+') {
  2138. diff_words_append(line, len,
  2139. &ecbdata->diff_words->plus);
  2140. return;
  2141. } else if (starts_with(line, "\\ ")) {
  2142. /*
  2143. * Eat the "no newline at eof" marker as if we
  2144. * saw a "+" or "-" line with nothing on it,
  2145. * and return without diff_words_flush() to
  2146. * defer processing. If this is the end of
  2147. * preimage, more "+" lines may come after it.
  2148. */
  2149. return;
  2150. }
  2151. diff_words_flush(ecbdata);
  2152. emit_diff_symbol(o, s, line, len, 0);
  2153. return;
  2154. }
  2155. switch (line[0]) {
  2156. case '+':
  2157. ecbdata->lno_in_postimage++;
  2158. emit_add_line(ecbdata, line + 1, len - 1);
  2159. break;
  2160. case '-':
  2161. ecbdata->lno_in_preimage++;
  2162. emit_del_line(ecbdata, line + 1, len - 1);
  2163. break;
  2164. case ' ':
  2165. ecbdata->lno_in_postimage++;
  2166. ecbdata->lno_in_preimage++;
  2167. emit_context_line(ecbdata, line + 1, len - 1);
  2168. break;
  2169. default:
  2170. /* incomplete line at the end */
  2171. ecbdata->lno_in_preimage++;
  2172. emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
  2173. line, len, 0);
  2174. break;
  2175. }
  2176. }
  2177. static void pprint_rename(struct strbuf *name, const char *a, const char *b)
  2178. {
  2179. const char *old_name = a;
  2180. const char *new_name = b;
  2181. int pfx_length, sfx_length;
  2182. int pfx_adjust_for_slash;
  2183. int len_a = strlen(a);
  2184. int len_b = strlen(b);
  2185. int a_midlen, b_midlen;
  2186. int qlen_a = quote_c_style(a, NULL, NULL, 0);
  2187. int qlen_b = quote_c_style(b, NULL, NULL, 0);
  2188. if (qlen_a || qlen_b) {
  2189. quote_c_style(a, name, NULL, 0);
  2190. strbuf_addstr(name, " => ");
  2191. quote_c_style(b, name, NULL, 0);
  2192. return;
  2193. }
  2194. /* Find common prefix */
  2195. pfx_length = 0;
  2196. while (*old_name && *new_name && *old_name == *new_name) {
  2197. if (*old_name == '/')
  2198. pfx_length = old_name - a + 1;
  2199. old_name++;
  2200. new_name++;
  2201. }
  2202. /* Find common suffix */
  2203. old_name = a + len_a;
  2204. new_name = b + len_b;
  2205. sfx_length = 0;
  2206. /*
  2207. * If there is a common prefix, it must end in a slash. In
  2208. * that case we let this loop run 1 into the prefix to see the
  2209. * same slash.
  2210. *
  2211. * If there is no common prefix, we cannot do this as it would
  2212. * underrun the input strings.
  2213. */
  2214. pfx_adjust_for_slash = (pfx_length ? 1 : 0);
  2215. while (a + pfx_length - pfx_adjust_for_slash <= old_name &&
  2216. b + pfx_length - pfx_adjust_for_slash <= new_name &&
  2217. *old_name == *new_name) {
  2218. if (*old_name == '/')
  2219. sfx_length = len_a - (old_name - a);
  2220. old_name--;
  2221. new_name--;
  2222. }
  2223. /*
  2224. * pfx{mid-a => mid-b}sfx
  2225. * {pfx-a => pfx-b}sfx
  2226. * pfx{sfx-a => sfx-b}
  2227. * name-a => name-b
  2228. */
  2229. a_midlen = len_a - pfx_length - sfx_length;
  2230. b_midlen = len_b - pfx_length - sfx_length;
  2231. if (a_midlen < 0)
  2232. a_midlen = 0;
  2233. if (b_midlen < 0)
  2234. b_midlen = 0;
  2235. strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
  2236. if (pfx_length + sfx_length) {
  2237. strbuf_add(name, a, pfx_length);
  2238. strbuf_addch(name, '{');
  2239. }
  2240. strbuf_add(name, a + pfx_length, a_midlen);
  2241. strbuf_addstr(name, " => ");
  2242. strbuf_add(name, b + pfx_length, b_midlen);
  2243. if (pfx_length + sfx_length) {
  2244. strbuf_addch(name, '}');
  2245. strbuf_add(name, a + len_a - sfx_length, sfx_length);
  2246. }
  2247. }
  2248. static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
  2249. const char *name_a,
  2250. const char *name_b)
  2251. {
  2252. struct diffstat_file *x;
  2253. x = xcalloc(1, sizeof(*x));
  2254. ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
  2255. diffstat->files[diffstat->nr++] = x;
  2256. if (name_b) {
  2257. x->from_name = xstrdup(name_a);
  2258. x->name = xstrdup(name_b);
  2259. x->is_renamed = 1;
  2260. }
  2261. else {
  2262. x->from_name = NULL;
  2263. x->name = xstrdup(name_a);
  2264. }
  2265. return x;
  2266. }
  2267. static void diffstat_consume(void *priv, char *line, unsigned long len)
  2268. {
  2269. struct diffstat_t *diffstat = priv;
  2270. struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
  2271. if (line[0] == '+')
  2272. x->added++;
  2273. else if (line[0] == '-')
  2274. x->deleted++;
  2275. }
  2276. const char mime_boundary_leader[] = "------------";
  2277. static int scale_linear(int it, int width, int max_change)
  2278. {
  2279. if (!it)
  2280. return 0;
  2281. /*
  2282. * make sure that at least one '-' or '+' is printed if
  2283. * there is any change to this path. The easiest way is to
  2284. * scale linearly as if the allotted width is one column shorter
  2285. * than it is, and then add 1 to the result.
  2286. */
  2287. return 1 + (it * (width - 1) / max_change);
  2288. }
  2289. static void show_graph(struct strbuf *out, char ch, int cnt,
  2290. const char *set, const char *reset)
  2291. {
  2292. if (cnt <= 0)
  2293. return;
  2294. strbuf_addstr(out, set);
  2295. strbuf_addchars(out, ch, cnt);
  2296. strbuf_addstr(out, reset);
  2297. }
  2298. static void fill_print_name(struct diffstat_file *file)
  2299. {
  2300. struct strbuf pname = STRBUF_INIT;
  2301. if (file->print_name)
  2302. return;
  2303. if (file->is_renamed)
  2304. pprint_rename(&pname, file->from_name, file->name);
  2305. else
  2306. quote_c_style(file->name, &pname, NULL, 0);
  2307. if (file->comments)
  2308. strbuf_addf(&pname, " (%s)", file->comments);
  2309. file->print_name = strbuf_detach(&pname, NULL);
  2310. }
  2311. static void print_stat_summary_inserts_deletes(struct diff_options *options,
  2312. int files, int insertions, int deletions)
  2313. {
  2314. struct strbuf sb = STRBUF_INIT;
  2315. if (!files) {
  2316. assert(insertions == 0 && deletions == 0);
  2317. emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
  2318. NULL, 0, 0);
  2319. return;
  2320. }
  2321. strbuf_addf(&sb,
  2322. (files == 1) ? " %d file changed" : " %d files changed",
  2323. files);
  2324. /*
  2325. * For binary diff, the caller may want to print "x files
  2326. * changed" with insertions == 0 && deletions == 0.
  2327. *
  2328. * Not omitting "0 insertions(+), 0 deletions(-)" in this case
  2329. * is probably less confusing (i.e skip over "2 files changed
  2330. * but nothing about added/removed lines? Is this a bug in Git?").
  2331. */
  2332. if (insertions || deletions == 0) {
  2333. strbuf_addf(&sb,
  2334. (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
  2335. insertions);
  2336. }
  2337. if (deletions || insertions == 0) {
  2338. strbuf_addf(&sb,
  2339. (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
  2340. deletions);
  2341. }
  2342. strbuf_addch(&sb, '\n');
  2343. emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
  2344. sb.buf, sb.len, 0);
  2345. strbuf_release(&sb);
  2346. }
  2347. void print_stat_summary(FILE *fp, int files,
  2348. int insertions, int deletions)
  2349. {
  2350. struct diff_options o;
  2351. memset(&o, 0, sizeof(o));
  2352. o.file = fp;
  2353. print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
  2354. }
  2355. static void show_stats(struct diffstat_t *data, struct diff_options *options)
  2356. {
  2357. int i, len, add, del, adds = 0, dels = 0;
  2358. uintmax_t max_change = 0, max_len = 0;
  2359. int total_files = data->nr, count;
  2360. int width, name_width, graph_width, number_width = 0, bin_width = 0;
  2361. const char *reset, *add_c, *del_c;
  2362. int extra_shown = 0;
  2363. const char *line_prefix = diff_line_prefix(options);
  2364. struct strbuf out = STRBUF_INIT;
  2365. if (data->nr == 0)
  2366. return;
  2367. count = options->stat_count ? options->stat_count : data->nr;
  2368. reset = diff_get_color_opt(options, DIFF_RESET);
  2369. add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
  2370. del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
  2371. /*
  2372. * Find the longest filename and max number of changes
  2373. */
  2374. for (i = 0; (i < count) && (i < data->nr); i++) {
  2375. struct diffstat_file *file = data->files[i];
  2376. uintmax_t change = file->added + file->deleted;
  2377. if (!file->is_interesting && (change == 0)) {
  2378. count++; /* not shown == room for one more */
  2379. continue;
  2380. }
  2381. fill_print_name(file);
  2382. len = strlen(file->print_name);
  2383. if (max_len < len)
  2384. max_len = len;
  2385. if (file->is_unmerged) {
  2386. /* "Unmerged" is 8 characters */
  2387. bin_width = bin_width < 8 ? 8 : bin_width;
  2388. continue;
  2389. }
  2390. if (file->is_binary) {
  2391. /* "Bin XXX -> YYY bytes" */
  2392. int w = 14 + decimal_width(file->added)
  2393. + decimal_width(file->deleted);
  2394. bin_width = bin_width < w ? w : bin_width;
  2395. /* Display change counts aligned with "Bin" */
  2396. number_width = 3;
  2397. continue;
  2398. }
  2399. if (max_change < change)
  2400. max_change = change;
  2401. }
  2402. count = i; /* where we can stop scanning in data->files[] */
  2403. /*
  2404. * We have width = stat_width or term_columns() columns total.
  2405. * We want a maximum of min(max_len, stat_name_width) for the name part.
  2406. * We want a maximum of min(max_change, stat_graph_width) for the +- part.
  2407. * We also need 1 for " " and 4 + decimal_width(max_change)
  2408. * for " | NNNN " and one the empty column at the end, altogether
  2409. * 6 + decimal_width(max_change).
  2410. *
  2411. * If there's not enough space, we will use the smaller of
  2412. * stat_name_width (if set) and 5/8*width for the filename,
  2413. * and the rest for constant elements + graph part, but no more
  2414. * than stat_graph_width for the graph part.
  2415. * (5/8 gives 50 for filename and 30 for the constant parts + graph
  2416. * for the standard terminal size).
  2417. *
  2418. * In other words: stat_width limits the maximum width, and
  2419. * stat_name_width fixes the maximum width of the filename,
  2420. * and is also used to divide available columns if there
  2421. * aren't enough.
  2422. *
  2423. * Binary files are displayed with "Bin XXX -> YYY bytes"
  2424. * instead of the change count and graph. This part is treated
  2425. * similarly to the graph part, except that it is not
  2426. * "scaled". If total width is too small to accommodate the
  2427. * guaranteed minimum width of the filename part and the
  2428. * separators and this message, this message will "overflow"
  2429. * making the line longer than the maximum width.
  2430. */
  2431. if (options->stat_width == -1)
  2432. width = term_columns() - strlen(line_prefix);
  2433. else
  2434. width = options->stat_width ? options->stat_width : 80;
  2435. number_width = decimal_width(max_change) > number_width ?
  2436. decimal_width(max_change) : number_width;
  2437. if (options->stat_graph_width == -1)
  2438. options->stat_graph_width = diff_stat_graph_width;
  2439. /*
  2440. * Guarantee 3/8*16==6 for the graph part
  2441. * and 5/8*16==10 for the filename part
  2442. */
  2443. if (width < 16 + 6 + number_width)
  2444. width = 16 + 6 + number_width;
  2445. /*
  2446. * First assign sizes that are wanted, ignoring available width.
  2447. * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
  2448. * starting from "XXX" should fit in graph_width.
  2449. */
  2450. graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
  2451. if (options->stat_graph_width &&
  2452. options->stat_graph_width < graph_width)
  2453. graph_width = options->stat_graph_width;
  2454. name_width = (options->stat_name_width > 0 &&
  2455. options->stat_name_width < max_len) ?
  2456. options->stat_name_width : max_len;
  2457. /*
  2458. * Adjust adjustable widths not to exceed maximum width
  2459. */
  2460. if (name_width + number_width + 6 + graph_width > width) {
  2461. if (graph_width > width * 3/8 - number_width - 6) {
  2462. graph_width = width * 3/8 - number_width - 6;
  2463. if (graph_width < 6)
  2464. graph_width = 6;
  2465. }
  2466. if (options->stat_graph_width &&
  2467. graph_width > options->stat_graph_width)
  2468. graph_width = options->stat_graph_width;
  2469. if (name_width > width - number_width - 6 - graph_width)
  2470. name_width = width - number_width - 6 - graph_width;
  2471. else
  2472. graph_width = width - number_width - 6 - name_width;
  2473. }
  2474. /*
  2475. * From here name_width is the width of the name area,
  2476. * and graph_width is the width of the graph area.
  2477. * max_change is used to scale graph properly.
  2478. */
  2479. for (i = 0; i < count; i++) {
  2480. const char *prefix = "";
  2481. struct diffstat_file *file = data->files[i];
  2482. char *name = file->print_name;
  2483. uintmax_t added = file->added;
  2484. uintmax_t deleted = file->deleted;
  2485. int name_len;
  2486. if (!file->is_interesting && (added + deleted == 0))
  2487. continue;
  2488. /*
  2489. * "scale" the filename
  2490. */
  2491. len = name_width;
  2492. name_len = strlen(name);
  2493. if (name_width < name_len) {
  2494. char *slash;
  2495. prefix = "...";
  2496. len -= 3;
  2497. name += name_len - len;
  2498. slash = strchr(name, '/');
  2499. if (slash)
  2500. name = slash;
  2501. }
  2502. if (file->is_binary) {
  2503. strbuf_addf(&out, " %s%-*s |", prefix, len, name);
  2504. strbuf_addf(&out, " %*s", number_width, "Bin");
  2505. if (!added && !deleted) {
  2506. strbuf_addch(&out, '\n');
  2507. emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
  2508. out.buf, out.len, 0);
  2509. strbuf_reset(&out);
  2510. continue;
  2511. }
  2512. strbuf_addf(&out, " %s%"PRIuMAX"%s",
  2513. del_c, deleted, reset);
  2514. strbuf_addstr(&out, " -> ");
  2515. strbuf_addf(&out, "%s%"PRIuMAX"%s",
  2516. add_c, added, reset);
  2517. strbuf_addstr(&out, " bytes\n");
  2518. emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
  2519. out.buf, out.len, 0);
  2520. strbuf_reset(&out);
  2521. continue;
  2522. }
  2523. else if (file->is_unmerged) {
  2524. strbuf_addf(&out, " %s%-*s |", prefix, len, name);
  2525. strbuf_addstr(&out, " Unmerged\n");
  2526. emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
  2527. out.buf, out.len, 0);
  2528. strbuf_reset(&out);
  2529. continue;
  2530. }
  2531. /*
  2532. * scale the add/delete
  2533. */
  2534. add = added;
  2535. del = deleted;
  2536. if (graph_width <= max_change) {
  2537. int total = scale_linear(add + del, graph_width, max_change);
  2538. if (total < 2 && add && del)
  2539. /* width >= 2 due to the sanity check */
  2540. total = 2;
  2541. if (add < del) {
  2542. add = scale_linear(add, graph_width, max_change);
  2543. del = total - add;
  2544. } else {
  2545. del = scale_linear(del, graph_width, max_change);
  2546. add = total - del;
  2547. }
  2548. }
  2549. strbuf_addf(&out, " %s%-*s |", prefix, len, name);
  2550. strbuf_addf(&out, " %*"PRIuMAX"%s",
  2551. number_width, added + deleted,
  2552. added + deleted ? " " : "");
  2553. show_graph(&out, '+', add, add_c, reset);
  2554. show_graph(&out, '-', del, del_c, reset);
  2555. strbuf_addch(&out, '\n');
  2556. emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
  2557. out.buf, out.len, 0);
  2558. strbuf_reset(&out);
  2559. }
  2560. for (i = 0; i < data->nr; i++) {
  2561. struct diffstat_file *file = data->files[i];
  2562. uintmax_t added = file->added;
  2563. uintmax_t deleted = file->deleted;
  2564. if (file->is_unmerged ||
  2565. (!file->is_interesting && (added + deleted == 0))) {
  2566. total_files--;
  2567. continue;
  2568. }
  2569. if (!file->is_binary) {
  2570. adds += added;
  2571. dels += deleted;
  2572. }
  2573. if (i < count)
  2574. continue;
  2575. if (!extra_shown)
  2576. emit_diff_symbol(options,
  2577. DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
  2578. NULL, 0, 0);
  2579. extra_shown = 1;
  2580. }
  2581. print_stat_summary_inserts_deletes(options, total_files, adds, dels);
  2582. strbuf_release(&out);
  2583. }
  2584. static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
  2585. {
  2586. int i, adds = 0, dels = 0, total_files = data->nr;
  2587. if (data->nr == 0)
  2588. return;
  2589. for (i = 0; i < data->nr; i++) {
  2590. int added = data->files[i]->added;
  2591. int deleted = data->files[i]->deleted;
  2592. if (data->files[i]->is_unmerged ||
  2593. (!data->files[i]->is_interesting && (added + deleted == 0))) {
  2594. total_files--;
  2595. } else if (!data->files[i]->is_binary) { /* don't count bytes */
  2596. adds += added;
  2597. dels += deleted;
  2598. }
  2599. }
  2600. print_stat_summary_inserts_deletes(options, total_files, adds, dels);
  2601. }
  2602. static void show_numstat(struct diffstat_t *data, struct diff_options *options)
  2603. {
  2604. int i;
  2605. if (data->nr == 0)
  2606. return;
  2607. for (i = 0; i < data->nr; i++) {
  2608. struct diffstat_file *file = data->files[i];
  2609. fprintf(options->file, "%s", diff_line_prefix(options));
  2610. if (file->is_binary)
  2611. fprintf(options->file, "-\t-\t");
  2612. else
  2613. fprintf(options->file,
  2614. "%"PRIuMAX"\t%"PRIuMAX"\t",
  2615. file->added, file->deleted);
  2616. if (options->line_termination) {
  2617. fill_print_name(file);
  2618. if (!file->is_renamed)
  2619. write_name_quoted(file->name, options->file,
  2620. options->line_termination);
  2621. else {
  2622. fputs(file->print_name, options->file);
  2623. putc(options->line_termination, options->file);
  2624. }
  2625. } else {
  2626. if (file->is_renamed) {
  2627. putc('\0', options->file);
  2628. write_name_quoted(file->from_name, options->file, '\0');
  2629. }
  2630. write_name_quoted(file->name, options->file, '\0');
  2631. }
  2632. }
  2633. }
  2634. struct dirstat_file {
  2635. const char *name;
  2636. unsigned long changed;
  2637. };
  2638. struct dirstat_dir {
  2639. struct dirstat_file *files;
  2640. int alloc, nr, permille, cumulative;
  2641. };
  2642. static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
  2643. unsigned long changed, const char *base, int baselen)
  2644. {
  2645. unsigned long sum_changes = 0;
  2646. unsigned int sources = 0;
  2647. const char *line_prefix = diff_line_prefix(opt);
  2648. while (dir->nr) {
  2649. struct dirstat_file *f = dir->files;
  2650. int namelen = strlen(f->name);
  2651. unsigned long changes;
  2652. char *slash;
  2653. if (namelen < baselen)
  2654. break;
  2655. if (memcmp(f->name, base, baselen))
  2656. break;
  2657. slash = strchr(f->name + baselen, '/');
  2658. if (slash) {
  2659. int newbaselen = slash + 1 - f->name;
  2660. changes = gather_dirstat(opt, dir, changed, f->name, newbaselen);
  2661. sources++;
  2662. } else {
  2663. changes = f->changed;
  2664. dir->files++;
  2665. dir->nr--;
  2666. sources += 2;
  2667. }
  2668. sum_changes += changes;
  2669. }
  2670. /*
  2671. * We don't report dirstat's for
  2672. * - the top level
  2673. * - or cases where everything came from a single directory
  2674. * under this directory (sources == 1).
  2675. */
  2676. if (baselen && sources != 1) {
  2677. if (sum_changes) {
  2678. int permille = sum_changes * 1000 / changed;
  2679. if (permille >= dir->permille) {
  2680. fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
  2681. permille / 10, permille % 10, baselen, base);
  2682. if (!dir->cumulative)
  2683. return 0;
  2684. }
  2685. }
  2686. }
  2687. return sum_changes;
  2688. }
  2689. static int dirstat_compare(const void *_a, const void *_b)
  2690. {
  2691. const struct dirstat_file *a = _a;
  2692. const struct dirstat_file *b = _b;
  2693. return strcmp(a->name, b->name);
  2694. }
  2695. static void show_dirstat(struct diff_options *options)
  2696. {
  2697. int i;
  2698. unsigned long changed;
  2699. struct dirstat_dir dir;
  2700. struct diff_queue_struct *q = &diff_queued_diff;
  2701. dir.files = NULL;
  2702. dir.alloc = 0;
  2703. dir.nr = 0;
  2704. dir.permille = options->dirstat_permille;
  2705. dir.cumulative = options->flags.dirstat_cumulative;
  2706. changed = 0;
  2707. for (i = 0; i < q->nr; i++) {
  2708. struct diff_filepair *p = q->queue[i];
  2709. const char *name;
  2710. unsigned long copied, added, damage;
  2711. struct diff_populate_filespec_options dpf_options = {
  2712. .check_size_only = 1,
  2713. };
  2714. name = p->two->path ? p->two->path : p->one->path;
  2715. if (p->one->oid_valid && p->two->oid_valid &&
  2716. oideq(&p->one->oid, &p->two->oid)) {
  2717. /*
  2718. * The SHA1 has not changed, so pre-/post-content is
  2719. * identical. We can therefore skip looking at the
  2720. * file contents altogether.
  2721. */
  2722. damage = 0;
  2723. goto found_damage;
  2724. }
  2725. if (options->flags.dirstat_by_file) {
  2726. /*
  2727. * In --dirstat-by-file mode, we don't really need to
  2728. * look at the actual file contents at all.
  2729. * The fact that the SHA1 changed is enough for us to
  2730. * add this file to the list of results
  2731. * (with each file contributing equal damage).
  2732. */
  2733. damage = 1;
  2734. goto found_damage;
  2735. }
  2736. if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
  2737. diff_populate_filespec(options->repo, p->one, NULL);
  2738. diff_populate_filespec(options->repo, p->two, NULL);
  2739. diffcore_count_changes(options->repo,
  2740. p->one, p->two, NULL, NULL,
  2741. &copied, &added);
  2742. diff_free_filespec_data(p->one);
  2743. diff_free_filespec_data(p->two);
  2744. } else if (DIFF_FILE_VALID(p->one)) {
  2745. diff_populate_filespec(options->repo, p->one, &dpf_options);
  2746. copied = added = 0;
  2747. diff_free_filespec_data(p->one);
  2748. } else if (DIFF_FILE_VALID(p->two)) {
  2749. diff_populate_filespec(options->repo, p->two, &dpf_options);
  2750. copied = 0;
  2751. added = p->two->size;
  2752. diff_free_filespec_data(p->two);
  2753. } else
  2754. continue;
  2755. /*
  2756. * Original minus copied is the removed material,
  2757. * added is the new material. They are both damages
  2758. * made to the preimage.
  2759. * If the resulting damage is zero, we know that
  2760. * diffcore_count_changes() considers the two entries to
  2761. * be identical, but since the oid changed, we
  2762. * know that there must have been _some_ kind of change,
  2763. * so we force all entries to have damage > 0.
  2764. */
  2765. damage = (p->one->size - copied) + added;
  2766. if (!damage)
  2767. damage = 1;
  2768. found_damage:
  2769. ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
  2770. dir.files[dir.nr].name = name;
  2771. dir.files[dir.nr].changed = damage;
  2772. changed += damage;
  2773. dir.nr++;
  2774. }
  2775. /* This can happen even with many files, if everything was renames */
  2776. if (!changed)
  2777. return;
  2778. /* Show all directories with more than x% of the changes */
  2779. QSORT(dir.files, dir.nr, dirstat_compare);
  2780. gather_dirstat(options, &dir, changed, "", 0);
  2781. }
  2782. static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
  2783. {
  2784. int i;
  2785. unsigned long changed;
  2786. struct dirstat_dir dir;
  2787. if (data->nr == 0)
  2788. return;
  2789. dir.files = NULL;
  2790. dir.alloc = 0;
  2791. dir.nr = 0;
  2792. dir.permille = options->dirstat_permille;
  2793. dir.cumulative = options->flags.dirstat_cumulative;
  2794. changed = 0;
  2795. for (i = 0; i < data->nr; i++) {
  2796. struct diffstat_file *file = data->files[i];
  2797. unsigned long damage = file->added + file->deleted;
  2798. if (file->is_binary)
  2799. /*
  2800. * binary files counts bytes, not lines. Must find some
  2801. * way to normalize binary bytes vs. textual lines.
  2802. * The following heuristic assumes that there are 64
  2803. * bytes per "line".
  2804. * This is stupid and ugly, but very cheap...
  2805. */
  2806. damage = DIV_ROUND_UP(damage, 64);
  2807. ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
  2808. dir.files[dir.nr].name = file->name;
  2809. dir.files[dir.nr].changed = damage;
  2810. changed += damage;
  2811. dir.nr++;
  2812. }
  2813. /* This can happen even with many files, if everything was renames */
  2814. if (!changed)
  2815. return;
  2816. /* Show all directories with more than x% of the changes */
  2817. QSORT(dir.files, dir.nr, dirstat_compare);
  2818. gather_dirstat(options, &dir, changed, "", 0);
  2819. }
  2820. static void free_diffstat_file(struct diffstat_file *f)
  2821. {
  2822. free(f->print_name);
  2823. free(f->name);
  2824. free(f->from_name);
  2825. free(f);
  2826. }
  2827. void free_diffstat_info(struct diffstat_t *diffstat)
  2828. {
  2829. int i;
  2830. for (i = 0; i < diffstat->nr; i++)
  2831. free_diffstat_file(diffstat->files[i]);
  2832. free(diffstat->files);
  2833. }
  2834. struct checkdiff_t {
  2835. const char *filename;
  2836. int lineno;
  2837. int conflict_marker_size;
  2838. struct diff_options *o;
  2839. unsigned ws_rule;
  2840. unsigned status;
  2841. };
  2842. static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
  2843. {
  2844. char firstchar;
  2845. int cnt;
  2846. if (len < marker_size + 1)
  2847. return 0;
  2848. firstchar = line[0];
  2849. switch (firstchar) {
  2850. case '=': case '>': case '<': case '|':
  2851. break;
  2852. default:
  2853. return 0;
  2854. }
  2855. for (cnt = 1; cnt < marker_size; cnt++)
  2856. if (line[cnt] != firstchar)
  2857. return 0;
  2858. /* line[1] through line[marker_size-1] are same as firstchar */
  2859. if (len < marker_size + 1 || !isspace(line[marker_size]))
  2860. return 0;
  2861. return 1;
  2862. }
  2863. static void checkdiff_consume_hunk(void *priv,
  2864. long ob, long on, long nb, long nn,
  2865. const char *func, long funclen)
  2866. {
  2867. struct checkdiff_t *data = priv;
  2868. data->lineno = nb - 1;
  2869. }
  2870. static void checkdiff_consume(void *priv, char *line, unsigned long len)
  2871. {
  2872. struct checkdiff_t *data = priv;
  2873. int marker_size = data->conflict_marker_size;
  2874. const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
  2875. const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
  2876. const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
  2877. char *err;
  2878. const char *line_prefix;
  2879. assert(data->o);
  2880. line_prefix = diff_line_prefix(data->o);
  2881. if (line[0] == '+') {
  2882. unsigned bad;
  2883. data->lineno++;
  2884. if (is_conflict_marker(line + 1, marker_size, len - 1)) {
  2885. data->status |= 1;
  2886. fprintf(data->o->file,
  2887. "%s%s:%d: leftover conflict marker\n",
  2888. line_prefix, data->filename, data->lineno);
  2889. }
  2890. bad = ws_check(line + 1, len - 1, data->ws_rule);
  2891. if (!bad)
  2892. return;
  2893. data->status |= bad;
  2894. err = whitespace_error_string(bad);
  2895. fprintf(data->o->file, "%s%s:%d: %s.\n",
  2896. line_prefix, data->filename, data->lineno, err);
  2897. free(err);
  2898. emit_line(data->o, set, reset, line, 1);
  2899. ws_check_emit(line + 1, len - 1, data->ws_rule,
  2900. data->o->file, set, reset, ws);
  2901. } else if (line[0] == ' ') {
  2902. data->lineno++;
  2903. }
  2904. }
  2905. static unsigned char *deflate_it(char *data,
  2906. unsigned long size,
  2907. unsigned long *result_size)
  2908. {
  2909. int bound;
  2910. unsigned char *deflated;
  2911. git_zstream stream;
  2912. git_deflate_init(&stream, zlib_compression_level);
  2913. bound = git_deflate_bound(&stream, size);
  2914. deflated = xmalloc(bound);
  2915. stream.next_out = deflated;
  2916. stream.avail_out = bound;
  2917. stream.next_in = (unsigned char *)data;
  2918. stream.avail_in = size;
  2919. while (git_deflate(&stream, Z_FINISH) == Z_OK)
  2920. ; /* nothing */
  2921. git_deflate_end(&stream);
  2922. *result_size = stream.total_out;
  2923. return deflated;
  2924. }
  2925. static void emit_binary_diff_body(struct diff_options *o,
  2926. mmfile_t *one, mmfile_t *two)
  2927. {
  2928. void *cp;
  2929. void *delta;
  2930. void *deflated;
  2931. void *data;
  2932. unsigned long orig_size;
  2933. unsigned long delta_size;
  2934. unsigned long deflate_size;
  2935. unsigned long data_size;
  2936. /* We could do deflated delta, or we could do just deflated two,
  2937. * whichever is smaller.
  2938. */
  2939. delta = NULL;
  2940. deflated = deflate_it(two->ptr, two->size, &deflate_size);
  2941. if (one->size && two->size) {
  2942. delta = diff_delta(one->ptr, one->size,
  2943. two->ptr, two->size,
  2944. &delta_size, deflate_size);
  2945. if (delta) {
  2946. void *to_free = delta;
  2947. orig_size = delta_size;
  2948. delta = deflate_it(delta, delta_size, &delta_size);
  2949. free(to_free);
  2950. }
  2951. }
  2952. if (delta && delta_size < deflate_size) {
  2953. char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size);
  2954. emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
  2955. s, strlen(s), 0);
  2956. free(s);
  2957. free(deflated);
  2958. data = delta;
  2959. data_size = delta_size;
  2960. } else {
  2961. char *s = xstrfmt("%lu", two->size);
  2962. emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
  2963. s, strlen(s), 0);
  2964. free(s);
  2965. free(delta);
  2966. data = deflated;
  2967. data_size = deflate_size;
  2968. }
  2969. /* emit data encoded in base85 */
  2970. cp = data;
  2971. while (data_size) {
  2972. int len;
  2973. int bytes = (52 < data_size) ? 52 : data_size;
  2974. char line[71];
  2975. data_size -= bytes;
  2976. if (bytes <= 26)
  2977. line[0] = bytes + 'A' - 1;
  2978. else
  2979. line[0] = bytes - 26 + 'a' - 1;
  2980. encode_85(line + 1, cp, bytes);
  2981. cp = (char *) cp + bytes;
  2982. len = strlen(line);
  2983. line[len++] = '\n';
  2984. line[len] = '\0';
  2985. emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
  2986. line, len, 0);
  2987. }
  2988. emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
  2989. free(data);
  2990. }
  2991. static void emit_binary_diff(struct diff_options *o,
  2992. mmfile_t *one, mmfile_t *two)
  2993. {
  2994. emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
  2995. emit_binary_diff_body(o, one, two);
  2996. emit_binary_diff_body(o, two, one);
  2997. }
  2998. int diff_filespec_is_binary(struct repository *r,
  2999. struct diff_filespec *one)
  3000. {
  3001. struct diff_populate_filespec_options dpf_options = {
  3002. .check_binary = 1,
  3003. };
  3004. if (one->is_binary == -1) {
  3005. diff_filespec_load_driver(one, r->index);
  3006. if (one->driver->binary != -1)
  3007. one->is_binary = one->driver->binary;
  3008. else {
  3009. if (!one->data && DIFF_FILE_VALID(one))
  3010. diff_populate_filespec(r, one, &dpf_options);
  3011. if (one->is_binary == -1 && one->data)
  3012. one->is_binary = buffer_is_binary(one->data,
  3013. one->size);
  3014. if (one->is_binary == -1)
  3015. one->is_binary = 0;
  3016. }
  3017. }
  3018. return one->is_binary;
  3019. }
  3020. static const struct userdiff_funcname *
  3021. diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one)
  3022. {
  3023. diff_filespec_load_driver(one, o->repo->index);
  3024. return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
  3025. }
  3026. void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
  3027. {
  3028. if (!options->a_prefix)
  3029. options->a_prefix = a;
  3030. if (!options->b_prefix)
  3031. options->b_prefix = b;
  3032. }
  3033. struct userdiff_driver *get_textconv(struct repository *r,
  3034. struct diff_filespec *one)
  3035. {
  3036. if (!DIFF_FILE_VALID(one))
  3037. return NULL;
  3038. diff_filespec_load_driver(one, r->index);
  3039. return userdiff_get_textconv(r, one->driver);
  3040. }
  3041. static void builtin_diff(const char *name_a,
  3042. const char *name_b,
  3043. struct diff_filespec *one,
  3044. struct diff_filespec *two,
  3045. const char *xfrm_msg,
  3046. int must_show_header,
  3047. struct diff_options *o,
  3048. int complete_rewrite)
  3049. {
  3050. mmfile_t mf1, mf2;
  3051. const char *lbl[2];
  3052. char *a_one, *b_two;
  3053. const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
  3054. const char *reset = diff_get_color_opt(o, DIFF_RESET);
  3055. const char *a_prefix, *b_prefix;
  3056. struct userdiff_driver *textconv_one = NULL;
  3057. struct userdiff_driver *textconv_two = NULL;
  3058. struct strbuf header = STRBUF_INIT;
  3059. const char *line_prefix = diff_line_prefix(o);
  3060. diff_set_mnemonic_prefix(o, "a/", "b/");
  3061. if (o->flags.reverse_diff) {
  3062. a_prefix = o->b_prefix;
  3063. b_prefix = o->a_prefix;
  3064. } else {
  3065. a_prefix = o->a_prefix;
  3066. b_prefix = o->b_prefix;
  3067. }
  3068. if (o->submodule_format == DIFF_SUBMODULE_LOG &&
  3069. (!one->mode || S_ISGITLINK(one->mode)) &&
  3070. (!two->mode || S_ISGITLINK(two->mode))) {
  3071. show_submodule_diff_summary(o, one->path ? one->path : two->path,
  3072. &one->oid, &two->oid,
  3073. two->dirty_submodule);
  3074. return;
  3075. } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
  3076. (!one->mode || S_ISGITLINK(one->mode)) &&
  3077. (!two->mode || S_ISGITLINK(two->mode))) {
  3078. show_submodule_inline_diff(o, one->path ? one->path : two->path,
  3079. &one->oid, &two->oid,
  3080. two->dirty_submodule);
  3081. return;
  3082. }
  3083. if (o->flags.allow_textconv) {
  3084. textconv_one = get_textconv(o->repo, one);
  3085. textconv_two = get_textconv(o->repo, two);
  3086. }
  3087. /* Never use a non-valid filename anywhere if at all possible */
  3088. name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
  3089. name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
  3090. a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
  3091. b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
  3092. lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
  3093. lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
  3094. strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset);
  3095. if (lbl[0][0] == '/') {
  3096. /* /dev/null */
  3097. strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
  3098. if (xfrm_msg)
  3099. strbuf_addstr(&header, xfrm_msg);
  3100. must_show_header = 1;
  3101. }
  3102. else if (lbl[1][0] == '/') {
  3103. strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
  3104. if (xfrm_msg)
  3105. strbuf_addstr(&header, xfrm_msg);
  3106. must_show_header = 1;
  3107. }
  3108. else {
  3109. if (one->mode != two->mode) {
  3110. strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
  3111. strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
  3112. must_show_header = 1;
  3113. }
  3114. if (xfrm_msg)
  3115. strbuf_addstr(&header, xfrm_msg);
  3116. /*
  3117. * we do not run diff between different kind
  3118. * of objects.
  3119. */
  3120. if ((one->mode ^ two->mode) & S_IFMT)
  3121. goto free_ab_and_return;
  3122. if (complete_rewrite &&
  3123. (textconv_one || !diff_filespec_is_binary(o->repo, one)) &&
  3124. (textconv_two || !diff_filespec_is_binary(o->repo, two))) {
  3125. emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
  3126. header.buf, header.len, 0);
  3127. strbuf_reset(&header);
  3128. emit_rewrite_diff(name_a, name_b, one, two,
  3129. textconv_one, textconv_two, o);
  3130. o->found_changes = 1;
  3131. goto free_ab_and_return;
  3132. }
  3133. }
  3134. if (o->irreversible_delete && lbl[1][0] == '/') {
  3135. emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
  3136. header.len, 0);
  3137. strbuf_reset(&header);
  3138. goto free_ab_and_return;
  3139. } else if (!o->flags.text &&
  3140. ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) ||
  3141. (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) {
  3142. struct strbuf sb = STRBUF_INIT;
  3143. if (!one->data && !two->data &&
  3144. S_ISREG(one->mode) && S_ISREG(two->mode) &&
  3145. !o->flags.binary) {
  3146. if (oideq(&one->oid, &two->oid)) {
  3147. if (must_show_header)
  3148. emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
  3149. header.buf, header.len,
  3150. 0);
  3151. goto free_ab_and_return;
  3152. }
  3153. emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
  3154. header.buf, header.len, 0);
  3155. strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
  3156. diff_line_prefix(o), lbl[0], lbl[1]);
  3157. emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
  3158. sb.buf, sb.len, 0);
  3159. strbuf_release(&sb);
  3160. goto free_ab_and_return;
  3161. }
  3162. if (fill_mmfile(o->repo, &mf1, one) < 0 ||
  3163. fill_mmfile(o->repo, &mf2, two) < 0)
  3164. die("unable to read files to diff");
  3165. /* Quite common confusing case */
  3166. if (mf1.size == mf2.size &&
  3167. !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
  3168. if (must_show_header)
  3169. emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
  3170. header.buf, header.len, 0);
  3171. goto free_ab_and_return;
  3172. }
  3173. emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
  3174. strbuf_reset(&header);
  3175. if (o->flags.binary)
  3176. emit_binary_diff(o, &mf1, &mf2);
  3177. else {
  3178. strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
  3179. diff_line_prefix(o), lbl[0], lbl[1]);
  3180. emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
  3181. sb.buf, sb.len, 0);
  3182. strbuf_release(&sb);
  3183. }
  3184. o->found_changes = 1;
  3185. } else {
  3186. /* Crazy xdl interfaces.. */
  3187. const char *diffopts;
  3188. const char *v;
  3189. xpparam_t xpp;
  3190. xdemitconf_t xecfg;
  3191. struct emit_callback ecbdata;
  3192. const struct userdiff_funcname *pe;
  3193. if (must_show_header) {
  3194. emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
  3195. header.buf, header.len, 0);
  3196. strbuf_reset(&header);
  3197. }
  3198. mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
  3199. mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
  3200. pe = diff_funcname_pattern(o, one);
  3201. if (!pe)
  3202. pe = diff_funcname_pattern(o, two);
  3203. memset(&xpp, 0, sizeof(xpp));
  3204. memset(&xecfg, 0, sizeof(xecfg));
  3205. memset(&ecbdata, 0, sizeof(ecbdata));
  3206. if (o->flags.suppress_diff_headers)
  3207. lbl[0] = NULL;
  3208. ecbdata.label_path = lbl;
  3209. ecbdata.color_diff = want_color(o->use_color);
  3210. ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
  3211. if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
  3212. check_blank_at_eof(&mf1, &mf2, &ecbdata);
  3213. ecbdata.opt = o;
  3214. if (header.len && !o->flags.suppress_diff_headers)
  3215. ecbdata.header = &header;
  3216. xpp.flags = o->xdl_opts;
  3217. xpp.anchors = o->anchors;
  3218. xpp.anchors_nr = o->anchors_nr;
  3219. xecfg.ctxlen = o->context;
  3220. xecfg.interhunkctxlen = o->interhunkcontext;
  3221. xecfg.flags = XDL_EMIT_FUNCNAMES;
  3222. if (o->flags.funccontext)
  3223. xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
  3224. if (pe)
  3225. xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
  3226. diffopts = getenv("GIT_DIFF_OPTS");
  3227. if (!diffopts)
  3228. ;
  3229. else if (skip_prefix(diffopts, "--unified=", &v))
  3230. xecfg.ctxlen = strtoul(v, NULL, 10);
  3231. else if (skip_prefix(diffopts, "-u", &v))
  3232. xecfg.ctxlen = strtoul(v, NULL, 10);
  3233. if (o->word_diff)
  3234. init_diff_words_data(&ecbdata, o, one, two);
  3235. if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume,
  3236. &ecbdata, &xpp, &xecfg))
  3237. die("unable to generate diff for %s", one->path);
  3238. if (o->word_diff)
  3239. free_diff_words_data(&ecbdata);
  3240. if (textconv_one)
  3241. free(mf1.ptr);
  3242. if (textconv_two)
  3243. free(mf2.ptr);
  3244. xdiff_clear_find_func(&xecfg);
  3245. }
  3246. free_ab_and_return:
  3247. strbuf_release(&header);
  3248. diff_free_filespec_data(one);
  3249. diff_free_filespec_data(two);
  3250. free(a_one);
  3251. free(b_two);
  3252. return;
  3253. }
  3254. static char *get_compact_summary(const struct diff_filepair *p, int is_renamed)
  3255. {
  3256. if (!is_renamed) {
  3257. if (p->status == DIFF_STATUS_ADDED) {
  3258. if (S_ISLNK(p->two->mode))
  3259. return "new +l";
  3260. else if ((p->two->mode & 0777) == 0755)
  3261. return "new +x";
  3262. else
  3263. return "new";
  3264. } else if (p->status == DIFF_STATUS_DELETED)
  3265. return "gone";
  3266. }
  3267. if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode))
  3268. return "mode -l";
  3269. else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode))
  3270. return "mode +l";
  3271. else if ((p->one->mode & 0777) == 0644 &&
  3272. (p->two->mode & 0777) == 0755)
  3273. return "mode +x";
  3274. else if ((p->one->mode & 0777) == 0755 &&
  3275. (p->two->mode & 0777) == 0644)
  3276. return "mode -x";
  3277. return NULL;
  3278. }
  3279. static void builtin_diffstat(const char *name_a, const char *name_b,
  3280. struct diff_filespec *one,
  3281. struct diff_filespec *two,
  3282. struct diffstat_t *diffstat,
  3283. struct diff_options *o,
  3284. struct diff_filepair *p)
  3285. {
  3286. mmfile_t mf1, mf2;
  3287. struct diffstat_file *data;
  3288. int may_differ;
  3289. int complete_rewrite = 0;
  3290. if (!DIFF_PAIR_UNMERGED(p)) {
  3291. if (p->status == DIFF_STATUS_MODIFIED && p->score)
  3292. complete_rewrite = 1;
  3293. }
  3294. data = diffstat_add(diffstat, name_a, name_b);
  3295. data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
  3296. if (o->flags.stat_with_summary)
  3297. data->comments = get_compact_summary(p, data->is_renamed);
  3298. if (!one || !two) {
  3299. data->is_unmerged = 1;
  3300. return;
  3301. }
  3302. /* saves some reads if true, not a guarantee of diff outcome */
  3303. may_differ = !(one->oid_valid && two->oid_valid &&
  3304. oideq(&one->oid, &two->oid));
  3305. if (diff_filespec_is_binary(o->repo, one) ||
  3306. diff_filespec_is_binary(o->repo, two)) {
  3307. data->is_binary = 1;
  3308. if (!may_differ) {
  3309. data->added = 0;
  3310. data->deleted = 0;
  3311. } else {
  3312. data->added = diff_filespec_size(o->repo, two);
  3313. data->deleted = diff_filespec_size(o->repo, one);
  3314. }
  3315. }
  3316. else if (complete_rewrite) {
  3317. diff_populate_filespec(o->repo, one, NULL);
  3318. diff_populate_filespec(o->repo, two, NULL);
  3319. data->deleted = count_lines(one->data, one->size);
  3320. data->added = count_lines(two->data, two->size);
  3321. }
  3322. else if (may_differ) {
  3323. /* Crazy xdl interfaces.. */
  3324. xpparam_t xpp;
  3325. xdemitconf_t xecfg;
  3326. if (fill_mmfile(o->repo, &mf1, one) < 0 ||
  3327. fill_mmfile(o->repo, &mf2, two) < 0)
  3328. die("unable to read files to diff");
  3329. memset(&xpp, 0, sizeof(xpp));
  3330. memset(&xecfg, 0, sizeof(xecfg));
  3331. xpp.flags = o->xdl_opts;
  3332. xpp.anchors = o->anchors;
  3333. xpp.anchors_nr = o->anchors_nr;
  3334. xecfg.ctxlen = o->context;
  3335. xecfg.interhunkctxlen = o->interhunkcontext;
  3336. if (xdi_diff_outf(&mf1, &mf2, discard_hunk_line,
  3337. diffstat_consume, diffstat, &xpp, &xecfg))
  3338. die("unable to generate diffstat for %s", one->path);
  3339. if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) {
  3340. struct diffstat_file *file =
  3341. diffstat->files[diffstat->nr - 1];
  3342. /*
  3343. * Omit diffstats of modified files where nothing changed.
  3344. * Even if may_differ, this might be the case due to
  3345. * ignoring whitespace changes, etc.
  3346. *
  3347. * But note that we special-case additions, deletions,
  3348. * renames, and mode changes as adding an empty file,
  3349. * for example is still of interest.
  3350. */
  3351. if ((p->status == DIFF_STATUS_MODIFIED)
  3352. && !file->added
  3353. && !file->deleted
  3354. && one->mode == two->mode) {
  3355. free_diffstat_file(file);
  3356. diffstat->nr--;
  3357. }
  3358. }
  3359. }
  3360. diff_free_filespec_data(one);
  3361. diff_free_filespec_data(two);
  3362. }
  3363. static void builtin_checkdiff(const char *name_a, const char *name_b,
  3364. const char *attr_path,
  3365. struct diff_filespec *one,
  3366. struct diff_filespec *two,
  3367. struct diff_options *o)
  3368. {
  3369. mmfile_t mf1, mf2;
  3370. struct checkdiff_t data;
  3371. if (!two)
  3372. return;
  3373. memset(&data, 0, sizeof(data));
  3374. data.filename = name_b ? name_b : name_a;
  3375. data.lineno = 0;
  3376. data.o = o;
  3377. data.ws_rule = whitespace_rule(o->repo->index, attr_path);
  3378. data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path);
  3379. if (fill_mmfile(o->repo, &mf1, one) < 0 ||
  3380. fill_mmfile(o->repo, &mf2, two) < 0)
  3381. die("unable to read files to diff");
  3382. /*
  3383. * All the other codepaths check both sides, but not checking
  3384. * the "old" side here is deliberate. We are checking the newly
  3385. * introduced changes, and as long as the "new" side is text, we
  3386. * can and should check what it introduces.
  3387. */
  3388. if (diff_filespec_is_binary(o->repo, two))
  3389. goto free_and_return;
  3390. else {
  3391. /* Crazy xdl interfaces.. */
  3392. xpparam_t xpp;
  3393. xdemitconf_t xecfg;
  3394. memset(&xpp, 0, sizeof(xpp));
  3395. memset(&xecfg, 0, sizeof(xecfg));
  3396. xecfg.ctxlen = 1; /* at least one context line */
  3397. xpp.flags = 0;
  3398. if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk,
  3399. checkdiff_consume, &data,
  3400. &xpp, &xecfg))
  3401. die("unable to generate checkdiff for %s", one->path);
  3402. if (data.ws_rule & WS_BLANK_AT_EOF) {
  3403. struct emit_callback ecbdata;
  3404. int blank_at_eof;
  3405. ecbdata.ws_rule = data.ws_rule;
  3406. check_blank_at_eof(&mf1, &mf2, &ecbdata);
  3407. blank_at_eof = ecbdata.blank_at_eof_in_postimage;
  3408. if (blank_at_eof) {
  3409. static char *err;
  3410. if (!err)
  3411. err = whitespace_error_string(WS_BLANK_AT_EOF);
  3412. fprintf(o->file, "%s:%d: %s.\n",
  3413. data.filename, blank_at_eof, err);
  3414. data.status = 1; /* report errors */
  3415. }
  3416. }
  3417. }
  3418. free_and_return:
  3419. diff_free_filespec_data(one);
  3420. diff_free_filespec_data(two);
  3421. if (data.status)
  3422. o->flags.check_failed = 1;
  3423. }
  3424. struct diff_filespec *alloc_filespec(const char *path)
  3425. {
  3426. struct diff_filespec *spec;
  3427. FLEXPTR_ALLOC_STR(spec, path, path);
  3428. spec->count = 1;
  3429. spec->is_binary = -1;
  3430. return spec;
  3431. }
  3432. void free_filespec(struct diff_filespec *spec)
  3433. {
  3434. if (!--spec->count) {
  3435. diff_free_filespec_data(spec);
  3436. free(spec);
  3437. }
  3438. }
  3439. void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
  3440. int oid_valid, unsigned short mode)
  3441. {
  3442. if (mode) {
  3443. spec->mode = canon_mode(mode);
  3444. oidcpy(&spec->oid, oid);
  3445. spec->oid_valid = oid_valid;
  3446. }
  3447. }
  3448. /*
  3449. * Given a name and sha1 pair, if the index tells us the file in
  3450. * the work tree has that object contents, return true, so that
  3451. * prepare_temp_file() does not have to inflate and extract.
  3452. */
  3453. static int reuse_worktree_file(struct index_state *istate,
  3454. const char *name,
  3455. const struct object_id *oid,
  3456. int want_file)
  3457. {
  3458. const struct cache_entry *ce;
  3459. struct stat st;
  3460. int pos, len;
  3461. /*
  3462. * We do not read the cache ourselves here, because the
  3463. * benchmark with my previous version that always reads cache
  3464. * shows that it makes things worse for diff-tree comparing
  3465. * two linux-2.6 kernel trees in an already checked out work
  3466. * tree. This is because most diff-tree comparisons deal with
  3467. * only a small number of files, while reading the cache is
  3468. * expensive for a large project, and its cost outweighs the
  3469. * savings we get by not inflating the object to a temporary
  3470. * file. Practically, this code only helps when we are used
  3471. * by diff-cache --cached, which does read the cache before
  3472. * calling us.
  3473. */
  3474. if (!istate->cache)
  3475. return 0;
  3476. /* We want to avoid the working directory if our caller
  3477. * doesn't need the data in a normal file, this system
  3478. * is rather slow with its stat/open/mmap/close syscalls,
  3479. * and the object is contained in a pack file. The pack
  3480. * is probably already open and will be faster to obtain
  3481. * the data through than the working directory. Loose
  3482. * objects however would tend to be slower as they need
  3483. * to be individually opened and inflated.
  3484. */
  3485. if (!FAST_WORKING_DIRECTORY && !want_file && has_object_pack(oid))
  3486. return 0;
  3487. /*
  3488. * Similarly, if we'd have to convert the file contents anyway, that
  3489. * makes the optimization not worthwhile.
  3490. */
  3491. if (!want_file && would_convert_to_git(istate, name))
  3492. return 0;
  3493. len = strlen(name);
  3494. pos = index_name_pos(istate, name, len);
  3495. if (pos < 0)
  3496. return 0;
  3497. ce = istate->cache[pos];
  3498. /*
  3499. * This is not the sha1 we are looking for, or
  3500. * unreusable because it is not a regular file.
  3501. */
  3502. if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
  3503. return 0;
  3504. /*
  3505. * If ce is marked as "assume unchanged", there is no
  3506. * guarantee that work tree matches what we are looking for.
  3507. */
  3508. if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
  3509. return 0;
  3510. /*
  3511. * If ce matches the file in the work tree, we can reuse it.
  3512. */
  3513. if (ce_uptodate(ce) ||
  3514. (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0)))
  3515. return 1;
  3516. return 0;
  3517. }
  3518. static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
  3519. {
  3520. struct strbuf buf = STRBUF_INIT;
  3521. char *dirty = "";
  3522. /* Are we looking at the work tree? */
  3523. if (s->dirty_submodule)
  3524. dirty = "-dirty";
  3525. strbuf_addf(&buf, "Subproject commit %s%s\n",
  3526. oid_to_hex(&s->oid), dirty);
  3527. s->size = buf.len;
  3528. if (size_only) {
  3529. s->data = NULL;
  3530. strbuf_release(&buf);
  3531. } else {
  3532. s->data = strbuf_detach(&buf, NULL);
  3533. s->should_free = 1;
  3534. }
  3535. return 0;
  3536. }
  3537. /*
  3538. * While doing rename detection and pickaxe operation, we may need to
  3539. * grab the data for the blob (or file) for our own in-core comparison.
  3540. * diff_filespec has data and size fields for this purpose.
  3541. */
  3542. int diff_populate_filespec(struct repository *r,
  3543. struct diff_filespec *s,
  3544. const struct diff_populate_filespec_options *options)
  3545. {
  3546. int size_only = options ? options->check_size_only : 0;
  3547. int check_binary = options ? options->check_binary : 0;
  3548. int err = 0;
  3549. int conv_flags = global_conv_flags_eol;
  3550. /*
  3551. * demote FAIL to WARN to allow inspecting the situation
  3552. * instead of refusing.
  3553. */
  3554. if (conv_flags & CONV_EOL_RNDTRP_DIE)
  3555. conv_flags = CONV_EOL_RNDTRP_WARN;
  3556. if (!DIFF_FILE_VALID(s))
  3557. die("internal error: asking to populate invalid file.");
  3558. if (S_ISDIR(s->mode))
  3559. return -1;
  3560. if (s->data)
  3561. return 0;
  3562. if (size_only && 0 < s->size)
  3563. return 0;
  3564. if (S_ISGITLINK(s->mode))
  3565. return diff_populate_gitlink(s, size_only);
  3566. if (!s->oid_valid ||
  3567. reuse_worktree_file(r->index, s->path, &s->oid, 0)) {
  3568. struct strbuf buf = STRBUF_INIT;
  3569. struct stat st;
  3570. int fd;
  3571. if (lstat(s->path, &st) < 0) {
  3572. err_empty:
  3573. err = -1;
  3574. empty:
  3575. s->data = (char *)"";
  3576. s->size = 0;
  3577. return err;
  3578. }
  3579. s->size = xsize_t(st.st_size);
  3580. if (!s->size)
  3581. goto empty;
  3582. if (S_ISLNK(st.st_mode)) {
  3583. struct strbuf sb = STRBUF_INIT;
  3584. if (strbuf_readlink(&sb, s->path, s->size))
  3585. goto err_empty;
  3586. s->size = sb.len;
  3587. s->data = strbuf_detach(&sb, NULL);
  3588. s->should_free = 1;
  3589. return 0;
  3590. }
  3591. /*
  3592. * Even if the caller would be happy with getting
  3593. * only the size, we cannot return early at this
  3594. * point if the path requires us to run the content
  3595. * conversion.
  3596. */
  3597. if (size_only && !would_convert_to_git(r->index, s->path))
  3598. return 0;
  3599. /*
  3600. * Note: this check uses xsize_t(st.st_size) that may
  3601. * not be the true size of the blob after it goes
  3602. * through convert_to_git(). This may not strictly be
  3603. * correct, but the whole point of big_file_threshold
  3604. * and is_binary check being that we want to avoid
  3605. * opening the file and inspecting the contents, this
  3606. * is probably fine.
  3607. */
  3608. if (check_binary &&
  3609. s->size > big_file_threshold && s->is_binary == -1) {
  3610. s->is_binary = 1;
  3611. return 0;
  3612. }
  3613. fd = open(s->path, O_RDONLY);
  3614. if (fd < 0)
  3615. goto err_empty;
  3616. s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
  3617. close(fd);
  3618. s->should_munmap = 1;
  3619. /*
  3620. * Convert from working tree format to canonical git format
  3621. */
  3622. if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) {
  3623. size_t size = 0;
  3624. munmap(s->data, s->size);
  3625. s->should_munmap = 0;
  3626. s->data = strbuf_detach(&buf, &size);
  3627. s->size = size;
  3628. s->should_free = 1;
  3629. }
  3630. }
  3631. else {
  3632. struct object_info info = {
  3633. .sizep = &s->size
  3634. };
  3635. if (!(size_only || check_binary))
  3636. /*
  3637. * Set contentp, since there is no chance that merely
  3638. * the size is sufficient.
  3639. */
  3640. info.contentp = &s->data;
  3641. if (options && options->missing_object_cb) {
  3642. if (!oid_object_info_extended(r, &s->oid, &info,
  3643. OBJECT_INFO_LOOKUP_REPLACE |
  3644. OBJECT_INFO_SKIP_FETCH_OBJECT))
  3645. goto object_read;
  3646. options->missing_object_cb(options->missing_object_data);
  3647. }
  3648. if (oid_object_info_extended(r, &s->oid, &info,
  3649. OBJECT_INFO_LOOKUP_REPLACE))
  3650. die("unable to read %s", oid_to_hex(&s->oid));
  3651. object_read:
  3652. if (size_only || check_binary) {
  3653. if (size_only)
  3654. return 0;
  3655. if (s->size > big_file_threshold && s->is_binary == -1) {
  3656. s->is_binary = 1;
  3657. return 0;
  3658. }
  3659. }
  3660. if (!info.contentp) {
  3661. info.contentp = &s->data;
  3662. if (oid_object_info_extended(r, &s->oid, &info,
  3663. OBJECT_INFO_LOOKUP_REPLACE))
  3664. die("unable to read %s", oid_to_hex(&s->oid));
  3665. }
  3666. s->should_free = 1;
  3667. }
  3668. return 0;
  3669. }
  3670. void diff_free_filespec_blob(struct diff_filespec *s)
  3671. {
  3672. if (s->should_free)
  3673. free(s->data);
  3674. else if (s->should_munmap)
  3675. munmap(s->data, s->size);
  3676. if (s->should_free || s->should_munmap) {
  3677. s->should_free = s->should_munmap = 0;
  3678. s->data = NULL;
  3679. }
  3680. }
  3681. void diff_free_filespec_data(struct diff_filespec *s)
  3682. {
  3683. diff_free_filespec_blob(s);
  3684. FREE_AND_NULL(s->cnt_data);
  3685. }
  3686. static void prep_temp_blob(struct index_state *istate,
  3687. const char *path, struct diff_tempfile *temp,
  3688. void *blob,
  3689. unsigned long size,
  3690. const struct object_id *oid,
  3691. int mode)
  3692. {
  3693. struct strbuf buf = STRBUF_INIT;
  3694. struct strbuf tempfile = STRBUF_INIT;
  3695. char *path_dup = xstrdup(path);
  3696. const char *base = basename(path_dup);
  3697. struct checkout_metadata meta;
  3698. init_checkout_metadata(&meta, NULL, NULL, oid);
  3699. /* Generate "XXXXXX_basename.ext" */
  3700. strbuf_addstr(&tempfile, "XXXXXX_");
  3701. strbuf_addstr(&tempfile, base);
  3702. temp->tempfile = mks_tempfile_ts(tempfile.buf, strlen(base) + 1);
  3703. if (!temp->tempfile)
  3704. die_errno("unable to create temp-file");
  3705. if (convert_to_working_tree(istate, path,
  3706. (const char *)blob, (size_t)size, &buf, &meta)) {
  3707. blob = buf.buf;
  3708. size = buf.len;
  3709. }
  3710. if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
  3711. close_tempfile_gently(temp->tempfile))
  3712. die_errno("unable to write temp-file");
  3713. temp->name = get_tempfile_path(temp->tempfile);
  3714. oid_to_hex_r(temp->hex, oid);
  3715. xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
  3716. strbuf_release(&buf);
  3717. strbuf_release(&tempfile);
  3718. free(path_dup);
  3719. }
  3720. static struct diff_tempfile *prepare_temp_file(struct repository *r,
  3721. const char *name,
  3722. struct diff_filespec *one)
  3723. {
  3724. struct diff_tempfile *temp = claim_diff_tempfile();
  3725. if (!DIFF_FILE_VALID(one)) {
  3726. not_a_valid_file:
  3727. /* A '-' entry produces this for file-2, and
  3728. * a '+' entry produces this for file-1.
  3729. */
  3730. temp->name = "/dev/null";
  3731. xsnprintf(temp->hex, sizeof(temp->hex), ".");
  3732. xsnprintf(temp->mode, sizeof(temp->mode), ".");
  3733. return temp;
  3734. }
  3735. if (!S_ISGITLINK(one->mode) &&
  3736. (!one->oid_valid ||
  3737. reuse_worktree_file(r->index, name, &one->oid, 1))) {
  3738. struct stat st;
  3739. if (lstat(name, &st) < 0) {
  3740. if (errno == ENOENT)
  3741. goto not_a_valid_file;
  3742. die_errno("stat(%s)", name);
  3743. }
  3744. if (S_ISLNK(st.st_mode)) {
  3745. struct strbuf sb = STRBUF_INIT;
  3746. if (strbuf_readlink(&sb, name, st.st_size) < 0)
  3747. die_errno("readlink(%s)", name);
  3748. prep_temp_blob(r->index, name, temp, sb.buf, sb.len,
  3749. (one->oid_valid ?
  3750. &one->oid : &null_oid),
  3751. (one->oid_valid ?
  3752. one->mode : S_IFLNK));
  3753. strbuf_release(&sb);
  3754. }
  3755. else {
  3756. /* we can borrow from the file in the work tree */
  3757. temp->name = name;
  3758. if (!one->oid_valid)
  3759. oid_to_hex_r(temp->hex, &null_oid);
  3760. else
  3761. oid_to_hex_r(temp->hex, &one->oid);
  3762. /* Even though we may sometimes borrow the
  3763. * contents from the work tree, we always want
  3764. * one->mode. mode is trustworthy even when
  3765. * !(one->oid_valid), as long as
  3766. * DIFF_FILE_VALID(one).
  3767. */
  3768. xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
  3769. }
  3770. return temp;
  3771. }
  3772. else {
  3773. if (diff_populate_filespec(r, one, NULL))
  3774. die("cannot read data blob for %s", one->path);
  3775. prep_temp_blob(r->index, name, temp,
  3776. one->data, one->size,
  3777. &one->oid, one->mode);
  3778. }
  3779. return temp;
  3780. }
  3781. static void add_external_diff_name(struct repository *r,
  3782. struct strvec *argv,
  3783. const char *name,
  3784. struct diff_filespec *df)
  3785. {
  3786. struct diff_tempfile *temp = prepare_temp_file(r, name, df);
  3787. strvec_push(argv, temp->name);
  3788. strvec_push(argv, temp->hex);
  3789. strvec_push(argv, temp->mode);
  3790. }
  3791. /* An external diff command takes:
  3792. *
  3793. * diff-cmd name infile1 infile1-sha1 infile1-mode \
  3794. * infile2 infile2-sha1 infile2-mode [ rename-to ]
  3795. *
  3796. */
  3797. static void run_external_diff(const char *pgm,
  3798. const char *name,
  3799. const char *other,
  3800. struct diff_filespec *one,
  3801. struct diff_filespec *two,
  3802. const char *xfrm_msg,
  3803. struct diff_options *o)
  3804. {
  3805. struct strvec argv = STRVEC_INIT;
  3806. struct strvec env = STRVEC_INIT;
  3807. struct diff_queue_struct *q = &diff_queued_diff;
  3808. strvec_push(&argv, pgm);
  3809. strvec_push(&argv, name);
  3810. if (one && two) {
  3811. add_external_diff_name(o->repo, &argv, name, one);
  3812. if (!other)
  3813. add_external_diff_name(o->repo, &argv, name, two);
  3814. else {
  3815. add_external_diff_name(o->repo, &argv, other, two);
  3816. strvec_push(&argv, other);
  3817. strvec_push(&argv, xfrm_msg);
  3818. }
  3819. }
  3820. strvec_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
  3821. strvec_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
  3822. diff_free_filespec_data(one);
  3823. diff_free_filespec_data(two);
  3824. if (run_command_v_opt_cd_env(argv.v, RUN_USING_SHELL, NULL, env.v))
  3825. die(_("external diff died, stopping at %s"), name);
  3826. remove_tempfile();
  3827. strvec_clear(&argv);
  3828. strvec_clear(&env);
  3829. }
  3830. static int similarity_index(struct diff_filepair *p)
  3831. {
  3832. return p->score * 100 / MAX_SCORE;
  3833. }
  3834. static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
  3835. {
  3836. if (startup_info->have_repository)
  3837. return find_unique_abbrev(oid, abbrev);
  3838. else {
  3839. char *hex = oid_to_hex(oid);
  3840. if (abbrev < 0)
  3841. abbrev = FALLBACK_DEFAULT_ABBREV;
  3842. if (abbrev > the_hash_algo->hexsz)
  3843. BUG("oid abbreviation out of range: %d", abbrev);
  3844. if (abbrev)
  3845. hex[abbrev] = '\0';
  3846. return hex;
  3847. }
  3848. }
  3849. static void fill_metainfo(struct strbuf *msg,
  3850. const char *name,
  3851. const char *other,
  3852. struct diff_filespec *one,
  3853. struct diff_filespec *two,
  3854. struct diff_options *o,
  3855. struct diff_filepair *p,
  3856. int *must_show_header,
  3857. int use_color)
  3858. {
  3859. const char *set = diff_get_color(use_color, DIFF_METAINFO);
  3860. const char *reset = diff_get_color(use_color, DIFF_RESET);
  3861. const char *line_prefix = diff_line_prefix(o);
  3862. *must_show_header = 1;
  3863. strbuf_init(msg, PATH_MAX * 2 + 300);
  3864. switch (p->status) {
  3865. case DIFF_STATUS_COPIED:
  3866. strbuf_addf(msg, "%s%ssimilarity index %d%%",
  3867. line_prefix, set, similarity_index(p));
  3868. strbuf_addf(msg, "%s\n%s%scopy from ",
  3869. reset, line_prefix, set);
  3870. quote_c_style(name, msg, NULL, 0);
  3871. strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
  3872. quote_c_style(other, msg, NULL, 0);
  3873. strbuf_addf(msg, "%s\n", reset);
  3874. break;
  3875. case DIFF_STATUS_RENAMED:
  3876. strbuf_addf(msg, "%s%ssimilarity index %d%%",
  3877. line_prefix, set, similarity_index(p));
  3878. strbuf_addf(msg, "%s\n%s%srename from ",
  3879. reset, line_prefix, set);
  3880. quote_c_style(name, msg, NULL, 0);
  3881. strbuf_addf(msg, "%s\n%s%srename to ",
  3882. reset, line_prefix, set);
  3883. quote_c_style(other, msg, NULL, 0);
  3884. strbuf_addf(msg, "%s\n", reset);
  3885. break;
  3886. case DIFF_STATUS_MODIFIED:
  3887. if (p->score) {
  3888. strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
  3889. line_prefix,
  3890. set, similarity_index(p), reset);
  3891. break;
  3892. }
  3893. /* fallthru */
  3894. default:
  3895. *must_show_header = 0;
  3896. }
  3897. if (one && two && !oideq(&one->oid, &two->oid)) {
  3898. const unsigned hexsz = the_hash_algo->hexsz;
  3899. int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV;
  3900. if (o->flags.full_index)
  3901. abbrev = hexsz;
  3902. if (o->flags.binary) {
  3903. mmfile_t mf;
  3904. if ((!fill_mmfile(o->repo, &mf, one) &&
  3905. diff_filespec_is_binary(o->repo, one)) ||
  3906. (!fill_mmfile(o->repo, &mf, two) &&
  3907. diff_filespec_is_binary(o->repo, two)))
  3908. abbrev = hexsz;
  3909. }
  3910. strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
  3911. diff_abbrev_oid(&one->oid, abbrev),
  3912. diff_abbrev_oid(&two->oid, abbrev));
  3913. if (one->mode == two->mode)
  3914. strbuf_addf(msg, " %06o", one->mode);
  3915. strbuf_addf(msg, "%s\n", reset);
  3916. }
  3917. }
  3918. static void run_diff_cmd(const char *pgm,
  3919. const char *name,
  3920. const char *other,
  3921. const char *attr_path,
  3922. struct diff_filespec *one,
  3923. struct diff_filespec *two,
  3924. struct strbuf *msg,
  3925. struct diff_options *o,
  3926. struct diff_filepair *p)
  3927. {
  3928. const char *xfrm_msg = NULL;
  3929. int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
  3930. int must_show_header = 0;
  3931. if (o->flags.allow_external) {
  3932. struct userdiff_driver *drv;
  3933. drv = userdiff_find_by_path(o->repo->index, attr_path);
  3934. if (drv && drv->external)
  3935. pgm = drv->external;
  3936. }
  3937. if (msg) {
  3938. /*
  3939. * don't use colors when the header is intended for an
  3940. * external diff driver
  3941. */
  3942. fill_metainfo(msg, name, other, one, two, o, p,
  3943. &must_show_header,
  3944. want_color(o->use_color) && !pgm);
  3945. xfrm_msg = msg->len ? msg->buf : NULL;
  3946. }
  3947. if (pgm) {
  3948. run_external_diff(pgm, name, other, one, two, xfrm_msg, o);
  3949. return;
  3950. }
  3951. if (one && two)
  3952. builtin_diff(name, other ? other : name,
  3953. one, two, xfrm_msg, must_show_header,
  3954. o, complete_rewrite);
  3955. else
  3956. fprintf(o->file, "* Unmerged path %s\n", name);
  3957. }
  3958. static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate)
  3959. {
  3960. if (DIFF_FILE_VALID(one)) {
  3961. if (!one->oid_valid) {
  3962. struct stat st;
  3963. if (one->is_stdin) {
  3964. oidclr(&one->oid);
  3965. return;
  3966. }
  3967. if (lstat(one->path, &st) < 0)
  3968. die_errno("stat '%s'", one->path);
  3969. if (index_path(istate, &one->oid, one->path, &st, 0))
  3970. die("cannot hash %s", one->path);
  3971. }
  3972. }
  3973. else
  3974. oidclr(&one->oid);
  3975. }
  3976. static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
  3977. {
  3978. /* Strip the prefix but do not molest /dev/null and absolute paths */
  3979. if (*namep && !is_absolute_path(*namep)) {
  3980. *namep += prefix_length;
  3981. if (**namep == '/')
  3982. ++*namep;
  3983. }
  3984. if (*otherp && !is_absolute_path(*otherp)) {
  3985. *otherp += prefix_length;
  3986. if (**otherp == '/')
  3987. ++*otherp;
  3988. }
  3989. }
  3990. static void run_diff(struct diff_filepair *p, struct diff_options *o)
  3991. {
  3992. const char *pgm = external_diff();
  3993. struct strbuf msg;
  3994. struct diff_filespec *one = p->one;
  3995. struct diff_filespec *two = p->two;
  3996. const char *name;
  3997. const char *other;
  3998. const char *attr_path;
  3999. name = one->path;
  4000. other = (strcmp(name, two->path) ? two->path : NULL);
  4001. attr_path = name;
  4002. if (o->prefix_length)
  4003. strip_prefix(o->prefix_length, &name, &other);
  4004. if (!o->flags.allow_external)
  4005. pgm = NULL;
  4006. if (DIFF_PAIR_UNMERGED(p)) {
  4007. run_diff_cmd(pgm, name, NULL, attr_path,
  4008. NULL, NULL, NULL, o, p);
  4009. return;
  4010. }
  4011. diff_fill_oid_info(one, o->repo->index);
  4012. diff_fill_oid_info(two, o->repo->index);
  4013. if (!pgm &&
  4014. DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
  4015. (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
  4016. /*
  4017. * a filepair that changes between file and symlink
  4018. * needs to be split into deletion and creation.
  4019. */
  4020. struct diff_filespec *null = alloc_filespec(two->path);
  4021. run_diff_cmd(NULL, name, other, attr_path,
  4022. one, null, &msg,
  4023. o, p);
  4024. free(null);
  4025. strbuf_release(&msg);
  4026. null = alloc_filespec(one->path);
  4027. run_diff_cmd(NULL, name, other, attr_path,
  4028. null, two, &msg, o, p);
  4029. free(null);
  4030. }
  4031. else
  4032. run_diff_cmd(pgm, name, other, attr_path,
  4033. one, two, &msg, o, p);
  4034. strbuf_release(&msg);
  4035. }
  4036. static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
  4037. struct diffstat_t *diffstat)
  4038. {
  4039. const char *name;
  4040. const char *other;
  4041. if (DIFF_PAIR_UNMERGED(p)) {
  4042. /* unmerged */
  4043. builtin_diffstat(p->one->path, NULL, NULL, NULL,
  4044. diffstat, o, p);
  4045. return;
  4046. }
  4047. name = p->one->path;
  4048. other = (strcmp(name, p->two->path) ? p->two->path : NULL);
  4049. if (o->prefix_length)
  4050. strip_prefix(o->prefix_length, &name, &other);
  4051. diff_fill_oid_info(p->one, o->repo->index);
  4052. diff_fill_oid_info(p->two, o->repo->index);
  4053. builtin_diffstat(name, other, p->one, p->two,
  4054. diffstat, o, p);
  4055. }
  4056. static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
  4057. {
  4058. const char *name;
  4059. const char *other;
  4060. const char *attr_path;
  4061. if (DIFF_PAIR_UNMERGED(p)) {
  4062. /* unmerged */
  4063. return;
  4064. }
  4065. name = p->one->path;
  4066. other = (strcmp(name, p->two->path) ? p->two->path : NULL);
  4067. attr_path = other ? other : name;
  4068. if (o->prefix_length)
  4069. strip_prefix(o->prefix_length, &name, &other);
  4070. diff_fill_oid_info(p->one, o->repo->index);
  4071. diff_fill_oid_info(p->two, o->repo->index);
  4072. builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
  4073. }
  4074. static void prep_parse_options(struct diff_options *options);
  4075. void repo_diff_setup(struct repository *r, struct diff_options *options)
  4076. {
  4077. memcpy(options, &default_diff_options, sizeof(*options));
  4078. options->file = stdout;
  4079. options->repo = r;
  4080. options->output_indicators[OUTPUT_INDICATOR_NEW] = '+';
  4081. options->output_indicators[OUTPUT_INDICATOR_OLD] = '-';
  4082. options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' ';
  4083. options->abbrev = DEFAULT_ABBREV;
  4084. options->line_termination = '\n';
  4085. options->break_opt = -1;
  4086. options->rename_limit = -1;
  4087. options->dirstat_permille = diff_dirstat_permille_default;
  4088. options->context = diff_context_default;
  4089. options->interhunkcontext = diff_interhunk_context_default;
  4090. options->ws_error_highlight = ws_error_highlight_default;
  4091. options->flags.rename_empty = 1;
  4092. options->flags.relative_name = diff_relative;
  4093. options->objfind = NULL;
  4094. /* pathchange left =NULL by default */
  4095. options->change = diff_change;
  4096. options->add_remove = diff_addremove;
  4097. options->use_color = diff_use_color_default;
  4098. options->detect_rename = diff_detect_rename_default;
  4099. options->xdl_opts |= diff_algorithm;
  4100. if (diff_indent_heuristic)
  4101. DIFF_XDL_SET(options, INDENT_HEURISTIC);
  4102. options->orderfile = diff_order_file_cfg;
  4103. if (diff_no_prefix) {
  4104. options->a_prefix = options->b_prefix = "";
  4105. } else if (!diff_mnemonic_prefix) {
  4106. options->a_prefix = "a/";
  4107. options->b_prefix = "b/";
  4108. }
  4109. options->color_moved = diff_color_moved_default;
  4110. options->color_moved_ws_handling = diff_color_moved_ws_default;
  4111. prep_parse_options(options);
  4112. }
  4113. void diff_setup_done(struct diff_options *options)
  4114. {
  4115. unsigned check_mask = DIFF_FORMAT_NAME |
  4116. DIFF_FORMAT_NAME_STATUS |
  4117. DIFF_FORMAT_CHECKDIFF |
  4118. DIFF_FORMAT_NO_OUTPUT;
  4119. /*
  4120. * This must be signed because we're comparing against a potentially
  4121. * negative value.
  4122. */
  4123. const int hexsz = the_hash_algo->hexsz;
  4124. if (options->set_default)
  4125. options->set_default(options);
  4126. if (HAS_MULTI_BITS(options->output_format & check_mask))
  4127. die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
  4128. if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
  4129. die(_("-G, -S and --find-object are mutually exclusive"));
  4130. /*
  4131. * Most of the time we can say "there are changes"
  4132. * only by checking if there are changed paths, but
  4133. * --ignore-whitespace* options force us to look
  4134. * inside contents.
  4135. */
  4136. if ((options->xdl_opts & XDF_WHITESPACE_FLAGS))
  4137. options->flags.diff_from_contents = 1;
  4138. else
  4139. options->flags.diff_from_contents = 0;
  4140. if (options->flags.find_copies_harder)
  4141. options->detect_rename = DIFF_DETECT_COPY;
  4142. if (!options->flags.relative_name)
  4143. options->prefix = NULL;
  4144. if (options->prefix)
  4145. options->prefix_length = strlen(options->prefix);
  4146. else
  4147. options->prefix_length = 0;
  4148. if (options->output_format & (DIFF_FORMAT_NAME |
  4149. DIFF_FORMAT_NAME_STATUS |
  4150. DIFF_FORMAT_CHECKDIFF |
  4151. DIFF_FORMAT_NO_OUTPUT))
  4152. options->output_format &= ~(DIFF_FORMAT_RAW |
  4153. DIFF_FORMAT_NUMSTAT |
  4154. DIFF_FORMAT_DIFFSTAT |
  4155. DIFF_FORMAT_SHORTSTAT |
  4156. DIFF_FORMAT_DIRSTAT |
  4157. DIFF_FORMAT_SUMMARY |
  4158. DIFF_FORMAT_PATCH);
  4159. /*
  4160. * These cases always need recursive; we do not drop caller-supplied
  4161. * recursive bits for other formats here.
  4162. */
  4163. if (options->output_format & (DIFF_FORMAT_PATCH |
  4164. DIFF_FORMAT_NUMSTAT |
  4165. DIFF_FORMAT_DIFFSTAT |
  4166. DIFF_FORMAT_SHORTSTAT |
  4167. DIFF_FORMAT_DIRSTAT |
  4168. DIFF_FORMAT_SUMMARY |
  4169. DIFF_FORMAT_CHECKDIFF))
  4170. options->flags.recursive = 1;
  4171. /*
  4172. * Also pickaxe would not work very well if you do not say recursive
  4173. */
  4174. if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
  4175. options->flags.recursive = 1;
  4176. /*
  4177. * When patches are generated, submodules diffed against the work tree
  4178. * must be checked for dirtiness too so it can be shown in the output
  4179. */
  4180. if (options->output_format & DIFF_FORMAT_PATCH)
  4181. options->flags.dirty_submodules = 1;
  4182. if (options->detect_rename && options->rename_limit < 0)
  4183. options->rename_limit = diff_rename_limit_default;
  4184. if (hexsz < options->abbrev)
  4185. options->abbrev = hexsz; /* full */
  4186. /*
  4187. * It does not make sense to show the first hit we happened
  4188. * to have found. It does not make sense not to return with
  4189. * exit code in such a case either.
  4190. */
  4191. if (options->flags.quick) {
  4192. options->output_format = DIFF_FORMAT_NO_OUTPUT;
  4193. options->flags.exit_with_status = 1;
  4194. }
  4195. options->diff_path_counter = 0;
  4196. if (options->flags.follow_renames && options->pathspec.nr != 1)
  4197. die(_("--follow requires exactly one pathspec"));
  4198. if (!options->use_color || external_diff())
  4199. options->color_moved = 0;
  4200. FREE_AND_NULL(options->parseopts);
  4201. }
  4202. int parse_long_opt(const char *opt, const char **argv,
  4203. const char **optarg)
  4204. {
  4205. const char *arg = argv[0];
  4206. if (!skip_prefix(arg, "--", &arg))
  4207. return 0;
  4208. if (!skip_prefix(arg, opt, &arg))
  4209. return 0;
  4210. if (*arg == '=') { /* stuck form: --option=value */
  4211. *optarg = arg + 1;
  4212. return 1;
  4213. }
  4214. if (*arg != '\0')
  4215. return 0;
  4216. /* separate form: --option value */
  4217. if (!argv[1])
  4218. die("Option '--%s' requires a value", opt);
  4219. *optarg = argv[1];
  4220. return 2;
  4221. }
  4222. static int diff_opt_stat(const struct option *opt, const char *value, int unset)
  4223. {
  4224. struct diff_options *options = opt->value;
  4225. int width = options->stat_width;
  4226. int name_width = options->stat_name_width;
  4227. int graph_width = options->stat_graph_width;
  4228. int count = options->stat_count;
  4229. char *end;
  4230. BUG_ON_OPT_NEG(unset);
  4231. if (!strcmp(opt->long_name, "stat")) {
  4232. if (value) {
  4233. width = strtoul(value, &end, 10);
  4234. if (*end == ',')
  4235. name_width = strtoul(end+1, &end, 10);
  4236. if (*end == ',')
  4237. count = strtoul(end+1, &end, 10);
  4238. if (*end)
  4239. return error(_("invalid --stat value: %s"), value);
  4240. }
  4241. } else if (!strcmp(opt->long_name, "stat-width")) {
  4242. width = strtoul(value, &end, 10);
  4243. if (*end)
  4244. return error(_("%s expects a numerical value"),
  4245. opt->long_name);
  4246. } else if (!strcmp(opt->long_name, "stat-name-width")) {
  4247. name_width = strtoul(value, &end, 10);
  4248. if (*end)
  4249. return error(_("%s expects a numerical value"),
  4250. opt->long_name);
  4251. } else if (!strcmp(opt->long_name, "stat-graph-width")) {
  4252. graph_width = strtoul(value, &end, 10);
  4253. if (*end)
  4254. return error(_("%s expects a numerical value"),
  4255. opt->long_name);
  4256. } else if (!strcmp(opt->long_name, "stat-count")) {
  4257. count = strtoul(value, &end, 10);
  4258. if (*end)
  4259. return error(_("%s expects a numerical value"),
  4260. opt->long_name);
  4261. } else
  4262. BUG("%s should not get here", opt->long_name);
  4263. options->output_format |= DIFF_FORMAT_DIFFSTAT;
  4264. options->stat_name_width = name_width;
  4265. options->stat_graph_width = graph_width;
  4266. options->stat_width = width;
  4267. options->stat_count = count;
  4268. return 0;
  4269. }
  4270. static int parse_dirstat_opt(struct diff_options *options, const char *params)
  4271. {
  4272. struct strbuf errmsg = STRBUF_INIT;
  4273. if (parse_dirstat_params(options, params, &errmsg))
  4274. die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
  4275. errmsg.buf);
  4276. strbuf_release(&errmsg);
  4277. /*
  4278. * The caller knows a dirstat-related option is given from the command
  4279. * line; allow it to say "return this_function();"
  4280. */
  4281. options->output_format |= DIFF_FORMAT_DIRSTAT;
  4282. return 1;
  4283. }
  4284. static const char diff_status_letters[] = {
  4285. DIFF_STATUS_ADDED,
  4286. DIFF_STATUS_COPIED,
  4287. DIFF_STATUS_DELETED,
  4288. DIFF_STATUS_MODIFIED,
  4289. DIFF_STATUS_RENAMED,
  4290. DIFF_STATUS_TYPE_CHANGED,
  4291. DIFF_STATUS_UNKNOWN,
  4292. DIFF_STATUS_UNMERGED,
  4293. DIFF_STATUS_FILTER_AON,
  4294. DIFF_STATUS_FILTER_BROKEN,
  4295. '\0',
  4296. };
  4297. static unsigned int filter_bit['Z' + 1];
  4298. static void prepare_filter_bits(void)
  4299. {
  4300. int i;
  4301. if (!filter_bit[DIFF_STATUS_ADDED]) {
  4302. for (i = 0; diff_status_letters[i]; i++)
  4303. filter_bit[(int) diff_status_letters[i]] = (1 << i);
  4304. }
  4305. }
  4306. static unsigned filter_bit_tst(char status, const struct diff_options *opt)
  4307. {
  4308. return opt->filter & filter_bit[(int) status];
  4309. }
  4310. unsigned diff_filter_bit(char status)
  4311. {
  4312. prepare_filter_bits();
  4313. return filter_bit[(int) status];
  4314. }
  4315. static int diff_opt_diff_filter(const struct option *option,
  4316. const char *optarg, int unset)
  4317. {
  4318. struct diff_options *opt = option->value;
  4319. int i, optch;
  4320. BUG_ON_OPT_NEG(unset);
  4321. prepare_filter_bits();
  4322. /*
  4323. * If there is a negation e.g. 'd' in the input, and we haven't
  4324. * initialized the filter field with another --diff-filter, start
  4325. * from full set of bits, except for AON.
  4326. */
  4327. if (!opt->filter) {
  4328. for (i = 0; (optch = optarg[i]) != '\0'; i++) {
  4329. if (optch < 'a' || 'z' < optch)
  4330. continue;
  4331. opt->filter = (1 << (ARRAY_SIZE(diff_status_letters) - 1)) - 1;
  4332. opt->filter &= ~filter_bit[DIFF_STATUS_FILTER_AON];
  4333. break;
  4334. }
  4335. }
  4336. for (i = 0; (optch = optarg[i]) != '\0'; i++) {
  4337. unsigned int bit;
  4338. int negate;
  4339. if ('a' <= optch && optch <= 'z') {
  4340. negate = 1;
  4341. optch = toupper(optch);
  4342. } else {
  4343. negate = 0;
  4344. }
  4345. bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0;
  4346. if (!bit)
  4347. return error(_("unknown change class '%c' in --diff-filter=%s"),
  4348. optarg[i], optarg);
  4349. if (negate)
  4350. opt->filter &= ~bit;
  4351. else
  4352. opt->filter |= bit;
  4353. }
  4354. return 0;
  4355. }
  4356. static void enable_patch_output(int *fmt)
  4357. {
  4358. *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
  4359. *fmt |= DIFF_FORMAT_PATCH;
  4360. }
  4361. static int diff_opt_ws_error_highlight(const struct option *option,
  4362. const char *arg, int unset)
  4363. {
  4364. struct diff_options *opt = option->value;
  4365. int val = parse_ws_error_highlight(arg);
  4366. BUG_ON_OPT_NEG(unset);
  4367. if (val < 0)
  4368. return error(_("unknown value after ws-error-highlight=%.*s"),
  4369. -1 - val, arg);
  4370. opt->ws_error_highlight = val;
  4371. return 0;
  4372. }
  4373. static int diff_opt_find_object(const struct option *option,
  4374. const char *arg, int unset)
  4375. {
  4376. struct diff_options *opt = option->value;
  4377. struct object_id oid;
  4378. BUG_ON_OPT_NEG(unset);
  4379. if (get_oid(arg, &oid))
  4380. return error(_("unable to resolve '%s'"), arg);
  4381. if (!opt->objfind)
  4382. opt->objfind = xcalloc(1, sizeof(*opt->objfind));
  4383. opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
  4384. opt->flags.recursive = 1;
  4385. opt->flags.tree_in_recursive = 1;
  4386. oidset_insert(opt->objfind, &oid);
  4387. return 0;
  4388. }
  4389. static int diff_opt_anchored(const struct option *opt,
  4390. const char *arg, int unset)
  4391. {
  4392. struct diff_options *options = opt->value;
  4393. BUG_ON_OPT_NEG(unset);
  4394. options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
  4395. ALLOC_GROW(options->anchors, options->anchors_nr + 1,
  4396. options->anchors_alloc);
  4397. options->anchors[options->anchors_nr++] = xstrdup(arg);
  4398. return 0;
  4399. }
  4400. static int diff_opt_binary(const struct option *opt,
  4401. const char *arg, int unset)
  4402. {
  4403. struct diff_options *options = opt->value;
  4404. BUG_ON_OPT_NEG(unset);
  4405. BUG_ON_OPT_ARG(arg);
  4406. enable_patch_output(&options->output_format);
  4407. options->flags.binary = 1;
  4408. return 0;
  4409. }
  4410. static int diff_opt_break_rewrites(const struct option *opt,
  4411. const char *arg, int unset)
  4412. {
  4413. int *break_opt = opt->value;
  4414. int opt1, opt2;
  4415. BUG_ON_OPT_NEG(unset);
  4416. if (!arg)
  4417. arg = "";
  4418. opt1 = parse_rename_score(&arg);
  4419. if (*arg == 0)
  4420. opt2 = 0;
  4421. else if (*arg != '/')
  4422. return error(_("%s expects <n>/<m> form"), opt->long_name);
  4423. else {
  4424. arg++;
  4425. opt2 = parse_rename_score(&arg);
  4426. }
  4427. if (*arg != 0)
  4428. return error(_("%s expects <n>/<m> form"), opt->long_name);
  4429. *break_opt = opt1 | (opt2 << 16);
  4430. return 0;
  4431. }
  4432. static int diff_opt_char(const struct option *opt,
  4433. const char *arg, int unset)
  4434. {
  4435. char *value = opt->value;
  4436. BUG_ON_OPT_NEG(unset);
  4437. if (arg[1])
  4438. return error(_("%s expects a character, got '%s'"),
  4439. opt->long_name, arg);
  4440. *value = arg[0];
  4441. return 0;
  4442. }
  4443. static int diff_opt_color_moved(const struct option *opt,
  4444. const char *arg, int unset)
  4445. {
  4446. struct diff_options *options = opt->value;
  4447. if (unset) {
  4448. options->color_moved = COLOR_MOVED_NO;
  4449. } else if (!arg) {
  4450. if (diff_color_moved_default)
  4451. options->color_moved = diff_color_moved_default;
  4452. if (options->color_moved == COLOR_MOVED_NO)
  4453. options->color_moved = COLOR_MOVED_DEFAULT;
  4454. } else {
  4455. int cm = parse_color_moved(arg);
  4456. if (cm < 0)
  4457. return error(_("bad --color-moved argument: %s"), arg);
  4458. options->color_moved = cm;
  4459. }
  4460. return 0;
  4461. }
  4462. static int diff_opt_color_moved_ws(const struct option *opt,
  4463. const char *arg, int unset)
  4464. {
  4465. struct diff_options *options = opt->value;
  4466. unsigned cm;
  4467. if (unset) {
  4468. options->color_moved_ws_handling = 0;
  4469. return 0;
  4470. }
  4471. cm = parse_color_moved_ws(arg);
  4472. if (cm & COLOR_MOVED_WS_ERROR)
  4473. return error(_("invalid mode '%s' in --color-moved-ws"), arg);
  4474. options->color_moved_ws_handling = cm;
  4475. return 0;
  4476. }
  4477. static int diff_opt_color_words(const struct option *opt,
  4478. const char *arg, int unset)
  4479. {
  4480. struct diff_options *options = opt->value;
  4481. BUG_ON_OPT_NEG(unset);
  4482. options->use_color = 1;
  4483. options->word_diff = DIFF_WORDS_COLOR;
  4484. options->word_regex = arg;
  4485. return 0;
  4486. }
  4487. static int diff_opt_compact_summary(const struct option *opt,
  4488. const char *arg, int unset)
  4489. {
  4490. struct diff_options *options = opt->value;
  4491. BUG_ON_OPT_ARG(arg);
  4492. if (unset) {
  4493. options->flags.stat_with_summary = 0;
  4494. } else {
  4495. options->flags.stat_with_summary = 1;
  4496. options->output_format |= DIFF_FORMAT_DIFFSTAT;
  4497. }
  4498. return 0;
  4499. }
  4500. static int diff_opt_diff_algorithm(const struct option *opt,
  4501. const char *arg, int unset)
  4502. {
  4503. struct diff_options *options = opt->value;
  4504. long value = parse_algorithm_value(arg);
  4505. BUG_ON_OPT_NEG(unset);
  4506. if (value < 0)
  4507. return error(_("option diff-algorithm accepts \"myers\", "
  4508. "\"minimal\", \"patience\" and \"histogram\""));
  4509. /* clear out previous settings */
  4510. DIFF_XDL_CLR(options, NEED_MINIMAL);
  4511. options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
  4512. options->xdl_opts |= value;
  4513. return 0;
  4514. }
  4515. static int diff_opt_dirstat(const struct option *opt,
  4516. const char *arg, int unset)
  4517. {
  4518. struct diff_options *options = opt->value;
  4519. BUG_ON_OPT_NEG(unset);
  4520. if (!strcmp(opt->long_name, "cumulative")) {
  4521. if (arg)
  4522. BUG("how come --cumulative take a value?");
  4523. arg = "cumulative";
  4524. } else if (!strcmp(opt->long_name, "dirstat-by-file"))
  4525. parse_dirstat_opt(options, "files");
  4526. parse_dirstat_opt(options, arg ? arg : "");
  4527. return 0;
  4528. }
  4529. static int diff_opt_find_copies(const struct option *opt,
  4530. const char *arg, int unset)
  4531. {
  4532. struct diff_options *options = opt->value;
  4533. BUG_ON_OPT_NEG(unset);
  4534. if (!arg)
  4535. arg = "";
  4536. options->rename_score = parse_rename_score(&arg);
  4537. if (*arg != 0)
  4538. return error(_("invalid argument to %s"), opt->long_name);
  4539. if (options->detect_rename == DIFF_DETECT_COPY)
  4540. options->flags.find_copies_harder = 1;
  4541. else
  4542. options->detect_rename = DIFF_DETECT_COPY;
  4543. return 0;
  4544. }
  4545. static int diff_opt_find_renames(const struct option *opt,
  4546. const char *arg, int unset)
  4547. {
  4548. struct diff_options *options = opt->value;
  4549. BUG_ON_OPT_NEG(unset);
  4550. if (!arg)
  4551. arg = "";
  4552. options->rename_score = parse_rename_score(&arg);
  4553. if (*arg != 0)
  4554. return error(_("invalid argument to %s"), opt->long_name);
  4555. options->detect_rename = DIFF_DETECT_RENAME;
  4556. return 0;
  4557. }
  4558. static int diff_opt_follow(const struct option *opt,
  4559. const char *arg, int unset)
  4560. {
  4561. struct diff_options *options = opt->value;
  4562. BUG_ON_OPT_ARG(arg);
  4563. if (unset) {
  4564. options->flags.follow_renames = 0;
  4565. options->flags.default_follow_renames = 0;
  4566. } else {
  4567. options->flags.follow_renames = 1;
  4568. }
  4569. return 0;
  4570. }
  4571. static int diff_opt_ignore_submodules(const struct option *opt,
  4572. const char *arg, int unset)
  4573. {
  4574. struct diff_options *options = opt->value;
  4575. BUG_ON_OPT_NEG(unset);
  4576. if (!arg)
  4577. arg = "all";
  4578. options->flags.override_submodule_config = 1;
  4579. handle_ignore_submodules_arg(options, arg);
  4580. return 0;
  4581. }
  4582. static int diff_opt_line_prefix(const struct option *opt,
  4583. const char *optarg, int unset)
  4584. {
  4585. struct diff_options *options = opt->value;
  4586. BUG_ON_OPT_NEG(unset);
  4587. options->line_prefix = optarg;
  4588. options->line_prefix_length = strlen(options->line_prefix);
  4589. graph_setup_line_prefix(options);
  4590. return 0;
  4591. }
  4592. static int diff_opt_no_prefix(const struct option *opt,
  4593. const char *optarg, int unset)
  4594. {
  4595. struct diff_options *options = opt->value;
  4596. BUG_ON_OPT_NEG(unset);
  4597. BUG_ON_OPT_ARG(optarg);
  4598. options->a_prefix = "";
  4599. options->b_prefix = "";
  4600. return 0;
  4601. }
  4602. static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
  4603. const struct option *opt,
  4604. const char *arg, int unset)
  4605. {
  4606. struct diff_options *options = opt->value;
  4607. char *path;
  4608. BUG_ON_OPT_NEG(unset);
  4609. path = prefix_filename(ctx->prefix, arg);
  4610. options->file = xfopen(path, "w");
  4611. options->close_file = 1;
  4612. if (options->use_color != GIT_COLOR_ALWAYS)
  4613. options->use_color = GIT_COLOR_NEVER;
  4614. free(path);
  4615. return 0;
  4616. }
  4617. static int diff_opt_patience(const struct option *opt,
  4618. const char *arg, int unset)
  4619. {
  4620. struct diff_options *options = opt->value;
  4621. int i;
  4622. BUG_ON_OPT_NEG(unset);
  4623. BUG_ON_OPT_ARG(arg);
  4624. options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
  4625. /*
  4626. * Both --patience and --anchored use PATIENCE_DIFF
  4627. * internally, so remove any anchors previously
  4628. * specified.
  4629. */
  4630. for (i = 0; i < options->anchors_nr; i++)
  4631. free(options->anchors[i]);
  4632. options->anchors_nr = 0;
  4633. return 0;
  4634. }
  4635. static int diff_opt_pickaxe_regex(const struct option *opt,
  4636. const char *arg, int unset)
  4637. {
  4638. struct diff_options *options = opt->value;
  4639. BUG_ON_OPT_NEG(unset);
  4640. options->pickaxe = arg;
  4641. options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
  4642. return 0;
  4643. }
  4644. static int diff_opt_pickaxe_string(const struct option *opt,
  4645. const char *arg, int unset)
  4646. {
  4647. struct diff_options *options = opt->value;
  4648. BUG_ON_OPT_NEG(unset);
  4649. options->pickaxe = arg;
  4650. options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
  4651. return 0;
  4652. }
  4653. static int diff_opt_relative(const struct option *opt,
  4654. const char *arg, int unset)
  4655. {
  4656. struct diff_options *options = opt->value;
  4657. options->flags.relative_name = !unset;
  4658. if (arg)
  4659. options->prefix = arg;
  4660. return 0;
  4661. }
  4662. static int diff_opt_submodule(const struct option *opt,
  4663. const char *arg, int unset)
  4664. {
  4665. struct diff_options *options = opt->value;
  4666. BUG_ON_OPT_NEG(unset);
  4667. if (!arg)
  4668. arg = "log";
  4669. if (parse_submodule_params(options, arg))
  4670. return error(_("failed to parse --submodule option parameter: '%s'"),
  4671. arg);
  4672. return 0;
  4673. }
  4674. static int diff_opt_textconv(const struct option *opt,
  4675. const char *arg, int unset)
  4676. {
  4677. struct diff_options *options = opt->value;
  4678. BUG_ON_OPT_ARG(arg);
  4679. if (unset) {
  4680. options->flags.allow_textconv = 0;
  4681. } else {
  4682. options->flags.allow_textconv = 1;
  4683. options->flags.textconv_set_via_cmdline = 1;
  4684. }
  4685. return 0;
  4686. }
  4687. static int diff_opt_unified(const struct option *opt,
  4688. const char *arg, int unset)
  4689. {
  4690. struct diff_options *options = opt->value;
  4691. char *s;
  4692. BUG_ON_OPT_NEG(unset);
  4693. if (arg) {
  4694. options->context = strtol(arg, &s, 10);
  4695. if (*s)
  4696. return error(_("%s expects a numerical value"), "--unified");
  4697. }
  4698. enable_patch_output(&options->output_format);
  4699. return 0;
  4700. }
  4701. static int diff_opt_word_diff(const struct option *opt,
  4702. const char *arg, int unset)
  4703. {
  4704. struct diff_options *options = opt->value;
  4705. BUG_ON_OPT_NEG(unset);
  4706. if (arg) {
  4707. if (!strcmp(arg, "plain"))
  4708. options->word_diff = DIFF_WORDS_PLAIN;
  4709. else if (!strcmp(arg, "color")) {
  4710. options->use_color = 1;
  4711. options->word_diff = DIFF_WORDS_COLOR;
  4712. }
  4713. else if (!strcmp(arg, "porcelain"))
  4714. options->word_diff = DIFF_WORDS_PORCELAIN;
  4715. else if (!strcmp(arg, "none"))
  4716. options->word_diff = DIFF_WORDS_NONE;
  4717. else
  4718. return error(_("bad --word-diff argument: %s"), arg);
  4719. } else {
  4720. if (options->word_diff == DIFF_WORDS_NONE)
  4721. options->word_diff = DIFF_WORDS_PLAIN;
  4722. }
  4723. return 0;
  4724. }
  4725. static int diff_opt_word_diff_regex(const struct option *opt,
  4726. const char *arg, int unset)
  4727. {
  4728. struct diff_options *options = opt->value;
  4729. BUG_ON_OPT_NEG(unset);
  4730. if (options->word_diff == DIFF_WORDS_NONE)
  4731. options->word_diff = DIFF_WORDS_PLAIN;
  4732. options->word_regex = arg;
  4733. return 0;
  4734. }
  4735. static void prep_parse_options(struct diff_options *options)
  4736. {
  4737. struct option parseopts[] = {
  4738. OPT_GROUP(N_("Diff output format options")),
  4739. OPT_BITOP('p', "patch", &options->output_format,
  4740. N_("generate patch"),
  4741. DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
  4742. OPT_BIT_F('s', "no-patch", &options->output_format,
  4743. N_("suppress diff output"),
  4744. DIFF_FORMAT_NO_OUTPUT, PARSE_OPT_NONEG),
  4745. OPT_BITOP('u', NULL, &options->output_format,
  4746. N_("generate patch"),
  4747. DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
  4748. OPT_CALLBACK_F('U', "unified", options, N_("<n>"),
  4749. N_("generate diffs with <n> lines context"),
  4750. PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified),
  4751. OPT_BOOL('W', "function-context", &options->flags.funccontext,
  4752. N_("generate diffs with <n> lines context")),
  4753. OPT_BIT_F(0, "raw", &options->output_format,
  4754. N_("generate the diff in raw format"),
  4755. DIFF_FORMAT_RAW, PARSE_OPT_NONEG),
  4756. OPT_BITOP(0, "patch-with-raw", &options->output_format,
  4757. N_("synonym for '-p --raw'"),
  4758. DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW,
  4759. DIFF_FORMAT_NO_OUTPUT),
  4760. OPT_BITOP(0, "patch-with-stat", &options->output_format,
  4761. N_("synonym for '-p --stat'"),
  4762. DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT,
  4763. DIFF_FORMAT_NO_OUTPUT),
  4764. OPT_BIT_F(0, "numstat", &options->output_format,
  4765. N_("machine friendly --stat"),
  4766. DIFF_FORMAT_NUMSTAT, PARSE_OPT_NONEG),
  4767. OPT_BIT_F(0, "shortstat", &options->output_format,
  4768. N_("output only the last line of --stat"),
  4769. DIFF_FORMAT_SHORTSTAT, PARSE_OPT_NONEG),
  4770. OPT_CALLBACK_F('X', "dirstat", options, N_("<param1,param2>..."),
  4771. N_("output the distribution of relative amount of changes for each sub-directory"),
  4772. PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
  4773. diff_opt_dirstat),
  4774. OPT_CALLBACK_F(0, "cumulative", options, NULL,
  4775. N_("synonym for --dirstat=cumulative"),
  4776. PARSE_OPT_NONEG | PARSE_OPT_NOARG,
  4777. diff_opt_dirstat),
  4778. OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1,param2>..."),
  4779. N_("synonym for --dirstat=files,param1,param2..."),
  4780. PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
  4781. diff_opt_dirstat),
  4782. OPT_BIT_F(0, "check", &options->output_format,
  4783. N_("warn if changes introduce conflict markers or whitespace errors"),
  4784. DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG),
  4785. OPT_BIT_F(0, "summary", &options->output_format,
  4786. N_("condensed summary such as creations, renames and mode changes"),
  4787. DIFF_FORMAT_SUMMARY, PARSE_OPT_NONEG),
  4788. OPT_BIT_F(0, "name-only", &options->output_format,
  4789. N_("show only names of changed files"),
  4790. DIFF_FORMAT_NAME, PARSE_OPT_NONEG),
  4791. OPT_BIT_F(0, "name-status", &options->output_format,
  4792. N_("show only names and status of changed files"),
  4793. DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG),
  4794. OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"),
  4795. N_("generate diffstat"),
  4796. PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat),
  4797. OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"),
  4798. N_("generate diffstat with a given width"),
  4799. PARSE_OPT_NONEG, diff_opt_stat),
  4800. OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"),
  4801. N_("generate diffstat with a given name width"),
  4802. PARSE_OPT_NONEG, diff_opt_stat),
  4803. OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"),
  4804. N_("generate diffstat with a given graph width"),
  4805. PARSE_OPT_NONEG, diff_opt_stat),
  4806. OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"),
  4807. N_("generate diffstat with limited lines"),
  4808. PARSE_OPT_NONEG, diff_opt_stat),
  4809. OPT_CALLBACK_F(0, "compact-summary", options, NULL,
  4810. N_("generate compact summary in diffstat"),
  4811. PARSE_OPT_NOARG, diff_opt_compact_summary),
  4812. OPT_CALLBACK_F(0, "binary", options, NULL,
  4813. N_("output a binary diff that can be applied"),
  4814. PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary),
  4815. OPT_BOOL(0, "full-index", &options->flags.full_index,
  4816. N_("show full pre- and post-image object names on the \"index\" lines")),
  4817. OPT_COLOR_FLAG(0, "color", &options->use_color,
  4818. N_("show colored diff")),
  4819. OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"),
  4820. N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
  4821. PARSE_OPT_NONEG, diff_opt_ws_error_highlight),
  4822. OPT_SET_INT('z', NULL, &options->line_termination,
  4823. N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
  4824. 0),
  4825. OPT__ABBREV(&options->abbrev),
  4826. OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"),
  4827. N_("show the given source prefix instead of \"a/\""),
  4828. PARSE_OPT_NONEG),
  4829. OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"),
  4830. N_("show the given destination prefix instead of \"b/\""),
  4831. PARSE_OPT_NONEG),
  4832. OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"),
  4833. N_("prepend an additional prefix to every line of output"),
  4834. PARSE_OPT_NONEG, diff_opt_line_prefix),
  4835. OPT_CALLBACK_F(0, "no-prefix", options, NULL,
  4836. N_("do not show any source or destination prefix"),
  4837. PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix),
  4838. OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext,
  4839. N_("show context between diff hunks up to the specified number of lines"),
  4840. PARSE_OPT_NONEG),
  4841. OPT_CALLBACK_F(0, "output-indicator-new",
  4842. &options->output_indicators[OUTPUT_INDICATOR_NEW],
  4843. N_("<char>"),
  4844. N_("specify the character to indicate a new line instead of '+'"),
  4845. PARSE_OPT_NONEG, diff_opt_char),
  4846. OPT_CALLBACK_F(0, "output-indicator-old",
  4847. &options->output_indicators[OUTPUT_INDICATOR_OLD],
  4848. N_("<char>"),
  4849. N_("specify the character to indicate an old line instead of '-'"),
  4850. PARSE_OPT_NONEG, diff_opt_char),
  4851. OPT_CALLBACK_F(0, "output-indicator-context",
  4852. &options->output_indicators[OUTPUT_INDICATOR_CONTEXT],
  4853. N_("<char>"),
  4854. N_("specify the character to indicate a context instead of ' '"),
  4855. PARSE_OPT_NONEG, diff_opt_char),
  4856. OPT_GROUP(N_("Diff rename options")),
  4857. OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"),
  4858. N_("break complete rewrite changes into pairs of delete and create"),
  4859. PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
  4860. diff_opt_break_rewrites),
  4861. OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"),
  4862. N_("detect renames"),
  4863. PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
  4864. diff_opt_find_renames),
  4865. OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete,
  4866. N_("omit the preimage for deletes"),
  4867. 1, PARSE_OPT_NONEG),
  4868. OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"),
  4869. N_("detect copies"),
  4870. PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
  4871. diff_opt_find_copies),
  4872. OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder,
  4873. N_("use unmodified files as source to find copies")),
  4874. OPT_SET_INT_F(0, "no-renames", &options->detect_rename,
  4875. N_("disable rename detection"),
  4876. 0, PARSE_OPT_NONEG),
  4877. OPT_BOOL(0, "rename-empty", &options->flags.rename_empty,
  4878. N_("use empty blobs as rename source")),
  4879. OPT_CALLBACK_F(0, "follow", options, NULL,
  4880. N_("continue listing the history of a file beyond renames"),
  4881. PARSE_OPT_NOARG, diff_opt_follow),
  4882. OPT_INTEGER('l', NULL, &options->rename_limit,
  4883. N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
  4884. OPT_GROUP(N_("Diff algorithm options")),
  4885. OPT_BIT(0, "minimal", &options->xdl_opts,
  4886. N_("produce the smallest possible diff"),
  4887. XDF_NEED_MINIMAL),
  4888. OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts,
  4889. N_("ignore whitespace when comparing lines"),
  4890. XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG),
  4891. OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts,
  4892. N_("ignore changes in amount of whitespace"),
  4893. XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG),
  4894. OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts,
  4895. N_("ignore changes in whitespace at EOL"),
  4896. XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG),
  4897. OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts,
  4898. N_("ignore carrier-return at the end of line"),
  4899. XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG),
  4900. OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts,
  4901. N_("ignore changes whose lines are all blank"),
  4902. XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG),
  4903. OPT_BIT(0, "indent-heuristic", &options->xdl_opts,
  4904. N_("heuristic to shift diff hunk boundaries for easy reading"),
  4905. XDF_INDENT_HEURISTIC),
  4906. OPT_CALLBACK_F(0, "patience", options, NULL,
  4907. N_("generate diff using the \"patience diff\" algorithm"),
  4908. PARSE_OPT_NONEG | PARSE_OPT_NOARG,
  4909. diff_opt_patience),
  4910. OPT_BITOP(0, "histogram", &options->xdl_opts,
  4911. N_("generate diff using the \"histogram diff\" algorithm"),
  4912. XDF_HISTOGRAM_DIFF, XDF_DIFF_ALGORITHM_MASK),
  4913. OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"),
  4914. N_("choose a diff algorithm"),
  4915. PARSE_OPT_NONEG, diff_opt_diff_algorithm),
  4916. OPT_CALLBACK_F(0, "anchored", options, N_("<text>"),
  4917. N_("generate diff using the \"anchored diff\" algorithm"),
  4918. PARSE_OPT_NONEG, diff_opt_anchored),
  4919. OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"),
  4920. N_("show word diff, using <mode> to delimit changed words"),
  4921. PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff),
  4922. OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"),
  4923. N_("use <regex> to decide what a word is"),
  4924. PARSE_OPT_NONEG, diff_opt_word_diff_regex),
  4925. OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"),
  4926. N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
  4927. PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_color_words),
  4928. OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"),
  4929. N_("moved lines of code are colored differently"),
  4930. PARSE_OPT_OPTARG, diff_opt_color_moved),
  4931. OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"),
  4932. N_("how white spaces are ignored in --color-moved"),
  4933. 0, diff_opt_color_moved_ws),
  4934. OPT_GROUP(N_("Other diff options")),
  4935. OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"),
  4936. N_("when run from subdir, exclude changes outside and show relative paths"),
  4937. PARSE_OPT_OPTARG,
  4938. diff_opt_relative),
  4939. OPT_BOOL('a', "text", &options->flags.text,
  4940. N_("treat all files as text")),
  4941. OPT_BOOL('R', NULL, &options->flags.reverse_diff,
  4942. N_("swap two inputs, reverse the diff")),
  4943. OPT_BOOL(0, "exit-code", &options->flags.exit_with_status,
  4944. N_("exit with 1 if there were differences, 0 otherwise")),
  4945. OPT_BOOL(0, "quiet", &options->flags.quick,
  4946. N_("disable all output of the program")),
  4947. OPT_BOOL(0, "ext-diff", &options->flags.allow_external,
  4948. N_("allow an external diff helper to be executed")),
  4949. OPT_CALLBACK_F(0, "textconv", options, NULL,
  4950. N_("run external text conversion filters when comparing binary files"),
  4951. PARSE_OPT_NOARG, diff_opt_textconv),
  4952. OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"),
  4953. N_("ignore changes to submodules in the diff generation"),
  4954. PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
  4955. diff_opt_ignore_submodules),
  4956. OPT_CALLBACK_F(0, "submodule", options, N_("<format>"),
  4957. N_("specify how differences in submodules are shown"),
  4958. PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
  4959. diff_opt_submodule),
  4960. OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index,
  4961. N_("hide 'git add -N' entries from the index"),
  4962. 1, PARSE_OPT_NONEG),
  4963. OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index,
  4964. N_("treat 'git add -N' entries as real in the index"),
  4965. 0, PARSE_OPT_NONEG),
  4966. OPT_CALLBACK_F('S', NULL, options, N_("<string>"),
  4967. N_("look for differences that change the number of occurrences of the specified string"),
  4968. 0, diff_opt_pickaxe_string),
  4969. OPT_CALLBACK_F('G', NULL, options, N_("<regex>"),
  4970. N_("look for differences that change the number of occurrences of the specified regex"),
  4971. 0, diff_opt_pickaxe_regex),
  4972. OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts,
  4973. N_("show all changes in the changeset with -S or -G"),
  4974. DIFF_PICKAXE_ALL, PARSE_OPT_NONEG),
  4975. OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts,
  4976. N_("treat <string> in -S as extended POSIX regular expression"),
  4977. DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG),
  4978. OPT_FILENAME('O', NULL, &options->orderfile,
  4979. N_("control the order in which files appear in the output")),
  4980. OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"),
  4981. N_("look for differences that change the number of occurrences of the specified object"),
  4982. PARSE_OPT_NONEG, diff_opt_find_object),
  4983. OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
  4984. N_("select files by diff type"),
  4985. PARSE_OPT_NONEG, diff_opt_diff_filter),
  4986. { OPTION_CALLBACK, 0, "output", options, N_("<file>"),
  4987. N_("Output to a specific file"),
  4988. PARSE_OPT_NONEG, NULL, 0, diff_opt_output },
  4989. OPT_END()
  4990. };
  4991. ALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));
  4992. memcpy(options->parseopts, parseopts, sizeof(parseopts));
  4993. }
  4994. int diff_opt_parse(struct diff_options *options,
  4995. const char **av, int ac, const char *prefix)
  4996. {
  4997. if (!prefix)
  4998. prefix = "";
  4999. ac = parse_options(ac, av, prefix, options->parseopts, NULL,
  5000. PARSE_OPT_KEEP_DASHDASH |
  5001. PARSE_OPT_KEEP_UNKNOWN |
  5002. PARSE_OPT_NO_INTERNAL_HELP |
  5003. PARSE_OPT_ONE_SHOT |
  5004. PARSE_OPT_STOP_AT_NON_OPTION);
  5005. return ac;
  5006. }
  5007. int parse_rename_score(const char **cp_p)
  5008. {
  5009. unsigned long num, scale;
  5010. int ch, dot;
  5011. const char *cp = *cp_p;
  5012. num = 0;
  5013. scale = 1;
  5014. dot = 0;
  5015. for (;;) {
  5016. ch = *cp;
  5017. if ( !dot && ch == '.' ) {
  5018. scale = 1;
  5019. dot = 1;
  5020. } else if ( ch == '%' ) {
  5021. scale = dot ? scale*100 : 100;
  5022. cp++; /* % is always at the end */
  5023. break;
  5024. } else if ( ch >= '0' && ch <= '9' ) {
  5025. if ( scale < 100000 ) {
  5026. scale *= 10;
  5027. num = (num*10) + (ch-'0');
  5028. }
  5029. } else {
  5030. break;
  5031. }
  5032. cp++;
  5033. }
  5034. *cp_p = cp;
  5035. /* user says num divided by scale and we say internally that
  5036. * is MAX_SCORE * num / scale.
  5037. */
  5038. return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
  5039. }
  5040. struct diff_queue_struct diff_queued_diff;
  5041. void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
  5042. {
  5043. ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
  5044. queue->queue[queue->nr++] = dp;
  5045. }
  5046. struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
  5047. struct diff_filespec *one,
  5048. struct diff_filespec *two)
  5049. {
  5050. struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
  5051. dp->one = one;
  5052. dp->two = two;
  5053. if (queue)
  5054. diff_q(queue, dp);
  5055. return dp;
  5056. }
  5057. void diff_free_filepair(struct diff_filepair *p)
  5058. {
  5059. free_filespec(p->one);
  5060. free_filespec(p->two);
  5061. free(p);
  5062. }
  5063. const char *diff_aligned_abbrev(const struct object_id *oid, int len)
  5064. {
  5065. int abblen;
  5066. const char *abbrev;
  5067. /* Do we want all 40 hex characters? */
  5068. if (len == the_hash_algo->hexsz)
  5069. return oid_to_hex(oid);
  5070. /* An abbreviated value is fine, possibly followed by an ellipsis. */
  5071. abbrev = diff_abbrev_oid(oid, len);
  5072. if (!print_sha1_ellipsis())
  5073. return abbrev;
  5074. abblen = strlen(abbrev);
  5075. /*
  5076. * In well-behaved cases, where the abbreviated result is the
  5077. * same as the requested length, append three dots after the
  5078. * abbreviation (hence the whole logic is limited to the case
  5079. * where abblen < 37); when the actual abbreviated result is a
  5080. * bit longer than the requested length, we reduce the number
  5081. * of dots so that they match the well-behaved ones. However,
  5082. * if the actual abbreviation is longer than the requested
  5083. * length by more than three, we give up on aligning, and add
  5084. * three dots anyway, to indicate that the output is not the
  5085. * full object name. Yes, this may be suboptimal, but this
  5086. * appears only in "diff --raw --abbrev" output and it is not
  5087. * worth the effort to change it now. Note that this would
  5088. * likely to work fine when the automatic sizing of default
  5089. * abbreviation length is used--we would be fed -1 in "len" in
  5090. * that case, and will end up always appending three-dots, but
  5091. * the automatic sizing is supposed to give abblen that ensures
  5092. * uniqueness across all objects (statistically speaking).
  5093. */
  5094. if (abblen < the_hash_algo->hexsz - 3) {
  5095. static char hex[GIT_MAX_HEXSZ + 1];
  5096. if (len < abblen && abblen <= len + 2)
  5097. xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
  5098. else
  5099. xsnprintf(hex, sizeof(hex), "%s...", abbrev);
  5100. return hex;
  5101. }
  5102. return oid_to_hex(oid);
  5103. }
  5104. static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
  5105. {
  5106. int line_termination = opt->line_termination;
  5107. int inter_name_termination = line_termination ? '\t' : '\0';
  5108. fprintf(opt->file, "%s", diff_line_prefix(opt));
  5109. if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
  5110. fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
  5111. diff_aligned_abbrev(&p->one->oid, opt->abbrev));
  5112. fprintf(opt->file, "%s ",
  5113. diff_aligned_abbrev(&p->two->oid, opt->abbrev));
  5114. }
  5115. if (p->score) {
  5116. fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
  5117. inter_name_termination);
  5118. } else {
  5119. fprintf(opt->file, "%c%c", p->status, inter_name_termination);
  5120. }
  5121. if (p->status == DIFF_STATUS_COPIED ||
  5122. p->status == DIFF_STATUS_RENAMED) {
  5123. const char *name_a, *name_b;
  5124. name_a = p->one->path;
  5125. name_b = p->two->path;
  5126. strip_prefix(opt->prefix_length, &name_a, &name_b);
  5127. write_name_quoted(name_a, opt->file, inter_name_termination);
  5128. write_name_quoted(name_b, opt->file, line_termination);
  5129. } else {
  5130. const char *name_a, *name_b;
  5131. name_a = p->one->mode ? p->one->path : p->two->path;
  5132. name_b = NULL;
  5133. strip_prefix(opt->prefix_length, &name_a, &name_b);
  5134. write_name_quoted(name_a, opt->file, line_termination);
  5135. }
  5136. }
  5137. int diff_unmodified_pair(struct diff_filepair *p)
  5138. {
  5139. /* This function is written stricter than necessary to support
  5140. * the currently implemented transformers, but the idea is to
  5141. * let transformers to produce diff_filepairs any way they want,
  5142. * and filter and clean them up here before producing the output.
  5143. */
  5144. struct diff_filespec *one = p->one, *two = p->two;
  5145. if (DIFF_PAIR_UNMERGED(p))
  5146. return 0; /* unmerged is interesting */
  5147. /* deletion, addition, mode or type change
  5148. * and rename are all interesting.
  5149. */
  5150. if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
  5151. DIFF_PAIR_MODE_CHANGED(p) ||
  5152. strcmp(one->path, two->path))
  5153. return 0;
  5154. /* both are valid and point at the same path. that is, we are
  5155. * dealing with a change.
  5156. */
  5157. if (one->oid_valid && two->oid_valid &&
  5158. oideq(&one->oid, &two->oid) &&
  5159. !one->dirty_submodule && !two->dirty_submodule)
  5160. return 1; /* no change */
  5161. if (!one->oid_valid && !two->oid_valid)
  5162. return 1; /* both look at the same file on the filesystem. */
  5163. return 0;
  5164. }
  5165. static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
  5166. {
  5167. if (diff_unmodified_pair(p))
  5168. return;
  5169. if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
  5170. (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
  5171. return; /* no tree diffs in patch format */
  5172. run_diff(p, o);
  5173. }
  5174. static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
  5175. struct diffstat_t *diffstat)
  5176. {
  5177. if (diff_unmodified_pair(p))
  5178. return;
  5179. if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
  5180. (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
  5181. return; /* no useful stat for tree diffs */
  5182. run_diffstat(p, o, diffstat);
  5183. }
  5184. static void diff_flush_checkdiff(struct diff_filepair *p,
  5185. struct diff_options *o)
  5186. {
  5187. if (diff_unmodified_pair(p))
  5188. return;
  5189. if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
  5190. (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
  5191. return; /* nothing to check in tree diffs */
  5192. run_checkdiff(p, o);
  5193. }
  5194. int diff_queue_is_empty(void)
  5195. {
  5196. struct diff_queue_struct *q = &diff_queued_diff;
  5197. int i;
  5198. for (i = 0; i < q->nr; i++)
  5199. if (!diff_unmodified_pair(q->queue[i]))
  5200. return 0;
  5201. return 1;
  5202. }
  5203. #if DIFF_DEBUG
  5204. void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
  5205. {
  5206. fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
  5207. x, one ? one : "",
  5208. s->path,
  5209. DIFF_FILE_VALID(s) ? "valid" : "invalid",
  5210. s->mode,
  5211. s->oid_valid ? oid_to_hex(&s->oid) : "");
  5212. fprintf(stderr, "queue[%d] %s size %lu\n",
  5213. x, one ? one : "",
  5214. s->size);
  5215. }
  5216. void diff_debug_filepair(const struct diff_filepair *p, int i)
  5217. {
  5218. diff_debug_filespec(p->one, i, "one");
  5219. diff_debug_filespec(p->two, i, "two");
  5220. fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
  5221. p->score, p->status ? p->status : '?',
  5222. p->one->rename_used, p->broken_pair);
  5223. }
  5224. void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
  5225. {
  5226. int i;
  5227. if (msg)
  5228. fprintf(stderr, "%s\n", msg);
  5229. fprintf(stderr, "q->nr = %d\n", q->nr);
  5230. for (i = 0; i < q->nr; i++) {
  5231. struct diff_filepair *p = q->queue[i];
  5232. diff_debug_filepair(p, i);
  5233. }
  5234. }
  5235. #endif
  5236. static void diff_resolve_rename_copy(void)
  5237. {
  5238. int i;
  5239. struct diff_filepair *p;
  5240. struct diff_queue_struct *q = &diff_queued_diff;
  5241. diff_debug_queue("resolve-rename-copy", q);
  5242. for (i = 0; i < q->nr; i++) {
  5243. p = q->queue[i];
  5244. p->status = 0; /* undecided */
  5245. if (DIFF_PAIR_UNMERGED(p))
  5246. p->status = DIFF_STATUS_UNMERGED;
  5247. else if (!DIFF_FILE_VALID(p->one))
  5248. p->status = DIFF_STATUS_ADDED;
  5249. else if (!DIFF_FILE_VALID(p->two))
  5250. p->status = DIFF_STATUS_DELETED;
  5251. else if (DIFF_PAIR_TYPE_CHANGED(p))
  5252. p->status = DIFF_STATUS_TYPE_CHANGED;
  5253. /* from this point on, we are dealing with a pair
  5254. * whose both sides are valid and of the same type, i.e.
  5255. * either in-place edit or rename/copy edit.
  5256. */
  5257. else if (DIFF_PAIR_RENAME(p)) {
  5258. /*
  5259. * A rename might have re-connected a broken
  5260. * pair up, causing the pathnames to be the
  5261. * same again. If so, that's not a rename at
  5262. * all, just a modification..
  5263. *
  5264. * Otherwise, see if this source was used for
  5265. * multiple renames, in which case we decrement
  5266. * the count, and call it a copy.
  5267. */
  5268. if (!strcmp(p->one->path, p->two->path))
  5269. p->status = DIFF_STATUS_MODIFIED;
  5270. else if (--p->one->rename_used > 0)
  5271. p->status = DIFF_STATUS_COPIED;
  5272. else
  5273. p->status = DIFF_STATUS_RENAMED;
  5274. }
  5275. else if (!oideq(&p->one->oid, &p->two->oid) ||
  5276. p->one->mode != p->two->mode ||
  5277. p->one->dirty_submodule ||
  5278. p->two->dirty_submodule ||
  5279. is_null_oid(&p->one->oid))
  5280. p->status = DIFF_STATUS_MODIFIED;
  5281. else {
  5282. /* This is a "no-change" entry and should not
  5283. * happen anymore, but prepare for broken callers.
  5284. */
  5285. error("feeding unmodified %s to diffcore",
  5286. p->one->path);
  5287. p->status = DIFF_STATUS_UNKNOWN;
  5288. }
  5289. }
  5290. diff_debug_queue("resolve-rename-copy done", q);
  5291. }
  5292. static int check_pair_status(struct diff_filepair *p)
  5293. {
  5294. switch (p->status) {
  5295. case DIFF_STATUS_UNKNOWN:
  5296. return 0;
  5297. case 0:
  5298. die("internal error in diff-resolve-rename-copy");
  5299. default:
  5300. return 1;
  5301. }
  5302. }
  5303. static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
  5304. {
  5305. int fmt = opt->output_format;
  5306. if (fmt & DIFF_FORMAT_CHECKDIFF)
  5307. diff_flush_checkdiff(p, opt);
  5308. else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
  5309. diff_flush_raw(p, opt);
  5310. else if (fmt & DIFF_FORMAT_NAME) {
  5311. const char *name_a, *name_b;
  5312. name_a = p->two->path;
  5313. name_b = NULL;
  5314. strip_prefix(opt->prefix_length, &name_a, &name_b);
  5315. fprintf(opt->file, "%s", diff_line_prefix(opt));
  5316. write_name_quoted(name_a, opt->file, opt->line_termination);
  5317. }
  5318. }
  5319. static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
  5320. {
  5321. struct strbuf sb = STRBUF_INIT;
  5322. if (fs->mode)
  5323. strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
  5324. else
  5325. strbuf_addf(&sb, " %s ", newdelete);
  5326. quote_c_style(fs->path, &sb, NULL, 0);
  5327. strbuf_addch(&sb, '\n');
  5328. emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
  5329. sb.buf, sb.len, 0);
  5330. strbuf_release(&sb);
  5331. }
  5332. static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
  5333. int show_name)
  5334. {
  5335. if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
  5336. struct strbuf sb = STRBUF_INIT;
  5337. strbuf_addf(&sb, " mode change %06o => %06o",
  5338. p->one->mode, p->two->mode);
  5339. if (show_name) {
  5340. strbuf_addch(&sb, ' ');
  5341. quote_c_style(p->two->path, &sb, NULL, 0);
  5342. }
  5343. strbuf_addch(&sb, '\n');
  5344. emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
  5345. sb.buf, sb.len, 0);
  5346. strbuf_release(&sb);
  5347. }
  5348. }
  5349. static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
  5350. struct diff_filepair *p)
  5351. {
  5352. struct strbuf sb = STRBUF_INIT;
  5353. struct strbuf names = STRBUF_INIT;
  5354. pprint_rename(&names, p->one->path, p->two->path);
  5355. strbuf_addf(&sb, " %s %s (%d%%)\n",
  5356. renamecopy, names.buf, similarity_index(p));
  5357. strbuf_release(&names);
  5358. emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
  5359. sb.buf, sb.len, 0);
  5360. show_mode_change(opt, p, 0);
  5361. strbuf_release(&sb);
  5362. }
  5363. static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
  5364. {
  5365. switch(p->status) {
  5366. case DIFF_STATUS_DELETED:
  5367. show_file_mode_name(opt, "delete", p->one);
  5368. break;
  5369. case DIFF_STATUS_ADDED:
  5370. show_file_mode_name(opt, "create", p->two);
  5371. break;
  5372. case DIFF_STATUS_COPIED:
  5373. show_rename_copy(opt, "copy", p);
  5374. break;
  5375. case DIFF_STATUS_RENAMED:
  5376. show_rename_copy(opt, "rename", p);
  5377. break;
  5378. default:
  5379. if (p->score) {
  5380. struct strbuf sb = STRBUF_INIT;
  5381. strbuf_addstr(&sb, " rewrite ");
  5382. quote_c_style(p->two->path, &sb, NULL, 0);
  5383. strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
  5384. emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
  5385. sb.buf, sb.len, 0);
  5386. strbuf_release(&sb);
  5387. }
  5388. show_mode_change(opt, p, !p->score);
  5389. break;
  5390. }
  5391. }
  5392. struct patch_id_t {
  5393. git_hash_ctx *ctx;
  5394. int patchlen;
  5395. };
  5396. static int remove_space(char *line, int len)
  5397. {
  5398. int i;
  5399. char *dst = line;
  5400. unsigned char c;
  5401. for (i = 0; i < len; i++)
  5402. if (!isspace((c = line[i])))
  5403. *dst++ = c;
  5404. return dst - line;
  5405. }
  5406. void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx)
  5407. {
  5408. unsigned char hash[GIT_MAX_RAWSZ];
  5409. unsigned short carry = 0;
  5410. int i;
  5411. the_hash_algo->final_fn(hash, ctx);
  5412. the_hash_algo->init_fn(ctx);
  5413. /* 20-byte sum, with carry */
  5414. for (i = 0; i < the_hash_algo->rawsz; ++i) {
  5415. carry += result->hash[i] + hash[i];
  5416. result->hash[i] = carry;
  5417. carry >>= 8;
  5418. }
  5419. }
  5420. static void patch_id_consume(void *priv, char *line, unsigned long len)
  5421. {
  5422. struct patch_id_t *data = priv;
  5423. int new_len;
  5424. if (len > 12 && starts_with(line, "\\ "))
  5425. return;
  5426. new_len = remove_space(line, len);
  5427. the_hash_algo->update_fn(data->ctx, line, new_len);
  5428. data->patchlen += new_len;
  5429. }
  5430. static void patch_id_add_string(git_hash_ctx *ctx, const char *str)
  5431. {
  5432. the_hash_algo->update_fn(ctx, str, strlen(str));
  5433. }
  5434. static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode)
  5435. {
  5436. /* large enough for 2^32 in octal */
  5437. char buf[12];
  5438. int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
  5439. the_hash_algo->update_fn(ctx, buf, len);
  5440. }
  5441. /* returns 0 upon success, and writes result into oid */
  5442. static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable)
  5443. {
  5444. struct diff_queue_struct *q = &diff_queued_diff;
  5445. int i;
  5446. git_hash_ctx ctx;
  5447. struct patch_id_t data;
  5448. the_hash_algo->init_fn(&ctx);
  5449. memset(&data, 0, sizeof(struct patch_id_t));
  5450. data.ctx = &ctx;
  5451. oidclr(oid);
  5452. for (i = 0; i < q->nr; i++) {
  5453. xpparam_t xpp;
  5454. xdemitconf_t xecfg;
  5455. mmfile_t mf1, mf2;
  5456. struct diff_filepair *p = q->queue[i];
  5457. int len1, len2;
  5458. memset(&xpp, 0, sizeof(xpp));
  5459. memset(&xecfg, 0, sizeof(xecfg));
  5460. if (p->status == 0)
  5461. return error("internal diff status error");
  5462. if (p->status == DIFF_STATUS_UNKNOWN)
  5463. continue;
  5464. if (diff_unmodified_pair(p))
  5465. continue;
  5466. if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
  5467. (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
  5468. continue;
  5469. if (DIFF_PAIR_UNMERGED(p))
  5470. continue;
  5471. diff_fill_oid_info(p->one, options->repo->index);
  5472. diff_fill_oid_info(p->two, options->repo->index);
  5473. len1 = remove_space(p->one->path, strlen(p->one->path));
  5474. len2 = remove_space(p->two->path, strlen(p->two->path));
  5475. patch_id_add_string(&ctx, "diff--git");
  5476. patch_id_add_string(&ctx, "a/");
  5477. the_hash_algo->update_fn(&ctx, p->one->path, len1);
  5478. patch_id_add_string(&ctx, "b/");
  5479. the_hash_algo->update_fn(&ctx, p->two->path, len2);
  5480. if (p->one->mode == 0) {
  5481. patch_id_add_string(&ctx, "newfilemode");
  5482. patch_id_add_mode(&ctx, p->two->mode);
  5483. patch_id_add_string(&ctx, "---/dev/null");
  5484. patch_id_add_string(&ctx, "+++b/");
  5485. the_hash_algo->update_fn(&ctx, p->two->path, len2);
  5486. } else if (p->two->mode == 0) {
  5487. patch_id_add_string(&ctx, "deletedfilemode");
  5488. patch_id_add_mode(&ctx, p->one->mode);
  5489. patch_id_add_string(&ctx, "---a/");
  5490. the_hash_algo->update_fn(&ctx, p->one->path, len1);
  5491. patch_id_add_string(&ctx, "+++/dev/null");
  5492. } else {
  5493. patch_id_add_string(&ctx, "---a/");
  5494. the_hash_algo->update_fn(&ctx, p->one->path, len1);
  5495. patch_id_add_string(&ctx, "+++b/");
  5496. the_hash_algo->update_fn(&ctx, p->two->path, len2);
  5497. }
  5498. if (diff_header_only)
  5499. continue;
  5500. if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||
  5501. fill_mmfile(options->repo, &mf2, p->two) < 0)
  5502. return error("unable to read files to diff");
  5503. if (diff_filespec_is_binary(options->repo, p->one) ||
  5504. diff_filespec_is_binary(options->repo, p->two)) {
  5505. the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid),
  5506. the_hash_algo->hexsz);
  5507. the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid),
  5508. the_hash_algo->hexsz);
  5509. continue;
  5510. }
  5511. xpp.flags = 0;
  5512. xecfg.ctxlen = 3;
  5513. xecfg.flags = 0;
  5514. if (xdi_diff_outf(&mf1, &mf2, discard_hunk_line,
  5515. patch_id_consume, &data, &xpp, &xecfg))
  5516. return error("unable to generate patch-id diff for %s",
  5517. p->one->path);
  5518. if (stable)
  5519. flush_one_hunk(oid, &ctx);
  5520. }
  5521. if (!stable)
  5522. the_hash_algo->final_fn(oid->hash, &ctx);
  5523. return 0;
  5524. }
  5525. int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable)
  5526. {
  5527. struct diff_queue_struct *q = &diff_queued_diff;
  5528. int i;
  5529. int result = diff_get_patch_id(options, oid, diff_header_only, stable);
  5530. for (i = 0; i < q->nr; i++)
  5531. diff_free_filepair(q->queue[i]);
  5532. free(q->queue);
  5533. DIFF_QUEUE_CLEAR(q);
  5534. return result;
  5535. }
  5536. static int is_summary_empty(const struct diff_queue_struct *q)
  5537. {
  5538. int i;
  5539. for (i = 0; i < q->nr; i++) {
  5540. const struct diff_filepair *p = q->queue[i];
  5541. switch (p->status) {
  5542. case DIFF_STATUS_DELETED:
  5543. case DIFF_STATUS_ADDED:
  5544. case DIFF_STATUS_COPIED:
  5545. case DIFF_STATUS_RENAMED:
  5546. return 0;
  5547. default:
  5548. if (p->score)
  5549. return 0;
  5550. if (p->one->mode && p->two->mode &&
  5551. p->one->mode != p->two->mode)
  5552. return 0;
  5553. break;
  5554. }
  5555. }
  5556. return 1;
  5557. }
  5558. static const char rename_limit_warning[] =
  5559. N_("inexact rename detection was skipped due to too many files.");
  5560. static const char degrade_cc_to_c_warning[] =
  5561. N_("only found copies from modified paths due to too many files.");
  5562. static const char rename_limit_advice[] =
  5563. N_("you may want to set your %s variable to at least "
  5564. "%d and retry the command.");
  5565. void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
  5566. {
  5567. fflush(stdout);
  5568. if (degraded_cc)
  5569. warning(_(degrade_cc_to_c_warning));
  5570. else if (needed)
  5571. warning(_(rename_limit_warning));
  5572. else
  5573. return;
  5574. if (0 < needed)
  5575. warning(_(rename_limit_advice), varname, needed);
  5576. }
  5577. static void diff_flush_patch_all_file_pairs(struct diff_options *o)
  5578. {
  5579. int i;
  5580. static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
  5581. struct diff_queue_struct *q = &diff_queued_diff;
  5582. if (WSEH_NEW & WS_RULE_MASK)
  5583. BUG("WS rules bit mask overlaps with diff symbol flags");
  5584. if (o->color_moved)
  5585. o->emitted_symbols = &esm;
  5586. for (i = 0; i < q->nr; i++) {
  5587. struct diff_filepair *p = q->queue[i];
  5588. if (check_pair_status(p))
  5589. diff_flush_patch(p, o);
  5590. }
  5591. if (o->emitted_symbols) {
  5592. if (o->color_moved) {
  5593. struct hashmap add_lines, del_lines;
  5594. if (o->color_moved_ws_handling &
  5595. COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
  5596. o->color_moved_ws_handling |= XDF_IGNORE_WHITESPACE;
  5597. hashmap_init(&del_lines, moved_entry_cmp, o, 0);
  5598. hashmap_init(&add_lines, moved_entry_cmp, o, 0);
  5599. add_lines_to_move_detection(o, &add_lines, &del_lines);
  5600. mark_color_as_moved(o, &add_lines, &del_lines);
  5601. if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
  5602. dim_moved_lines(o);
  5603. hashmap_free_entries(&add_lines, struct moved_entry,
  5604. ent);
  5605. hashmap_free_entries(&del_lines, struct moved_entry,
  5606. ent);
  5607. }
  5608. for (i = 0; i < esm.nr; i++)
  5609. emit_diff_symbol_from_struct(o, &esm.buf[i]);
  5610. for (i = 0; i < esm.nr; i++)
  5611. free((void *)esm.buf[i].line);
  5612. esm.nr = 0;
  5613. o->emitted_symbols = NULL;
  5614. }
  5615. }
  5616. void diff_flush(struct diff_options *options)
  5617. {
  5618. struct diff_queue_struct *q = &diff_queued_diff;
  5619. int i, output_format = options->output_format;
  5620. int separator = 0;
  5621. int dirstat_by_line = 0;
  5622. /*
  5623. * Order: raw, stat, summary, patch
  5624. * or: name/name-status/checkdiff (other bits clear)
  5625. */
  5626. if (!q->nr)
  5627. goto free_queue;
  5628. if (output_format & (DIFF_FORMAT_RAW |
  5629. DIFF_FORMAT_NAME |
  5630. DIFF_FORMAT_NAME_STATUS |
  5631. DIFF_FORMAT_CHECKDIFF)) {
  5632. for (i = 0; i < q->nr; i++) {
  5633. struct diff_filepair *p = q->queue[i];
  5634. if (check_pair_status(p))
  5635. flush_one_pair(p, options);
  5636. }
  5637. separator++;
  5638. }
  5639. if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line)
  5640. dirstat_by_line = 1;
  5641. if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) ||
  5642. dirstat_by_line) {
  5643. struct diffstat_t diffstat;
  5644. compute_diffstat(options, &diffstat, q);
  5645. if (output_format & DIFF_FORMAT_NUMSTAT)
  5646. show_numstat(&diffstat, options);
  5647. if (output_format & DIFF_FORMAT_DIFFSTAT)
  5648. show_stats(&diffstat, options);
  5649. if (output_format & DIFF_FORMAT_SHORTSTAT)
  5650. show_shortstats(&diffstat, options);
  5651. if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line)
  5652. show_dirstat_by_line(&diffstat, options);
  5653. free_diffstat_info(&diffstat);
  5654. separator++;
  5655. }
  5656. if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
  5657. show_dirstat(options);
  5658. if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
  5659. for (i = 0; i < q->nr; i++) {
  5660. diff_summary(options, q->queue[i]);
  5661. }
  5662. separator++;
  5663. }
  5664. if (output_format & DIFF_FORMAT_NO_OUTPUT &&
  5665. options->flags.exit_with_status &&
  5666. options->flags.diff_from_contents) {
  5667. /*
  5668. * run diff_flush_patch for the exit status. setting
  5669. * options->file to /dev/null should be safe, because we
  5670. * aren't supposed to produce any output anyway.
  5671. */
  5672. if (options->close_file)
  5673. fclose(options->file);
  5674. options->file = xfopen("/dev/null", "w");
  5675. options->close_file = 1;
  5676. options->color_moved = 0;
  5677. for (i = 0; i < q->nr; i++) {
  5678. struct diff_filepair *p = q->queue[i];
  5679. if (check_pair_status(p))
  5680. diff_flush_patch(p, options);
  5681. if (options->found_changes)
  5682. break;
  5683. }
  5684. }
  5685. if (output_format & DIFF_FORMAT_PATCH) {
  5686. if (separator) {
  5687. emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
  5688. if (options->stat_sep)
  5689. /* attach patch instead of inline */
  5690. emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
  5691. NULL, 0, 0);
  5692. }
  5693. diff_flush_patch_all_file_pairs(options);
  5694. }
  5695. if (output_format & DIFF_FORMAT_CALLBACK)
  5696. options->format_callback(q, options, options->format_callback_data);
  5697. for (i = 0; i < q->nr; i++)
  5698. diff_free_filepair(q->queue[i]);
  5699. free_queue:
  5700. free(q->queue);
  5701. DIFF_QUEUE_CLEAR(q);
  5702. if (options->close_file)
  5703. fclose(options->file);
  5704. /*
  5705. * Report the content-level differences with HAS_CHANGES;
  5706. * diff_addremove/diff_change does not set the bit when
  5707. * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
  5708. */
  5709. if (options->flags.diff_from_contents) {
  5710. if (options->found_changes)
  5711. options->flags.has_changes = 1;
  5712. else
  5713. options->flags.has_changes = 0;
  5714. }
  5715. }
  5716. static int match_filter(const struct diff_options *options, const struct diff_filepair *p)
  5717. {
  5718. return (((p->status == DIFF_STATUS_MODIFIED) &&
  5719. ((p->score &&
  5720. filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) ||
  5721. (!p->score &&
  5722. filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) ||
  5723. ((p->status != DIFF_STATUS_MODIFIED) &&
  5724. filter_bit_tst(p->status, options)));
  5725. }
  5726. static void diffcore_apply_filter(struct diff_options *options)
  5727. {
  5728. int i;
  5729. struct diff_queue_struct *q = &diff_queued_diff;
  5730. struct diff_queue_struct outq;
  5731. DIFF_QUEUE_CLEAR(&outq);
  5732. if (!options->filter)
  5733. return;
  5734. if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) {
  5735. int found;
  5736. for (i = found = 0; !found && i < q->nr; i++) {
  5737. if (match_filter(options, q->queue[i]))
  5738. found++;
  5739. }
  5740. if (found)
  5741. return;
  5742. /* otherwise we will clear the whole queue
  5743. * by copying the empty outq at the end of this
  5744. * function, but first clear the current entries
  5745. * in the queue.
  5746. */
  5747. for (i = 0; i < q->nr; i++)
  5748. diff_free_filepair(q->queue[i]);
  5749. }
  5750. else {
  5751. /* Only the matching ones */
  5752. for (i = 0; i < q->nr; i++) {
  5753. struct diff_filepair *p = q->queue[i];
  5754. if (match_filter(options, p))
  5755. diff_q(&outq, p);
  5756. else
  5757. diff_free_filepair(p);
  5758. }
  5759. }
  5760. free(q->queue);
  5761. *q = outq;
  5762. }
  5763. /* Check whether two filespecs with the same mode and size are identical */
  5764. static int diff_filespec_is_identical(struct repository *r,
  5765. struct diff_filespec *one,
  5766. struct diff_filespec *two)
  5767. {
  5768. if (S_ISGITLINK(one->mode))
  5769. return 0;
  5770. if (diff_populate_filespec(r, one, NULL))
  5771. return 0;
  5772. if (diff_populate_filespec(r, two, NULL))
  5773. return 0;
  5774. return !memcmp(one->data, two->data, one->size);
  5775. }
  5776. static int diff_filespec_check_stat_unmatch(struct repository *r,
  5777. struct diff_filepair *p)
  5778. {
  5779. struct diff_populate_filespec_options dpf_options = {
  5780. .check_size_only = 1,
  5781. .missing_object_cb = diff_queued_diff_prefetch,
  5782. .missing_object_data = r,
  5783. };
  5784. if (p->done_skip_stat_unmatch)
  5785. return p->skip_stat_unmatch_result;
  5786. p->done_skip_stat_unmatch = 1;
  5787. p->skip_stat_unmatch_result = 0;
  5788. /*
  5789. * 1. Entries that come from stat info dirtiness
  5790. * always have both sides (iow, not create/delete),
  5791. * one side of the object name is unknown, with
  5792. * the same mode and size. Keep the ones that
  5793. * do not match these criteria. They have real
  5794. * differences.
  5795. *
  5796. * 2. At this point, the file is known to be modified,
  5797. * with the same mode and size, and the object
  5798. * name of one side is unknown. Need to inspect
  5799. * the identical contents.
  5800. */
  5801. if (!DIFF_FILE_VALID(p->one) || /* (1) */
  5802. !DIFF_FILE_VALID(p->two) ||
  5803. (p->one->oid_valid && p->two->oid_valid) ||
  5804. (p->one->mode != p->two->mode) ||
  5805. diff_populate_filespec(r, p->one, &dpf_options) ||
  5806. diff_populate_filespec(r, p->two, &dpf_options) ||
  5807. (p->one->size != p->two->size) ||
  5808. !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */
  5809. p->skip_stat_unmatch_result = 1;
  5810. return p->skip_stat_unmatch_result;
  5811. }
  5812. static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
  5813. {
  5814. int i;
  5815. struct diff_queue_struct *q = &diff_queued_diff;
  5816. struct diff_queue_struct outq;
  5817. DIFF_QUEUE_CLEAR(&outq);
  5818. for (i = 0; i < q->nr; i++) {
  5819. struct diff_filepair *p = q->queue[i];
  5820. if (diff_filespec_check_stat_unmatch(diffopt->repo, p))
  5821. diff_q(&outq, p);
  5822. else {
  5823. /*
  5824. * The caller can subtract 1 from skip_stat_unmatch
  5825. * to determine how many paths were dirty only
  5826. * due to stat info mismatch.
  5827. */
  5828. if (!diffopt->flags.no_index)
  5829. diffopt->skip_stat_unmatch++;
  5830. diff_free_filepair(p);
  5831. }
  5832. }
  5833. free(q->queue);
  5834. *q = outq;
  5835. }
  5836. static int diffnamecmp(const void *a_, const void *b_)
  5837. {
  5838. const struct diff_filepair *a = *((const struct diff_filepair **)a_);
  5839. const struct diff_filepair *b = *((const struct diff_filepair **)b_);
  5840. const char *name_a, *name_b;
  5841. name_a = a->one ? a->one->path : a->two->path;
  5842. name_b = b->one ? b->one->path : b->two->path;
  5843. return strcmp(name_a, name_b);
  5844. }
  5845. void diffcore_fix_diff_index(void)
  5846. {
  5847. struct diff_queue_struct *q = &diff_queued_diff;
  5848. QSORT(q->queue, q->nr, diffnamecmp);
  5849. }
  5850. void diff_add_if_missing(struct repository *r,
  5851. struct oid_array *to_fetch,
  5852. const struct diff_filespec *filespec)
  5853. {
  5854. if (filespec && filespec->oid_valid &&
  5855. !S_ISGITLINK(filespec->mode) &&
  5856. oid_object_info_extended(r, &filespec->oid, NULL,
  5857. OBJECT_INFO_FOR_PREFETCH))
  5858. oid_array_append(to_fetch, &filespec->oid);
  5859. }
  5860. void diff_queued_diff_prefetch(void *repository)
  5861. {
  5862. struct repository *repo = repository;
  5863. int i;
  5864. struct diff_queue_struct *q = &diff_queued_diff;
  5865. struct oid_array to_fetch = OID_ARRAY_INIT;
  5866. for (i = 0; i < q->nr; i++) {
  5867. struct diff_filepair *p = q->queue[i];
  5868. diff_add_if_missing(repo, &to_fetch, p->one);
  5869. diff_add_if_missing(repo, &to_fetch, p->two);
  5870. }
  5871. /*
  5872. * NEEDSWORK: Consider deduplicating the OIDs sent.
  5873. */
  5874. promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr);
  5875. oid_array_clear(&to_fetch);
  5876. }
  5877. void diffcore_std(struct diff_options *options)
  5878. {
  5879. int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT |
  5880. DIFF_FORMAT_NUMSTAT |
  5881. DIFF_FORMAT_PATCH |
  5882. DIFF_FORMAT_SHORTSTAT |
  5883. DIFF_FORMAT_DIRSTAT;
  5884. /*
  5885. * Check if the user requested a blob-data-requiring diff output and/or
  5886. * break-rewrite detection (which requires blob data). If yes, prefetch
  5887. * the diff pairs.
  5888. *
  5889. * If no prefetching occurs, diffcore_rename() will prefetch if it
  5890. * decides that it needs inexact rename detection.
  5891. */
  5892. if (options->repo == the_repository && has_promisor_remote() &&
  5893. (options->output_format & output_formats_to_prefetch ||
  5894. options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
  5895. diff_queued_diff_prefetch(options->repo);
  5896. /* NOTE please keep the following in sync with diff_tree_combined() */
  5897. if (options->skip_stat_unmatch)
  5898. diffcore_skip_stat_unmatch(options);
  5899. if (!options->found_follow) {
  5900. /* See try_to_follow_renames() in tree-diff.c */
  5901. if (options->break_opt != -1)
  5902. diffcore_break(options->repo,
  5903. options->break_opt);
  5904. if (options->detect_rename)
  5905. diffcore_rename(options);
  5906. if (options->break_opt != -1)
  5907. diffcore_merge_broken();
  5908. }
  5909. if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
  5910. diffcore_pickaxe(options);
  5911. if (options->orderfile)
  5912. diffcore_order(options->orderfile);
  5913. if (!options->found_follow)
  5914. /* See try_to_follow_renames() in tree-diff.c */
  5915. diff_resolve_rename_copy();
  5916. diffcore_apply_filter(options);
  5917. if (diff_queued_diff.nr && !options->flags.diff_from_contents)
  5918. options->flags.has_changes = 1;
  5919. else
  5920. options->flags.has_changes = 0;
  5921. options->found_follow = 0;
  5922. }
  5923. int diff_result_code(struct diff_options *opt, int status)
  5924. {
  5925. int result = 0;
  5926. diff_warn_rename_limit("diff.renameLimit",
  5927. opt->needed_rename_limit,
  5928. opt->degraded_cc_to_c);
  5929. if (!opt->flags.exit_with_status &&
  5930. !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
  5931. return status;
  5932. if (opt->flags.exit_with_status &&
  5933. opt->flags.has_changes)
  5934. result |= 01;
  5935. if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
  5936. opt->flags.check_failed)
  5937. result |= 02;
  5938. return result;
  5939. }
  5940. int diff_can_quit_early(struct diff_options *opt)
  5941. {
  5942. return (opt->flags.quick &&
  5943. !opt->filter &&
  5944. opt->flags.has_changes);
  5945. }
  5946. /*
  5947. * Shall changes to this submodule be ignored?
  5948. *
  5949. * Submodule changes can be configured to be ignored separately for each path,
  5950. * but that configuration can be overridden from the command line.
  5951. */
  5952. static int is_submodule_ignored(const char *path, struct diff_options *options)
  5953. {
  5954. int ignored = 0;
  5955. struct diff_flags orig_flags = options->flags;
  5956. if (!options->flags.override_submodule_config)
  5957. set_diffopt_flags_from_submodule_config(options, path);
  5958. if (options->flags.ignore_submodules)
  5959. ignored = 1;
  5960. options->flags = orig_flags;
  5961. return ignored;
  5962. }
  5963. void compute_diffstat(struct diff_options *options,
  5964. struct diffstat_t *diffstat,
  5965. struct diff_queue_struct *q)
  5966. {
  5967. int i;
  5968. memset(diffstat, 0, sizeof(struct diffstat_t));
  5969. for (i = 0; i < q->nr; i++) {
  5970. struct diff_filepair *p = q->queue[i];
  5971. if (check_pair_status(p))
  5972. diff_flush_stat(p, options, diffstat);
  5973. }
  5974. }
  5975. void diff_addremove(struct diff_options *options,
  5976. int addremove, unsigned mode,
  5977. const struct object_id *oid,
  5978. int oid_valid,
  5979. const char *concatpath, unsigned dirty_submodule)
  5980. {
  5981. struct diff_filespec *one, *two;
  5982. if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
  5983. return;
  5984. /* This may look odd, but it is a preparation for
  5985. * feeding "there are unchanged files which should
  5986. * not produce diffs, but when you are doing copy
  5987. * detection you would need them, so here they are"
  5988. * entries to the diff-core. They will be prefixed
  5989. * with something like '=' or '*' (I haven't decided
  5990. * which but should not make any difference).
  5991. * Feeding the same new and old to diff_change()
  5992. * also has the same effect.
  5993. * Before the final output happens, they are pruned after
  5994. * merged into rename/copy pairs as appropriate.
  5995. */
  5996. if (options->flags.reverse_diff)
  5997. addremove = (addremove == '+' ? '-' :
  5998. addremove == '-' ? '+' : addremove);
  5999. if (options->prefix &&
  6000. strncmp(concatpath, options->prefix, options->prefix_length))
  6001. return;
  6002. one = alloc_filespec(concatpath);
  6003. two = alloc_filespec(concatpath);
  6004. if (addremove != '+')
  6005. fill_filespec(one, oid, oid_valid, mode);
  6006. if (addremove != '-') {
  6007. fill_filespec(two, oid, oid_valid, mode);
  6008. two->dirty_submodule = dirty_submodule;
  6009. }
  6010. diff_queue(&diff_queued_diff, one, two);
  6011. if (!options->flags.diff_from_contents)
  6012. options->flags.has_changes = 1;
  6013. }
  6014. void diff_change(struct diff_options *options,
  6015. unsigned old_mode, unsigned new_mode,
  6016. const struct object_id *old_oid,
  6017. const struct object_id *new_oid,
  6018. int old_oid_valid, int new_oid_valid,
  6019. const char *concatpath,
  6020. unsigned old_dirty_submodule, unsigned new_dirty_submodule)
  6021. {
  6022. struct diff_filespec *one, *two;
  6023. struct diff_filepair *p;
  6024. if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
  6025. is_submodule_ignored(concatpath, options))
  6026. return;
  6027. if (options->flags.reverse_diff) {
  6028. SWAP(old_mode, new_mode);
  6029. SWAP(old_oid, new_oid);
  6030. SWAP(old_oid_valid, new_oid_valid);
  6031. SWAP(old_dirty_submodule, new_dirty_submodule);
  6032. }
  6033. if (options->prefix &&
  6034. strncmp(concatpath, options->prefix, options->prefix_length))
  6035. return;
  6036. one = alloc_filespec(concatpath);
  6037. two = alloc_filespec(concatpath);
  6038. fill_filespec(one, old_oid, old_oid_valid, old_mode);
  6039. fill_filespec(two, new_oid, new_oid_valid, new_mode);
  6040. one->dirty_submodule = old_dirty_submodule;
  6041. two->dirty_submodule = new_dirty_submodule;
  6042. p = diff_queue(&diff_queued_diff, one, two);
  6043. if (options->flags.diff_from_contents)
  6044. return;
  6045. if (options->flags.quick && options->skip_stat_unmatch &&
  6046. !diff_filespec_check_stat_unmatch(options->repo, p)) {
  6047. diff_free_filespec_data(p->one);
  6048. diff_free_filespec_data(p->two);
  6049. return;
  6050. }
  6051. options->flags.has_changes = 1;
  6052. }
  6053. struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)
  6054. {
  6055. struct diff_filepair *pair;
  6056. struct diff_filespec *one, *two;
  6057. if (options->prefix &&
  6058. strncmp(path, options->prefix, options->prefix_length))
  6059. return NULL;
  6060. one = alloc_filespec(path);
  6061. two = alloc_filespec(path);
  6062. pair = diff_queue(&diff_queued_diff, one, two);
  6063. pair->is_unmerged = 1;
  6064. return pair;
  6065. }
  6066. static char *run_textconv(struct repository *r,
  6067. const char *pgm,
  6068. struct diff_filespec *spec,
  6069. size_t *outsize)
  6070. {
  6071. struct diff_tempfile *temp;
  6072. const char *argv[3];
  6073. const char **arg = argv;
  6074. struct child_process child = CHILD_PROCESS_INIT;
  6075. struct strbuf buf = STRBUF_INIT;
  6076. int err = 0;
  6077. temp = prepare_temp_file(r, spec->path, spec);
  6078. *arg++ = pgm;
  6079. *arg++ = temp->name;
  6080. *arg = NULL;
  6081. child.use_shell = 1;
  6082. child.argv = argv;
  6083. child.out = -1;
  6084. if (start_command(&child)) {
  6085. remove_tempfile();
  6086. return NULL;
  6087. }
  6088. if (strbuf_read(&buf, child.out, 0) < 0)
  6089. err = error("error reading from textconv command '%s'", pgm);
  6090. close(child.out);
  6091. if (finish_command(&child) || err) {
  6092. strbuf_release(&buf);
  6093. remove_tempfile();
  6094. return NULL;
  6095. }
  6096. remove_tempfile();
  6097. return strbuf_detach(&buf, outsize);
  6098. }
  6099. size_t fill_textconv(struct repository *r,
  6100. struct userdiff_driver *driver,
  6101. struct diff_filespec *df,
  6102. char **outbuf)
  6103. {
  6104. size_t size;
  6105. if (!driver) {
  6106. if (!DIFF_FILE_VALID(df)) {
  6107. *outbuf = "";
  6108. return 0;
  6109. }
  6110. if (diff_populate_filespec(r, df, NULL))
  6111. die("unable to read files to diff");
  6112. *outbuf = df->data;
  6113. return df->size;
  6114. }
  6115. if (!driver->textconv)
  6116. BUG("fill_textconv called with non-textconv driver");
  6117. if (driver->textconv_cache && df->oid_valid) {
  6118. *outbuf = notes_cache_get(driver->textconv_cache,
  6119. &df->oid,
  6120. &size);
  6121. if (*outbuf)
  6122. return size;
  6123. }
  6124. *outbuf = run_textconv(r, driver->textconv, df, &size);
  6125. if (!*outbuf)
  6126. die("unable to read files to diff");
  6127. if (driver->textconv_cache && df->oid_valid) {
  6128. /* ignore errors, as we might be in a readonly repository */
  6129. notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
  6130. size);
  6131. /*
  6132. * we could save up changes and flush them all at the end,
  6133. * but we would need an extra call after all diffing is done.
  6134. * Since generating a cache entry is the slow path anyway,
  6135. * this extra overhead probably isn't a big deal.
  6136. */
  6137. notes_cache_write(driver->textconv_cache);
  6138. }
  6139. return size;
  6140. }
  6141. int textconv_object(struct repository *r,
  6142. const char *path,
  6143. unsigned mode,
  6144. const struct object_id *oid,
  6145. int oid_valid,
  6146. char **buf,
  6147. unsigned long *buf_size)
  6148. {
  6149. struct diff_filespec *df;
  6150. struct userdiff_driver *textconv;
  6151. df = alloc_filespec(path);
  6152. fill_filespec(df, oid, oid_valid, mode);
  6153. textconv = get_textconv(r, df);
  6154. if (!textconv) {
  6155. free_filespec(df);
  6156. return 0;
  6157. }
  6158. *buf_size = fill_textconv(r, textconv, df, buf);
  6159. free_filespec(df);
  6160. return 1;
  6161. }
  6162. void setup_diff_pager(struct diff_options *opt)
  6163. {
  6164. /*
  6165. * If the user asked for our exit code, then either they want --quiet
  6166. * or --exit-code. We should definitely not bother with a pager in the
  6167. * former case, as we will generate no output. Since we still properly
  6168. * report our exit code even when a pager is run, we _could_ run a
  6169. * pager with --exit-code. But since we have not done so historically,
  6170. * and because it is easy to find people oneline advising "git diff
  6171. * --exit-code" in hooks and other scripts, we do not do so.
  6172. */
  6173. if (!opt->flags.exit_with_status &&
  6174. check_pager_config("diff") != 0)
  6175. setup_pager();
  6176. }