revision.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217
  1. #include "cache.h"
  2. #include "object-store.h"
  3. #include "tag.h"
  4. #include "blob.h"
  5. #include "tree.h"
  6. #include "commit.h"
  7. #include "diff.h"
  8. #include "refs.h"
  9. #include "revision.h"
  10. #include "repository.h"
  11. #include "graph.h"
  12. #include "grep.h"
  13. #include "reflog-walk.h"
  14. #include "patch-ids.h"
  15. #include "decorate.h"
  16. #include "log-tree.h"
  17. #include "string-list.h"
  18. #include "line-log.h"
  19. #include "mailmap.h"
  20. #include "commit-slab.h"
  21. #include "dir.h"
  22. #include "cache-tree.h"
  23. #include "bisect.h"
  24. #include "packfile.h"
  25. #include "worktree.h"
  26. #include "strvec.h"
  27. #include "commit-reach.h"
  28. #include "commit-graph.h"
  29. #include "prio-queue.h"
  30. #include "hashmap.h"
  31. #include "utf8.h"
  32. #include "bloom.h"
  33. #include "json-writer.h"
  34. volatile show_early_output_fn_t show_early_output;
  35. static const char *term_bad;
  36. static const char *term_good;
  37. implement_shared_commit_slab(revision_sources, char *);
  38. static inline int want_ancestry(const struct rev_info *revs);
  39. void show_object_with_name(FILE *out, struct object *obj, const char *name)
  40. {
  41. const char *p;
  42. fprintf(out, "%s ", oid_to_hex(&obj->oid));
  43. for (p = name; *p && *p != '\n'; p++)
  44. fputc(*p, out);
  45. fputc('\n', out);
  46. }
  47. static void mark_blob_uninteresting(struct blob *blob)
  48. {
  49. if (!blob)
  50. return;
  51. if (blob->object.flags & UNINTERESTING)
  52. return;
  53. blob->object.flags |= UNINTERESTING;
  54. }
  55. static void mark_tree_contents_uninteresting(struct repository *r,
  56. struct tree *tree)
  57. {
  58. struct tree_desc desc;
  59. struct name_entry entry;
  60. if (parse_tree_gently(tree, 1) < 0)
  61. return;
  62. init_tree_desc(&desc, tree->buffer, tree->size);
  63. while (tree_entry(&desc, &entry)) {
  64. switch (object_type(entry.mode)) {
  65. case OBJ_TREE:
  66. mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
  67. break;
  68. case OBJ_BLOB:
  69. mark_blob_uninteresting(lookup_blob(r, &entry.oid));
  70. break;
  71. default:
  72. /* Subproject commit - not in this repository */
  73. break;
  74. }
  75. }
  76. /*
  77. * We don't care about the tree any more
  78. * after it has been marked uninteresting.
  79. */
  80. free_tree_buffer(tree);
  81. }
  82. void mark_tree_uninteresting(struct repository *r, struct tree *tree)
  83. {
  84. struct object *obj;
  85. if (!tree)
  86. return;
  87. obj = &tree->object;
  88. if (obj->flags & UNINTERESTING)
  89. return;
  90. obj->flags |= UNINTERESTING;
  91. mark_tree_contents_uninteresting(r, tree);
  92. }
  93. struct path_and_oids_entry {
  94. struct hashmap_entry ent;
  95. char *path;
  96. struct oidset trees;
  97. };
  98. static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
  99. const struct hashmap_entry *eptr,
  100. const struct hashmap_entry *entry_or_key,
  101. const void *keydata)
  102. {
  103. const struct path_and_oids_entry *e1, *e2;
  104. e1 = container_of(eptr, const struct path_and_oids_entry, ent);
  105. e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
  106. return strcmp(e1->path, e2->path);
  107. }
  108. static void paths_and_oids_init(struct hashmap *map)
  109. {
  110. hashmap_init(map, path_and_oids_cmp, NULL, 0);
  111. }
  112. static void paths_and_oids_clear(struct hashmap *map)
  113. {
  114. struct hashmap_iter iter;
  115. struct path_and_oids_entry *entry;
  116. hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
  117. oidset_clear(&entry->trees);
  118. free(entry->path);
  119. }
  120. hashmap_free_entries(map, struct path_and_oids_entry, ent);
  121. }
  122. static void paths_and_oids_insert(struct hashmap *map,
  123. const char *path,
  124. const struct object_id *oid)
  125. {
  126. int hash = strhash(path);
  127. struct path_and_oids_entry key;
  128. struct path_and_oids_entry *entry;
  129. hashmap_entry_init(&key.ent, hash);
  130. /* use a shallow copy for the lookup */
  131. key.path = (char *)path;
  132. oidset_init(&key.trees, 0);
  133. entry = hashmap_get_entry(map, &key, ent, NULL);
  134. if (!entry) {
  135. entry = xcalloc(1, sizeof(struct path_and_oids_entry));
  136. hashmap_entry_init(&entry->ent, hash);
  137. entry->path = xstrdup(key.path);
  138. oidset_init(&entry->trees, 16);
  139. hashmap_put(map, &entry->ent);
  140. }
  141. oidset_insert(&entry->trees, oid);
  142. }
  143. static void add_children_by_path(struct repository *r,
  144. struct tree *tree,
  145. struct hashmap *map)
  146. {
  147. struct tree_desc desc;
  148. struct name_entry entry;
  149. if (!tree)
  150. return;
  151. if (parse_tree_gently(tree, 1) < 0)
  152. return;
  153. init_tree_desc(&desc, tree->buffer, tree->size);
  154. while (tree_entry(&desc, &entry)) {
  155. switch (object_type(entry.mode)) {
  156. case OBJ_TREE:
  157. paths_and_oids_insert(map, entry.path, &entry.oid);
  158. if (tree->object.flags & UNINTERESTING) {
  159. struct tree *child = lookup_tree(r, &entry.oid);
  160. if (child)
  161. child->object.flags |= UNINTERESTING;
  162. }
  163. break;
  164. case OBJ_BLOB:
  165. if (tree->object.flags & UNINTERESTING) {
  166. struct blob *child = lookup_blob(r, &entry.oid);
  167. if (child)
  168. child->object.flags |= UNINTERESTING;
  169. }
  170. break;
  171. default:
  172. /* Subproject commit - not in this repository */
  173. break;
  174. }
  175. }
  176. free_tree_buffer(tree);
  177. }
  178. void mark_trees_uninteresting_sparse(struct repository *r,
  179. struct oidset *trees)
  180. {
  181. unsigned has_interesting = 0, has_uninteresting = 0;
  182. struct hashmap map;
  183. struct hashmap_iter map_iter;
  184. struct path_and_oids_entry *entry;
  185. struct object_id *oid;
  186. struct oidset_iter iter;
  187. oidset_iter_init(trees, &iter);
  188. while ((!has_interesting || !has_uninteresting) &&
  189. (oid = oidset_iter_next(&iter))) {
  190. struct tree *tree = lookup_tree(r, oid);
  191. if (!tree)
  192. continue;
  193. if (tree->object.flags & UNINTERESTING)
  194. has_uninteresting = 1;
  195. else
  196. has_interesting = 1;
  197. }
  198. /* Do not walk unless we have both types of trees. */
  199. if (!has_uninteresting || !has_interesting)
  200. return;
  201. paths_and_oids_init(&map);
  202. oidset_iter_init(trees, &iter);
  203. while ((oid = oidset_iter_next(&iter))) {
  204. struct tree *tree = lookup_tree(r, oid);
  205. add_children_by_path(r, tree, &map);
  206. }
  207. hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
  208. mark_trees_uninteresting_sparse(r, &entry->trees);
  209. paths_and_oids_clear(&map);
  210. }
  211. struct commit_stack {
  212. struct commit **items;
  213. size_t nr, alloc;
  214. };
  215. #define COMMIT_STACK_INIT { NULL, 0, 0 }
  216. static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
  217. {
  218. ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
  219. stack->items[stack->nr++] = commit;
  220. }
  221. static struct commit *commit_stack_pop(struct commit_stack *stack)
  222. {
  223. return stack->nr ? stack->items[--stack->nr] : NULL;
  224. }
  225. static void commit_stack_clear(struct commit_stack *stack)
  226. {
  227. FREE_AND_NULL(stack->items);
  228. stack->nr = stack->alloc = 0;
  229. }
  230. static void mark_one_parent_uninteresting(struct commit *commit,
  231. struct commit_stack *pending)
  232. {
  233. struct commit_list *l;
  234. if (commit->object.flags & UNINTERESTING)
  235. return;
  236. commit->object.flags |= UNINTERESTING;
  237. /*
  238. * Normally we haven't parsed the parent
  239. * yet, so we won't have a parent of a parent
  240. * here. However, it may turn out that we've
  241. * reached this commit some other way (where it
  242. * wasn't uninteresting), in which case we need
  243. * to mark its parents recursively too..
  244. */
  245. for (l = commit->parents; l; l = l->next)
  246. commit_stack_push(pending, l->item);
  247. }
  248. void mark_parents_uninteresting(struct commit *commit)
  249. {
  250. struct commit_stack pending = COMMIT_STACK_INIT;
  251. struct commit_list *l;
  252. for (l = commit->parents; l; l = l->next)
  253. mark_one_parent_uninteresting(l->item, &pending);
  254. while (pending.nr > 0)
  255. mark_one_parent_uninteresting(commit_stack_pop(&pending),
  256. &pending);
  257. commit_stack_clear(&pending);
  258. }
  259. static void add_pending_object_with_path(struct rev_info *revs,
  260. struct object *obj,
  261. const char *name, unsigned mode,
  262. const char *path)
  263. {
  264. struct interpret_branch_name_options options = { 0 };
  265. if (!obj)
  266. return;
  267. if (revs->no_walk && (obj->flags & UNINTERESTING))
  268. revs->no_walk = 0;
  269. if (revs->reflog_info && obj->type == OBJ_COMMIT) {
  270. struct strbuf buf = STRBUF_INIT;
  271. int len = interpret_branch_name(name, 0, &buf, &options);
  272. if (0 < len && name[len] && buf.len)
  273. strbuf_addstr(&buf, name + len);
  274. add_reflog_for_walk(revs->reflog_info,
  275. (struct commit *)obj,
  276. buf.buf[0] ? buf.buf: name);
  277. strbuf_release(&buf);
  278. return; /* do not add the commit itself */
  279. }
  280. add_object_array_with_path(obj, name, &revs->pending, mode, path);
  281. }
  282. static void add_pending_object_with_mode(struct rev_info *revs,
  283. struct object *obj,
  284. const char *name, unsigned mode)
  285. {
  286. add_pending_object_with_path(revs, obj, name, mode, NULL);
  287. }
  288. void add_pending_object(struct rev_info *revs,
  289. struct object *obj, const char *name)
  290. {
  291. add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
  292. }
  293. void add_head_to_pending(struct rev_info *revs)
  294. {
  295. struct object_id oid;
  296. struct object *obj;
  297. if (get_oid("HEAD", &oid))
  298. return;
  299. obj = parse_object(revs->repo, &oid);
  300. if (!obj)
  301. return;
  302. add_pending_object(revs, obj, "HEAD");
  303. }
  304. static struct object *get_reference(struct rev_info *revs, const char *name,
  305. const struct object_id *oid,
  306. unsigned int flags)
  307. {
  308. struct object *object;
  309. /*
  310. * If the repository has commit graphs, repo_parse_commit() avoids
  311. * reading the object buffer, so use it whenever possible.
  312. */
  313. if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
  314. struct commit *c = lookup_commit(revs->repo, oid);
  315. if (!repo_parse_commit(revs->repo, c))
  316. object = (struct object *) c;
  317. else
  318. object = NULL;
  319. } else {
  320. object = parse_object(revs->repo, oid);
  321. }
  322. if (!object) {
  323. if (revs->ignore_missing)
  324. return object;
  325. if (revs->exclude_promisor_objects && is_promisor_object(oid))
  326. return NULL;
  327. die("bad object %s", name);
  328. }
  329. object->flags |= flags;
  330. return object;
  331. }
  332. void add_pending_oid(struct rev_info *revs, const char *name,
  333. const struct object_id *oid, unsigned int flags)
  334. {
  335. struct object *object = get_reference(revs, name, oid, flags);
  336. add_pending_object(revs, object, name);
  337. }
  338. static struct commit *handle_commit(struct rev_info *revs,
  339. struct object_array_entry *entry)
  340. {
  341. struct object *object = entry->item;
  342. const char *name = entry->name;
  343. const char *path = entry->path;
  344. unsigned int mode = entry->mode;
  345. unsigned long flags = object->flags;
  346. /*
  347. * Tag object? Look what it points to..
  348. */
  349. while (object->type == OBJ_TAG) {
  350. struct tag *tag = (struct tag *) object;
  351. if (revs->tag_objects && !(flags & UNINTERESTING))
  352. add_pending_object(revs, object, tag->tag);
  353. object = parse_object(revs->repo, get_tagged_oid(tag));
  354. if (!object) {
  355. if (revs->ignore_missing_links || (flags & UNINTERESTING))
  356. return NULL;
  357. if (revs->exclude_promisor_objects &&
  358. is_promisor_object(&tag->tagged->oid))
  359. return NULL;
  360. die("bad object %s", oid_to_hex(&tag->tagged->oid));
  361. }
  362. object->flags |= flags;
  363. /*
  364. * We'll handle the tagged object by looping or dropping
  365. * through to the non-tag handlers below. Do not
  366. * propagate path data from the tag's pending entry.
  367. */
  368. path = NULL;
  369. mode = 0;
  370. }
  371. /*
  372. * Commit object? Just return it, we'll do all the complex
  373. * reachability crud.
  374. */
  375. if (object->type == OBJ_COMMIT) {
  376. struct commit *commit = (struct commit *)object;
  377. if (repo_parse_commit(revs->repo, commit) < 0)
  378. die("unable to parse commit %s", name);
  379. if (flags & UNINTERESTING) {
  380. mark_parents_uninteresting(commit);
  381. if (!revs->topo_order || !generation_numbers_enabled(the_repository))
  382. revs->limited = 1;
  383. }
  384. if (revs->sources) {
  385. char **slot = revision_sources_at(revs->sources, commit);
  386. if (!*slot)
  387. *slot = xstrdup(name);
  388. }
  389. return commit;
  390. }
  391. /*
  392. * Tree object? Either mark it uninteresting, or add it
  393. * to the list of objects to look at later..
  394. */
  395. if (object->type == OBJ_TREE) {
  396. struct tree *tree = (struct tree *)object;
  397. if (!revs->tree_objects)
  398. return NULL;
  399. if (flags & UNINTERESTING) {
  400. mark_tree_contents_uninteresting(revs->repo, tree);
  401. return NULL;
  402. }
  403. add_pending_object_with_path(revs, object, name, mode, path);
  404. return NULL;
  405. }
  406. /*
  407. * Blob object? You know the drill by now..
  408. */
  409. if (object->type == OBJ_BLOB) {
  410. if (!revs->blob_objects)
  411. return NULL;
  412. if (flags & UNINTERESTING)
  413. return NULL;
  414. add_pending_object_with_path(revs, object, name, mode, path);
  415. return NULL;
  416. }
  417. die("%s is unknown object", name);
  418. }
  419. static int everybody_uninteresting(struct commit_list *orig,
  420. struct commit **interesting_cache)
  421. {
  422. struct commit_list *list = orig;
  423. if (*interesting_cache) {
  424. struct commit *commit = *interesting_cache;
  425. if (!(commit->object.flags & UNINTERESTING))
  426. return 0;
  427. }
  428. while (list) {
  429. struct commit *commit = list->item;
  430. list = list->next;
  431. if (commit->object.flags & UNINTERESTING)
  432. continue;
  433. *interesting_cache = commit;
  434. return 0;
  435. }
  436. return 1;
  437. }
  438. /*
  439. * A definition of "relevant" commit that we can use to simplify limited graphs
  440. * by eliminating side branches.
  441. *
  442. * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
  443. * in our list), or that is a specified BOTTOM commit. Then after computing
  444. * a limited list, during processing we can generally ignore boundary merges
  445. * coming from outside the graph, (ie from irrelevant parents), and treat
  446. * those merges as if they were single-parent. TREESAME is defined to consider
  447. * only relevant parents, if any. If we are TREESAME to our on-graph parents,
  448. * we don't care if we were !TREESAME to non-graph parents.
  449. *
  450. * Treating bottom commits as relevant ensures that a limited graph's
  451. * connection to the actual bottom commit is not viewed as a side branch, but
  452. * treated as part of the graph. For example:
  453. *
  454. * ....Z...A---X---o---o---B
  455. * . /
  456. * W---Y
  457. *
  458. * When computing "A..B", the A-X connection is at least as important as
  459. * Y-X, despite A being flagged UNINTERESTING.
  460. *
  461. * And when computing --ancestry-path "A..B", the A-X connection is more
  462. * important than Y-X, despite both A and Y being flagged UNINTERESTING.
  463. */
  464. static inline int relevant_commit(struct commit *commit)
  465. {
  466. return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
  467. }
  468. /*
  469. * Return a single relevant commit from a parent list. If we are a TREESAME
  470. * commit, and this selects one of our parents, then we can safely simplify to
  471. * that parent.
  472. */
  473. static struct commit *one_relevant_parent(const struct rev_info *revs,
  474. struct commit_list *orig)
  475. {
  476. struct commit_list *list = orig;
  477. struct commit *relevant = NULL;
  478. if (!orig)
  479. return NULL;
  480. /*
  481. * For 1-parent commits, or if first-parent-only, then return that
  482. * first parent (even if not "relevant" by the above definition).
  483. * TREESAME will have been set purely on that parent.
  484. */
  485. if (revs->first_parent_only || !orig->next)
  486. return orig->item;
  487. /*
  488. * For multi-parent commits, identify a sole relevant parent, if any.
  489. * If we have only one relevant parent, then TREESAME will be set purely
  490. * with regard to that parent, and we can simplify accordingly.
  491. *
  492. * If we have more than one relevant parent, or no relevant parents
  493. * (and multiple irrelevant ones), then we can't select a parent here
  494. * and return NULL.
  495. */
  496. while (list) {
  497. struct commit *commit = list->item;
  498. list = list->next;
  499. if (relevant_commit(commit)) {
  500. if (relevant)
  501. return NULL;
  502. relevant = commit;
  503. }
  504. }
  505. return relevant;
  506. }
  507. /*
  508. * The goal is to get REV_TREE_NEW as the result only if the
  509. * diff consists of all '+' (and no other changes), REV_TREE_OLD
  510. * if the whole diff is removal of old data, and otherwise
  511. * REV_TREE_DIFFERENT (of course if the trees are the same we
  512. * want REV_TREE_SAME).
  513. *
  514. * The only time we care about the distinction is when
  515. * remove_empty_trees is in effect, in which case we care only about
  516. * whether the whole change is REV_TREE_NEW, or if there's another type
  517. * of change. Which means we can stop the diff early in either of these
  518. * cases:
  519. *
  520. * 1. We're not using remove_empty_trees at all.
  521. *
  522. * 2. We saw anything except REV_TREE_NEW.
  523. */
  524. static int tree_difference = REV_TREE_SAME;
  525. static void file_add_remove(struct diff_options *options,
  526. int addremove, unsigned mode,
  527. const struct object_id *oid,
  528. int oid_valid,
  529. const char *fullpath, unsigned dirty_submodule)
  530. {
  531. int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
  532. struct rev_info *revs = options->change_fn_data;
  533. tree_difference |= diff;
  534. if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
  535. options->flags.has_changes = 1;
  536. }
  537. static void file_change(struct diff_options *options,
  538. unsigned old_mode, unsigned new_mode,
  539. const struct object_id *old_oid,
  540. const struct object_id *new_oid,
  541. int old_oid_valid, int new_oid_valid,
  542. const char *fullpath,
  543. unsigned old_dirty_submodule, unsigned new_dirty_submodule)
  544. {
  545. tree_difference = REV_TREE_DIFFERENT;
  546. options->flags.has_changes = 1;
  547. }
  548. static int bloom_filter_atexit_registered;
  549. static unsigned int count_bloom_filter_maybe;
  550. static unsigned int count_bloom_filter_definitely_not;
  551. static unsigned int count_bloom_filter_false_positive;
  552. static unsigned int count_bloom_filter_not_present;
  553. static void trace2_bloom_filter_statistics_atexit(void)
  554. {
  555. struct json_writer jw = JSON_WRITER_INIT;
  556. jw_object_begin(&jw, 0);
  557. jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
  558. jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
  559. jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
  560. jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
  561. jw_end(&jw);
  562. trace2_data_json("bloom", the_repository, "statistics", &jw);
  563. jw_release(&jw);
  564. }
  565. static int forbid_bloom_filters(struct pathspec *spec)
  566. {
  567. if (spec->has_wildcard)
  568. return 1;
  569. if (spec->nr > 1)
  570. return 1;
  571. if (spec->magic & ~PATHSPEC_LITERAL)
  572. return 1;
  573. if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
  574. return 1;
  575. return 0;
  576. }
  577. static void prepare_to_use_bloom_filter(struct rev_info *revs)
  578. {
  579. struct pathspec_item *pi;
  580. char *path_alloc = NULL;
  581. const char *path, *p;
  582. size_t len;
  583. int path_component_nr = 1;
  584. if (!revs->commits)
  585. return;
  586. if (forbid_bloom_filters(&revs->prune_data))
  587. return;
  588. repo_parse_commit(revs->repo, revs->commits->item);
  589. revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
  590. if (!revs->bloom_filter_settings)
  591. return;
  592. if (!revs->pruning.pathspec.nr)
  593. return;
  594. pi = &revs->pruning.pathspec.items[0];
  595. /* remove single trailing slash from path, if needed */
  596. if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
  597. path_alloc = xmemdupz(pi->match, pi->len - 1);
  598. path = path_alloc;
  599. } else
  600. path = pi->match;
  601. len = strlen(path);
  602. if (!len) {
  603. revs->bloom_filter_settings = NULL;
  604. free(path_alloc);
  605. return;
  606. }
  607. p = path;
  608. while (*p) {
  609. /*
  610. * At this point, the path is normalized to use Unix-style
  611. * path separators. This is required due to how the
  612. * changed-path Bloom filters store the paths.
  613. */
  614. if (*p == '/')
  615. path_component_nr++;
  616. p++;
  617. }
  618. revs->bloom_keys_nr = path_component_nr;
  619. ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
  620. fill_bloom_key(path, len, &revs->bloom_keys[0],
  621. revs->bloom_filter_settings);
  622. path_component_nr = 1;
  623. p = path + len - 1;
  624. while (p > path) {
  625. if (*p == '/')
  626. fill_bloom_key(path, p - path,
  627. &revs->bloom_keys[path_component_nr++],
  628. revs->bloom_filter_settings);
  629. p--;
  630. }
  631. if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
  632. atexit(trace2_bloom_filter_statistics_atexit);
  633. bloom_filter_atexit_registered = 1;
  634. }
  635. free(path_alloc);
  636. }
  637. static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
  638. struct commit *commit)
  639. {
  640. struct bloom_filter *filter;
  641. int result = 1, j;
  642. if (!revs->repo->objects->commit_graph)
  643. return -1;
  644. if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
  645. return -1;
  646. filter = get_bloom_filter(revs->repo, commit);
  647. if (!filter) {
  648. count_bloom_filter_not_present++;
  649. return -1;
  650. }
  651. for (j = 0; result && j < revs->bloom_keys_nr; j++) {
  652. result = bloom_filter_contains(filter,
  653. &revs->bloom_keys[j],
  654. revs->bloom_filter_settings);
  655. }
  656. if (result)
  657. count_bloom_filter_maybe++;
  658. else
  659. count_bloom_filter_definitely_not++;
  660. return result;
  661. }
  662. static int rev_compare_tree(struct rev_info *revs,
  663. struct commit *parent, struct commit *commit, int nth_parent)
  664. {
  665. struct tree *t1 = get_commit_tree(parent);
  666. struct tree *t2 = get_commit_tree(commit);
  667. int bloom_ret = 1;
  668. if (!t1)
  669. return REV_TREE_NEW;
  670. if (!t2)
  671. return REV_TREE_OLD;
  672. if (revs->simplify_by_decoration) {
  673. /*
  674. * If we are simplifying by decoration, then the commit
  675. * is worth showing if it has a tag pointing at it.
  676. */
  677. if (get_name_decoration(&commit->object))
  678. return REV_TREE_DIFFERENT;
  679. /*
  680. * A commit that is not pointed by a tag is uninteresting
  681. * if we are not limited by path. This means that you will
  682. * see the usual "commits that touch the paths" plus any
  683. * tagged commit by specifying both --simplify-by-decoration
  684. * and pathspec.
  685. */
  686. if (!revs->prune_data.nr)
  687. return REV_TREE_SAME;
  688. }
  689. if (revs->bloom_keys_nr && !nth_parent) {
  690. bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
  691. if (bloom_ret == 0)
  692. return REV_TREE_SAME;
  693. }
  694. tree_difference = REV_TREE_SAME;
  695. revs->pruning.flags.has_changes = 0;
  696. diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
  697. if (!nth_parent)
  698. if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
  699. count_bloom_filter_false_positive++;
  700. return tree_difference;
  701. }
  702. static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
  703. {
  704. struct tree *t1 = get_commit_tree(commit);
  705. if (!t1)
  706. return 0;
  707. tree_difference = REV_TREE_SAME;
  708. revs->pruning.flags.has_changes = 0;
  709. diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
  710. return tree_difference == REV_TREE_SAME;
  711. }
  712. struct treesame_state {
  713. unsigned int nparents;
  714. unsigned char treesame[FLEX_ARRAY];
  715. };
  716. static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
  717. {
  718. unsigned n = commit_list_count(commit->parents);
  719. struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
  720. st->nparents = n;
  721. add_decoration(&revs->treesame, &commit->object, st);
  722. return st;
  723. }
  724. /*
  725. * Must be called immediately after removing the nth_parent from a commit's
  726. * parent list, if we are maintaining the per-parent treesame[] decoration.
  727. * This does not recalculate the master TREESAME flag - update_treesame()
  728. * should be called to update it after a sequence of treesame[] modifications
  729. * that may have affected it.
  730. */
  731. static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
  732. {
  733. struct treesame_state *st;
  734. int old_same;
  735. if (!commit->parents) {
  736. /*
  737. * Have just removed the only parent from a non-merge.
  738. * Different handling, as we lack decoration.
  739. */
  740. if (nth_parent != 0)
  741. die("compact_treesame %u", nth_parent);
  742. old_same = !!(commit->object.flags & TREESAME);
  743. if (rev_same_tree_as_empty(revs, commit))
  744. commit->object.flags |= TREESAME;
  745. else
  746. commit->object.flags &= ~TREESAME;
  747. return old_same;
  748. }
  749. st = lookup_decoration(&revs->treesame, &commit->object);
  750. if (!st || nth_parent >= st->nparents)
  751. die("compact_treesame %u", nth_parent);
  752. old_same = st->treesame[nth_parent];
  753. memmove(st->treesame + nth_parent,
  754. st->treesame + nth_parent + 1,
  755. st->nparents - nth_parent - 1);
  756. /*
  757. * If we've just become a non-merge commit, update TREESAME
  758. * immediately, and remove the no-longer-needed decoration.
  759. * If still a merge, defer update until update_treesame().
  760. */
  761. if (--st->nparents == 1) {
  762. if (commit->parents->next)
  763. die("compact_treesame parents mismatch");
  764. if (st->treesame[0] && revs->dense)
  765. commit->object.flags |= TREESAME;
  766. else
  767. commit->object.flags &= ~TREESAME;
  768. free(add_decoration(&revs->treesame, &commit->object, NULL));
  769. }
  770. return old_same;
  771. }
  772. static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
  773. {
  774. if (commit->parents && commit->parents->next) {
  775. unsigned n;
  776. struct treesame_state *st;
  777. struct commit_list *p;
  778. unsigned relevant_parents;
  779. unsigned relevant_change, irrelevant_change;
  780. st = lookup_decoration(&revs->treesame, &commit->object);
  781. if (!st)
  782. die("update_treesame %s", oid_to_hex(&commit->object.oid));
  783. relevant_parents = 0;
  784. relevant_change = irrelevant_change = 0;
  785. for (p = commit->parents, n = 0; p; n++, p = p->next) {
  786. if (relevant_commit(p->item)) {
  787. relevant_change |= !st->treesame[n];
  788. relevant_parents++;
  789. } else
  790. irrelevant_change |= !st->treesame[n];
  791. }
  792. if (relevant_parents ? relevant_change : irrelevant_change)
  793. commit->object.flags &= ~TREESAME;
  794. else
  795. commit->object.flags |= TREESAME;
  796. }
  797. return commit->object.flags & TREESAME;
  798. }
  799. static inline int limiting_can_increase_treesame(const struct rev_info *revs)
  800. {
  801. /*
  802. * TREESAME is irrelevant unless prune && dense;
  803. * if simplify_history is set, we can't have a mixture of TREESAME and
  804. * !TREESAME INTERESTING parents (and we don't have treesame[]
  805. * decoration anyway);
  806. * if first_parent_only is set, then the TREESAME flag is locked
  807. * against the first parent (and again we lack treesame[] decoration).
  808. */
  809. return revs->prune && revs->dense &&
  810. !revs->simplify_history &&
  811. !revs->first_parent_only;
  812. }
  813. static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
  814. {
  815. struct commit_list **pp, *parent;
  816. struct treesame_state *ts = NULL;
  817. int relevant_change = 0, irrelevant_change = 0;
  818. int relevant_parents, nth_parent;
  819. /*
  820. * If we don't do pruning, everything is interesting
  821. */
  822. if (!revs->prune)
  823. return;
  824. if (!get_commit_tree(commit))
  825. return;
  826. if (!commit->parents) {
  827. if (rev_same_tree_as_empty(revs, commit))
  828. commit->object.flags |= TREESAME;
  829. return;
  830. }
  831. /*
  832. * Normal non-merge commit? If we don't want to make the
  833. * history dense, we consider it always to be a change..
  834. */
  835. if (!revs->dense && !commit->parents->next)
  836. return;
  837. for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
  838. (parent = *pp) != NULL;
  839. pp = &parent->next, nth_parent++) {
  840. struct commit *p = parent->item;
  841. if (relevant_commit(p))
  842. relevant_parents++;
  843. if (nth_parent == 1) {
  844. /*
  845. * This our second loop iteration - so we now know
  846. * we're dealing with a merge.
  847. *
  848. * Do not compare with later parents when we care only about
  849. * the first parent chain, in order to avoid derailing the
  850. * traversal to follow a side branch that brought everything
  851. * in the path we are limited to by the pathspec.
  852. */
  853. if (revs->first_parent_only)
  854. break;
  855. /*
  856. * If this will remain a potentially-simplifiable
  857. * merge, remember per-parent treesame if needed.
  858. * Initialise the array with the comparison from our
  859. * first iteration.
  860. */
  861. if (revs->treesame.name &&
  862. !revs->simplify_history &&
  863. !(commit->object.flags & UNINTERESTING)) {
  864. ts = initialise_treesame(revs, commit);
  865. if (!(irrelevant_change || relevant_change))
  866. ts->treesame[0] = 1;
  867. }
  868. }
  869. if (repo_parse_commit(revs->repo, p) < 0)
  870. die("cannot simplify commit %s (because of %s)",
  871. oid_to_hex(&commit->object.oid),
  872. oid_to_hex(&p->object.oid));
  873. switch (rev_compare_tree(revs, p, commit, nth_parent)) {
  874. case REV_TREE_SAME:
  875. if (!revs->simplify_history || !relevant_commit(p)) {
  876. /* Even if a merge with an uninteresting
  877. * side branch brought the entire change
  878. * we are interested in, we do not want
  879. * to lose the other branches of this
  880. * merge, so we just keep going.
  881. */
  882. if (ts)
  883. ts->treesame[nth_parent] = 1;
  884. continue;
  885. }
  886. parent->next = NULL;
  887. commit->parents = parent;
  888. /*
  889. * A merge commit is a "diversion" if it is not
  890. * TREESAME to its first parent but is TREESAME
  891. * to a later parent. In the simplified history,
  892. * we "divert" the history walk to the later
  893. * parent. These commits are shown when "show_pulls"
  894. * is enabled, so do not mark the object as
  895. * TREESAME here.
  896. */
  897. if (!revs->show_pulls || !nth_parent)
  898. commit->object.flags |= TREESAME;
  899. return;
  900. case REV_TREE_NEW:
  901. if (revs->remove_empty_trees &&
  902. rev_same_tree_as_empty(revs, p)) {
  903. /* We are adding all the specified
  904. * paths from this parent, so the
  905. * history beyond this parent is not
  906. * interesting. Remove its parents
  907. * (they are grandparents for us).
  908. * IOW, we pretend this parent is a
  909. * "root" commit.
  910. */
  911. if (repo_parse_commit(revs->repo, p) < 0)
  912. die("cannot simplify commit %s (invalid %s)",
  913. oid_to_hex(&commit->object.oid),
  914. oid_to_hex(&p->object.oid));
  915. p->parents = NULL;
  916. }
  917. /* fallthrough */
  918. case REV_TREE_OLD:
  919. case REV_TREE_DIFFERENT:
  920. if (relevant_commit(p))
  921. relevant_change = 1;
  922. else
  923. irrelevant_change = 1;
  924. if (!nth_parent)
  925. commit->object.flags |= PULL_MERGE;
  926. continue;
  927. }
  928. die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
  929. }
  930. /*
  931. * TREESAME is straightforward for single-parent commits. For merge
  932. * commits, it is most useful to define it so that "irrelevant"
  933. * parents cannot make us !TREESAME - if we have any relevant
  934. * parents, then we only consider TREESAMEness with respect to them,
  935. * allowing irrelevant merges from uninteresting branches to be
  936. * simplified away. Only if we have only irrelevant parents do we
  937. * base TREESAME on them. Note that this logic is replicated in
  938. * update_treesame, which should be kept in sync.
  939. */
  940. if (relevant_parents ? !relevant_change : !irrelevant_change)
  941. commit->object.flags |= TREESAME;
  942. }
  943. static int process_parents(struct rev_info *revs, struct commit *commit,
  944. struct commit_list **list, struct prio_queue *queue)
  945. {
  946. struct commit_list *parent = commit->parents;
  947. unsigned left_flag;
  948. if (commit->object.flags & ADDED)
  949. return 0;
  950. commit->object.flags |= ADDED;
  951. if (revs->include_check &&
  952. !revs->include_check(commit, revs->include_check_data))
  953. return 0;
  954. /*
  955. * If the commit is uninteresting, don't try to
  956. * prune parents - we want the maximal uninteresting
  957. * set.
  958. *
  959. * Normally we haven't parsed the parent
  960. * yet, so we won't have a parent of a parent
  961. * here. However, it may turn out that we've
  962. * reached this commit some other way (where it
  963. * wasn't uninteresting), in which case we need
  964. * to mark its parents recursively too..
  965. */
  966. if (commit->object.flags & UNINTERESTING) {
  967. while (parent) {
  968. struct commit *p = parent->item;
  969. parent = parent->next;
  970. if (p)
  971. p->object.flags |= UNINTERESTING;
  972. if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
  973. continue;
  974. if (p->parents)
  975. mark_parents_uninteresting(p);
  976. if (p->object.flags & SEEN)
  977. continue;
  978. p->object.flags |= SEEN;
  979. if (list)
  980. commit_list_insert_by_date(p, list);
  981. if (queue)
  982. prio_queue_put(queue, p);
  983. }
  984. return 0;
  985. }
  986. /*
  987. * Ok, the commit wasn't uninteresting. Try to
  988. * simplify the commit history and find the parent
  989. * that has no differences in the path set if one exists.
  990. */
  991. try_to_simplify_commit(revs, commit);
  992. if (revs->no_walk)
  993. return 0;
  994. left_flag = (commit->object.flags & SYMMETRIC_LEFT);
  995. for (parent = commit->parents; parent; parent = parent->next) {
  996. struct commit *p = parent->item;
  997. int gently = revs->ignore_missing_links ||
  998. revs->exclude_promisor_objects;
  999. if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
  1000. if (revs->exclude_promisor_objects &&
  1001. is_promisor_object(&p->object.oid)) {
  1002. if (revs->first_parent_only)
  1003. break;
  1004. continue;
  1005. }
  1006. return -1;
  1007. }
  1008. if (revs->sources) {
  1009. char **slot = revision_sources_at(revs->sources, p);
  1010. if (!*slot)
  1011. *slot = *revision_sources_at(revs->sources, commit);
  1012. }
  1013. p->object.flags |= left_flag;
  1014. if (!(p->object.flags & SEEN)) {
  1015. p->object.flags |= SEEN;
  1016. if (list)
  1017. commit_list_insert_by_date(p, list);
  1018. if (queue)
  1019. prio_queue_put(queue, p);
  1020. }
  1021. if (revs->first_parent_only)
  1022. break;
  1023. }
  1024. return 0;
  1025. }
  1026. static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
  1027. {
  1028. struct commit_list *p;
  1029. int left_count = 0, right_count = 0;
  1030. int left_first;
  1031. struct patch_ids ids;
  1032. unsigned cherry_flag;
  1033. /* First count the commits on the left and on the right */
  1034. for (p = list; p; p = p->next) {
  1035. struct commit *commit = p->item;
  1036. unsigned flags = commit->object.flags;
  1037. if (flags & BOUNDARY)
  1038. ;
  1039. else if (flags & SYMMETRIC_LEFT)
  1040. left_count++;
  1041. else
  1042. right_count++;
  1043. }
  1044. if (!left_count || !right_count)
  1045. return;
  1046. left_first = left_count < right_count;
  1047. init_patch_ids(revs->repo, &ids);
  1048. ids.diffopts.pathspec = revs->diffopt.pathspec;
  1049. /* Compute patch-ids for one side */
  1050. for (p = list; p; p = p->next) {
  1051. struct commit *commit = p->item;
  1052. unsigned flags = commit->object.flags;
  1053. if (flags & BOUNDARY)
  1054. continue;
  1055. /*
  1056. * If we have fewer left, left_first is set and we omit
  1057. * commits on the right branch in this loop. If we have
  1058. * fewer right, we skip the left ones.
  1059. */
  1060. if (left_first != !!(flags & SYMMETRIC_LEFT))
  1061. continue;
  1062. add_commit_patch_id(commit, &ids);
  1063. }
  1064. /* either cherry_mark or cherry_pick are true */
  1065. cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
  1066. /* Check the other side */
  1067. for (p = list; p; p = p->next) {
  1068. struct commit *commit = p->item;
  1069. struct patch_id *id;
  1070. unsigned flags = commit->object.flags;
  1071. if (flags & BOUNDARY)
  1072. continue;
  1073. /*
  1074. * If we have fewer left, left_first is set and we omit
  1075. * commits on the left branch in this loop.
  1076. */
  1077. if (left_first == !!(flags & SYMMETRIC_LEFT))
  1078. continue;
  1079. /*
  1080. * Have we seen the same patch id?
  1081. */
  1082. id = has_commit_patch_id(commit, &ids);
  1083. if (!id)
  1084. continue;
  1085. commit->object.flags |= cherry_flag;
  1086. id->commit->object.flags |= cherry_flag;
  1087. }
  1088. free_patch_ids(&ids);
  1089. }
  1090. /* How many extra uninteresting commits we want to see.. */
  1091. #define SLOP 5
  1092. static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
  1093. struct commit **interesting_cache)
  1094. {
  1095. /*
  1096. * No source list at all? We're definitely done..
  1097. */
  1098. if (!src)
  1099. return 0;
  1100. /*
  1101. * Does the destination list contain entries with a date
  1102. * before the source list? Definitely _not_ done.
  1103. */
  1104. if (date <= src->item->date)
  1105. return SLOP;
  1106. /*
  1107. * Does the source list still have interesting commits in
  1108. * it? Definitely not done..
  1109. */
  1110. if (!everybody_uninteresting(src, interesting_cache))
  1111. return SLOP;
  1112. /* Ok, we're closing in.. */
  1113. return slop-1;
  1114. }
  1115. /*
  1116. * "rev-list --ancestry-path A..B" computes commits that are ancestors
  1117. * of B but not ancestors of A but further limits the result to those
  1118. * that are descendants of A. This takes the list of bottom commits and
  1119. * the result of "A..B" without --ancestry-path, and limits the latter
  1120. * further to the ones that can reach one of the commits in "bottom".
  1121. */
  1122. static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
  1123. {
  1124. struct commit_list *p;
  1125. struct commit_list *rlist = NULL;
  1126. int made_progress;
  1127. /*
  1128. * Reverse the list so that it will be likely that we would
  1129. * process parents before children.
  1130. */
  1131. for (p = list; p; p = p->next)
  1132. commit_list_insert(p->item, &rlist);
  1133. for (p = bottom; p; p = p->next)
  1134. p->item->object.flags |= TMP_MARK;
  1135. /*
  1136. * Mark the ones that can reach bottom commits in "list",
  1137. * in a bottom-up fashion.
  1138. */
  1139. do {
  1140. made_progress = 0;
  1141. for (p = rlist; p; p = p->next) {
  1142. struct commit *c = p->item;
  1143. struct commit_list *parents;
  1144. if (c->object.flags & (TMP_MARK | UNINTERESTING))
  1145. continue;
  1146. for (parents = c->parents;
  1147. parents;
  1148. parents = parents->next) {
  1149. if (!(parents->item->object.flags & TMP_MARK))
  1150. continue;
  1151. c->object.flags |= TMP_MARK;
  1152. made_progress = 1;
  1153. break;
  1154. }
  1155. }
  1156. } while (made_progress);
  1157. /*
  1158. * NEEDSWORK: decide if we want to remove parents that are
  1159. * not marked with TMP_MARK from commit->parents for commits
  1160. * in the resulting list. We may not want to do that, though.
  1161. */
  1162. /*
  1163. * The ones that are not marked with TMP_MARK are uninteresting
  1164. */
  1165. for (p = list; p; p = p->next) {
  1166. struct commit *c = p->item;
  1167. if (c->object.flags & TMP_MARK)
  1168. continue;
  1169. c->object.flags |= UNINTERESTING;
  1170. }
  1171. /* We are done with the TMP_MARK */
  1172. for (p = list; p; p = p->next)
  1173. p->item->object.flags &= ~TMP_MARK;
  1174. for (p = bottom; p; p = p->next)
  1175. p->item->object.flags &= ~TMP_MARK;
  1176. free_commit_list(rlist);
  1177. }
  1178. /*
  1179. * Before walking the history, keep the set of "negative" refs the
  1180. * caller has asked to exclude.
  1181. *
  1182. * This is used to compute "rev-list --ancestry-path A..B", as we need
  1183. * to filter the result of "A..B" further to the ones that can actually
  1184. * reach A.
  1185. */
  1186. static struct commit_list *collect_bottom_commits(struct commit_list *list)
  1187. {
  1188. struct commit_list *elem, *bottom = NULL;
  1189. for (elem = list; elem; elem = elem->next)
  1190. if (elem->item->object.flags & BOTTOM)
  1191. commit_list_insert(elem->item, &bottom);
  1192. return bottom;
  1193. }
  1194. /* Assumes either left_only or right_only is set */
  1195. static void limit_left_right(struct commit_list *list, struct rev_info *revs)
  1196. {
  1197. struct commit_list *p;
  1198. for (p = list; p; p = p->next) {
  1199. struct commit *commit = p->item;
  1200. if (revs->right_only) {
  1201. if (commit->object.flags & SYMMETRIC_LEFT)
  1202. commit->object.flags |= SHOWN;
  1203. } else /* revs->left_only is set */
  1204. if (!(commit->object.flags & SYMMETRIC_LEFT))
  1205. commit->object.flags |= SHOWN;
  1206. }
  1207. }
  1208. static int limit_list(struct rev_info *revs)
  1209. {
  1210. int slop = SLOP;
  1211. timestamp_t date = TIME_MAX;
  1212. struct commit_list *list = revs->commits;
  1213. struct commit_list *newlist = NULL;
  1214. struct commit_list **p = &newlist;
  1215. struct commit_list *bottom = NULL;
  1216. struct commit *interesting_cache = NULL;
  1217. if (revs->ancestry_path) {
  1218. bottom = collect_bottom_commits(list);
  1219. if (!bottom)
  1220. die("--ancestry-path given but there are no bottom commits");
  1221. }
  1222. while (list) {
  1223. struct commit *commit = pop_commit(&list);
  1224. struct object *obj = &commit->object;
  1225. show_early_output_fn_t show;
  1226. if (commit == interesting_cache)
  1227. interesting_cache = NULL;
  1228. if (revs->max_age != -1 && (commit->date < revs->max_age))
  1229. obj->flags |= UNINTERESTING;
  1230. if (process_parents(revs, commit, &list, NULL) < 0)
  1231. return -1;
  1232. if (obj->flags & UNINTERESTING) {
  1233. mark_parents_uninteresting(commit);
  1234. slop = still_interesting(list, date, slop, &interesting_cache);
  1235. if (slop)
  1236. continue;
  1237. break;
  1238. }
  1239. if (revs->min_age != -1 && (commit->date > revs->min_age) &&
  1240. !revs->line_level_traverse)
  1241. continue;
  1242. date = commit->date;
  1243. p = &commit_list_insert(commit, p)->next;
  1244. show = show_early_output;
  1245. if (!show)
  1246. continue;
  1247. show(revs, newlist);
  1248. show_early_output = NULL;
  1249. }
  1250. if (revs->cherry_pick || revs->cherry_mark)
  1251. cherry_pick_list(newlist, revs);
  1252. if (revs->left_only || revs->right_only)
  1253. limit_left_right(newlist, revs);
  1254. if (bottom) {
  1255. limit_to_ancestry(bottom, newlist);
  1256. free_commit_list(bottom);
  1257. }
  1258. /*
  1259. * Check if any commits have become TREESAME by some of their parents
  1260. * becoming UNINTERESTING.
  1261. */
  1262. if (limiting_can_increase_treesame(revs))
  1263. for (list = newlist; list; list = list->next) {
  1264. struct commit *c = list->item;
  1265. if (c->object.flags & (UNINTERESTING | TREESAME))
  1266. continue;
  1267. update_treesame(revs, c);
  1268. }
  1269. revs->commits = newlist;
  1270. return 0;
  1271. }
  1272. /*
  1273. * Add an entry to refs->cmdline with the specified information.
  1274. * *name is copied.
  1275. */
  1276. static void add_rev_cmdline(struct rev_info *revs,
  1277. struct object *item,
  1278. const char *name,
  1279. int whence,
  1280. unsigned flags)
  1281. {
  1282. struct rev_cmdline_info *info = &revs->cmdline;
  1283. unsigned int nr = info->nr;
  1284. ALLOC_GROW(info->rev, nr + 1, info->alloc);
  1285. info->rev[nr].item = item;
  1286. info->rev[nr].name = xstrdup(name);
  1287. info->rev[nr].whence = whence;
  1288. info->rev[nr].flags = flags;
  1289. info->nr++;
  1290. }
  1291. static void add_rev_cmdline_list(struct rev_info *revs,
  1292. struct commit_list *commit_list,
  1293. int whence,
  1294. unsigned flags)
  1295. {
  1296. while (commit_list) {
  1297. struct object *object = &commit_list->item->object;
  1298. add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
  1299. whence, flags);
  1300. commit_list = commit_list->next;
  1301. }
  1302. }
  1303. struct all_refs_cb {
  1304. int all_flags;
  1305. int warned_bad_reflog;
  1306. struct rev_info *all_revs;
  1307. const char *name_for_errormsg;
  1308. struct worktree *wt;
  1309. };
  1310. int ref_excluded(struct string_list *ref_excludes, const char *path)
  1311. {
  1312. struct string_list_item *item;
  1313. if (!ref_excludes)
  1314. return 0;
  1315. for_each_string_list_item(item, ref_excludes) {
  1316. if (!wildmatch(item->string, path, 0))
  1317. return 1;
  1318. }
  1319. return 0;
  1320. }
  1321. static int handle_one_ref(const char *path, const struct object_id *oid,
  1322. int flag, void *cb_data)
  1323. {
  1324. struct all_refs_cb *cb = cb_data;
  1325. struct object *object;
  1326. if (ref_excluded(cb->all_revs->ref_excludes, path))
  1327. return 0;
  1328. object = get_reference(cb->all_revs, path, oid, cb->all_flags);
  1329. add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
  1330. add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
  1331. return 0;
  1332. }
  1333. static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
  1334. unsigned flags)
  1335. {
  1336. cb->all_revs = revs;
  1337. cb->all_flags = flags;
  1338. revs->rev_input_given = 1;
  1339. cb->wt = NULL;
  1340. }
  1341. void clear_ref_exclusion(struct string_list **ref_excludes_p)
  1342. {
  1343. if (*ref_excludes_p) {
  1344. string_list_clear(*ref_excludes_p, 0);
  1345. free(*ref_excludes_p);
  1346. }
  1347. *ref_excludes_p = NULL;
  1348. }
  1349. void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
  1350. {
  1351. if (!*ref_excludes_p) {
  1352. *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
  1353. (*ref_excludes_p)->strdup_strings = 1;
  1354. }
  1355. string_list_append(*ref_excludes_p, exclude);
  1356. }
  1357. static void handle_refs(struct ref_store *refs,
  1358. struct rev_info *revs, unsigned flags,
  1359. int (*for_each)(struct ref_store *, each_ref_fn, void *))
  1360. {
  1361. struct all_refs_cb cb;
  1362. if (!refs) {
  1363. /* this could happen with uninitialized submodules */
  1364. return;
  1365. }
  1366. init_all_refs_cb(&cb, revs, flags);
  1367. for_each(refs, handle_one_ref, &cb);
  1368. }
  1369. static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
  1370. {
  1371. struct all_refs_cb *cb = cb_data;
  1372. if (!is_null_oid(oid)) {
  1373. struct object *o = parse_object(cb->all_revs->repo, oid);
  1374. if (o) {
  1375. o->flags |= cb->all_flags;
  1376. /* ??? CMDLINEFLAGS ??? */
  1377. add_pending_object(cb->all_revs, o, "");
  1378. }
  1379. else if (!cb->warned_bad_reflog) {
  1380. warning("reflog of '%s' references pruned commits",
  1381. cb->name_for_errormsg);
  1382. cb->warned_bad_reflog = 1;
  1383. }
  1384. }
  1385. }
  1386. static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
  1387. const char *email, timestamp_t timestamp, int tz,
  1388. const char *message, void *cb_data)
  1389. {
  1390. handle_one_reflog_commit(ooid, cb_data);
  1391. handle_one_reflog_commit(noid, cb_data);
  1392. return 0;
  1393. }
  1394. static int handle_one_reflog(const char *refname_in_wt,
  1395. const struct object_id *oid,
  1396. int flag, void *cb_data)
  1397. {
  1398. struct all_refs_cb *cb = cb_data;
  1399. struct strbuf refname = STRBUF_INIT;
  1400. cb->warned_bad_reflog = 0;
  1401. strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
  1402. cb->name_for_errormsg = refname.buf;
  1403. refs_for_each_reflog_ent(get_main_ref_store(the_repository),
  1404. refname.buf,
  1405. handle_one_reflog_ent, cb_data);
  1406. strbuf_release(&refname);
  1407. return 0;
  1408. }
  1409. static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
  1410. {
  1411. struct worktree **worktrees, **p;
  1412. worktrees = get_worktrees();
  1413. for (p = worktrees; *p; p++) {
  1414. struct worktree *wt = *p;
  1415. if (wt->is_current)
  1416. continue;
  1417. cb->wt = wt;
  1418. refs_for_each_reflog(get_worktree_ref_store(wt),
  1419. handle_one_reflog,
  1420. cb);
  1421. }
  1422. free_worktrees(worktrees);
  1423. }
  1424. void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
  1425. {
  1426. struct all_refs_cb cb;
  1427. cb.all_revs = revs;
  1428. cb.all_flags = flags;
  1429. cb.wt = NULL;
  1430. for_each_reflog(handle_one_reflog, &cb);
  1431. if (!revs->single_worktree)
  1432. add_other_reflogs_to_pending(&cb);
  1433. }
  1434. static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
  1435. struct strbuf *path, unsigned int flags)
  1436. {
  1437. size_t baselen = path->len;
  1438. int i;
  1439. if (it->entry_count >= 0) {
  1440. struct tree *tree = lookup_tree(revs->repo, &it->oid);
  1441. tree->object.flags |= flags;
  1442. add_pending_object_with_path(revs, &tree->object, "",
  1443. 040000, path->buf);
  1444. }
  1445. for (i = 0; i < it->subtree_nr; i++) {
  1446. struct cache_tree_sub *sub = it->down[i];
  1447. strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
  1448. add_cache_tree(sub->cache_tree, revs, path, flags);
  1449. strbuf_setlen(path, baselen);
  1450. }
  1451. }
  1452. static void do_add_index_objects_to_pending(struct rev_info *revs,
  1453. struct index_state *istate,
  1454. unsigned int flags)
  1455. {
  1456. int i;
  1457. for (i = 0; i < istate->cache_nr; i++) {
  1458. struct cache_entry *ce = istate->cache[i];
  1459. struct blob *blob;
  1460. if (S_ISGITLINK(ce->ce_mode))
  1461. continue;
  1462. blob = lookup_blob(revs->repo, &ce->oid);
  1463. if (!blob)
  1464. die("unable to add index blob to traversal");
  1465. blob->object.flags |= flags;
  1466. add_pending_object_with_path(revs, &blob->object, "",
  1467. ce->ce_mode, ce->name);
  1468. }
  1469. if (istate->cache_tree) {
  1470. struct strbuf path = STRBUF_INIT;
  1471. add_cache_tree(istate->cache_tree, revs, &path, flags);
  1472. strbuf_release(&path);
  1473. }
  1474. }
  1475. void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
  1476. {
  1477. struct worktree **worktrees, **p;
  1478. repo_read_index(revs->repo);
  1479. do_add_index_objects_to_pending(revs, revs->repo->index, flags);
  1480. if (revs->single_worktree)
  1481. return;
  1482. worktrees = get_worktrees();
  1483. for (p = worktrees; *p; p++) {
  1484. struct worktree *wt = *p;
  1485. struct index_state istate = { NULL };
  1486. if (wt->is_current)
  1487. continue; /* current index already taken care of */
  1488. if (read_index_from(&istate,
  1489. worktree_git_path(wt, "index"),
  1490. get_worktree_git_dir(wt)) > 0)
  1491. do_add_index_objects_to_pending(revs, &istate, flags);
  1492. discard_index(&istate);
  1493. }
  1494. free_worktrees(worktrees);
  1495. }
  1496. struct add_alternate_refs_data {
  1497. struct rev_info *revs;
  1498. unsigned int flags;
  1499. };
  1500. static void add_one_alternate_ref(const struct object_id *oid,
  1501. void *vdata)
  1502. {
  1503. const char *name = ".alternate";
  1504. struct add_alternate_refs_data *data = vdata;
  1505. struct object *obj;
  1506. obj = get_reference(data->revs, name, oid, data->flags);
  1507. add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
  1508. add_pending_object(data->revs, obj, name);
  1509. }
  1510. static void add_alternate_refs_to_pending(struct rev_info *revs,
  1511. unsigned int flags)
  1512. {
  1513. struct add_alternate_refs_data data;
  1514. data.revs = revs;
  1515. data.flags = flags;
  1516. for_each_alternate_ref(add_one_alternate_ref, &data);
  1517. }
  1518. static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
  1519. int exclude_parent)
  1520. {
  1521. struct object_id oid;
  1522. struct object *it;
  1523. struct commit *commit;
  1524. struct commit_list *parents;
  1525. int parent_number;
  1526. const char *arg = arg_;
  1527. if (*arg == '^') {
  1528. flags ^= UNINTERESTING | BOTTOM;
  1529. arg++;
  1530. }
  1531. if (get_oid_committish(arg, &oid))
  1532. return 0;
  1533. while (1) {
  1534. it = get_reference(revs, arg, &oid, 0);
  1535. if (!it && revs->ignore_missing)
  1536. return 0;
  1537. if (it->type != OBJ_TAG)
  1538. break;
  1539. if (!((struct tag*)it)->tagged)
  1540. return 0;
  1541. oidcpy(&oid, &((struct tag*)it)->tagged->oid);
  1542. }
  1543. if (it->type != OBJ_COMMIT)
  1544. return 0;
  1545. commit = (struct commit *)it;
  1546. if (exclude_parent &&
  1547. exclude_parent > commit_list_count(commit->parents))
  1548. return 0;
  1549. for (parents = commit->parents, parent_number = 1;
  1550. parents;
  1551. parents = parents->next, parent_number++) {
  1552. if (exclude_parent && parent_number != exclude_parent)
  1553. continue;
  1554. it = &parents->item->object;
  1555. it->flags |= flags;
  1556. add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
  1557. add_pending_object(revs, it, arg);
  1558. }
  1559. return 1;
  1560. }
  1561. void repo_init_revisions(struct repository *r,
  1562. struct rev_info *revs,
  1563. const char *prefix)
  1564. {
  1565. memset(revs, 0, sizeof(*revs));
  1566. revs->repo = r;
  1567. revs->abbrev = DEFAULT_ABBREV;
  1568. revs->ignore_merges = -1;
  1569. revs->simplify_history = 1;
  1570. revs->pruning.repo = r;
  1571. revs->pruning.flags.recursive = 1;
  1572. revs->pruning.flags.quick = 1;
  1573. revs->pruning.add_remove = file_add_remove;
  1574. revs->pruning.change = file_change;
  1575. revs->pruning.change_fn_data = revs;
  1576. revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
  1577. revs->dense = 1;
  1578. revs->prefix = prefix;
  1579. revs->max_age = -1;
  1580. revs->min_age = -1;
  1581. revs->skip_count = -1;
  1582. revs->max_count = -1;
  1583. revs->max_parents = -1;
  1584. revs->expand_tabs_in_log = -1;
  1585. revs->commit_format = CMIT_FMT_DEFAULT;
  1586. revs->expand_tabs_in_log_default = 8;
  1587. init_grep_defaults(revs->repo);
  1588. grep_init(&revs->grep_filter, revs->repo, prefix);
  1589. revs->grep_filter.status_only = 1;
  1590. repo_diff_setup(revs->repo, &revs->diffopt);
  1591. if (prefix && !revs->diffopt.prefix) {
  1592. revs->diffopt.prefix = prefix;
  1593. revs->diffopt.prefix_length = strlen(prefix);
  1594. }
  1595. init_display_notes(&revs->notes_opt);
  1596. }
  1597. static void add_pending_commit_list(struct rev_info *revs,
  1598. struct commit_list *commit_list,
  1599. unsigned int flags)
  1600. {
  1601. while (commit_list) {
  1602. struct object *object = &commit_list->item->object;
  1603. object->flags |= flags;
  1604. add_pending_object(revs, object, oid_to_hex(&object->oid));
  1605. commit_list = commit_list->next;
  1606. }
  1607. }
  1608. static void prepare_show_merge(struct rev_info *revs)
  1609. {
  1610. struct commit_list *bases;
  1611. struct commit *head, *other;
  1612. struct object_id oid;
  1613. const char **prune = NULL;
  1614. int i, prune_num = 1; /* counting terminating NULL */
  1615. struct index_state *istate = revs->repo->index;
  1616. if (get_oid("HEAD", &oid))
  1617. die("--merge without HEAD?");
  1618. head = lookup_commit_or_die(&oid, "HEAD");
  1619. if (get_oid("MERGE_HEAD", &oid))
  1620. die("--merge without MERGE_HEAD?");
  1621. other = lookup_commit_or_die(&oid, "MERGE_HEAD");
  1622. add_pending_object(revs, &head->object, "HEAD");
  1623. add_pending_object(revs, &other->object, "MERGE_HEAD");
  1624. bases = get_merge_bases(head, other);
  1625. add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
  1626. add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
  1627. free_commit_list(bases);
  1628. head->object.flags |= SYMMETRIC_LEFT;
  1629. if (!istate->cache_nr)
  1630. repo_read_index(revs->repo);
  1631. for (i = 0; i < istate->cache_nr; i++) {
  1632. const struct cache_entry *ce = istate->cache[i];
  1633. if (!ce_stage(ce))
  1634. continue;
  1635. if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
  1636. prune_num++;
  1637. REALLOC_ARRAY(prune, prune_num);
  1638. prune[prune_num-2] = ce->name;
  1639. prune[prune_num-1] = NULL;
  1640. }
  1641. while ((i+1 < istate->cache_nr) &&
  1642. ce_same_name(ce, istate->cache[i+1]))
  1643. i++;
  1644. }
  1645. clear_pathspec(&revs->prune_data);
  1646. parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
  1647. PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
  1648. revs->limited = 1;
  1649. }
  1650. static int dotdot_missing(const char *arg, char *dotdot,
  1651. struct rev_info *revs, int symmetric)
  1652. {
  1653. if (revs->ignore_missing)
  1654. return 0;
  1655. /* de-munge so we report the full argument */
  1656. *dotdot = '.';
  1657. die(symmetric
  1658. ? "Invalid symmetric difference expression %s"
  1659. : "Invalid revision range %s", arg);
  1660. }
  1661. static int handle_dotdot_1(const char *arg, char *dotdot,
  1662. struct rev_info *revs, int flags,
  1663. int cant_be_filename,
  1664. struct object_context *a_oc,
  1665. struct object_context *b_oc)
  1666. {
  1667. const char *a_name, *b_name;
  1668. struct object_id a_oid, b_oid;
  1669. struct object *a_obj, *b_obj;
  1670. unsigned int a_flags, b_flags;
  1671. int symmetric = 0;
  1672. unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
  1673. unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
  1674. a_name = arg;
  1675. if (!*a_name)
  1676. a_name = "HEAD";
  1677. b_name = dotdot + 2;
  1678. if (*b_name == '.') {
  1679. symmetric = 1;
  1680. b_name++;
  1681. }
  1682. if (!*b_name)
  1683. b_name = "HEAD";
  1684. if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
  1685. get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
  1686. return -1;
  1687. if (!cant_be_filename) {
  1688. *dotdot = '.';
  1689. verify_non_filename(revs->prefix, arg);
  1690. *dotdot = '\0';
  1691. }
  1692. a_obj = parse_object(revs->repo, &a_oid);
  1693. b_obj = parse_object(revs->repo, &b_oid);
  1694. if (!a_obj || !b_obj)
  1695. return dotdot_missing(arg, dotdot, revs, symmetric);
  1696. if (!symmetric) {
  1697. /* just A..B */
  1698. b_flags = flags;
  1699. a_flags = flags_exclude;
  1700. } else {
  1701. /* A...B -- find merge bases between the two */
  1702. struct commit *a, *b;
  1703. struct commit_list *exclude;
  1704. a = lookup_commit_reference(revs->repo, &a_obj->oid);
  1705. b = lookup_commit_reference(revs->repo, &b_obj->oid);
  1706. if (!a || !b)
  1707. return dotdot_missing(arg, dotdot, revs, symmetric);
  1708. exclude = get_merge_bases(a, b);
  1709. add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
  1710. flags_exclude);
  1711. add_pending_commit_list(revs, exclude, flags_exclude);
  1712. free_commit_list(exclude);
  1713. b_flags = flags;
  1714. a_flags = flags | SYMMETRIC_LEFT;
  1715. }
  1716. a_obj->flags |= a_flags;
  1717. b_obj->flags |= b_flags;
  1718. add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
  1719. add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
  1720. add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
  1721. add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
  1722. return 0;
  1723. }
  1724. static int handle_dotdot(const char *arg,
  1725. struct rev_info *revs, int flags,
  1726. int cant_be_filename)
  1727. {
  1728. struct object_context a_oc, b_oc;
  1729. char *dotdot = strstr(arg, "..");
  1730. int ret;
  1731. if (!dotdot)
  1732. return -1;
  1733. memset(&a_oc, 0, sizeof(a_oc));
  1734. memset(&b_oc, 0, sizeof(b_oc));
  1735. *dotdot = '\0';
  1736. ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
  1737. &a_oc, &b_oc);
  1738. *dotdot = '.';
  1739. free(a_oc.path);
  1740. free(b_oc.path);
  1741. return ret;
  1742. }
  1743. static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
  1744. {
  1745. struct object_context oc;
  1746. char *mark;
  1747. struct object *object;
  1748. struct object_id oid;
  1749. int local_flags;
  1750. const char *arg = arg_;
  1751. int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
  1752. unsigned get_sha1_flags = GET_OID_RECORD_PATH;
  1753. flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
  1754. if (!cant_be_filename && !strcmp(arg, "..")) {
  1755. /*
  1756. * Just ".."? That is not a range but the
  1757. * pathspec for the parent directory.
  1758. */
  1759. return -1;
  1760. }
  1761. if (!handle_dotdot(arg, revs, flags, revarg_opt))
  1762. return 0;
  1763. mark = strstr(arg, "^@");
  1764. if (mark && !mark[2]) {
  1765. *mark = 0;
  1766. if (add_parents_only(revs, arg, flags, 0))
  1767. return 0;
  1768. *mark = '^';
  1769. }
  1770. mark = strstr(arg, "^!");
  1771. if (mark && !mark[2]) {
  1772. *mark = 0;
  1773. if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
  1774. *mark = '^';
  1775. }
  1776. mark = strstr(arg, "^-");
  1777. if (mark) {
  1778. int exclude_parent = 1;
  1779. if (mark[2]) {
  1780. char *end;
  1781. exclude_parent = strtoul(mark + 2, &end, 10);
  1782. if (*end != '\0' || !exclude_parent)
  1783. return -1;
  1784. }
  1785. *mark = 0;
  1786. if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
  1787. *mark = '^';
  1788. }
  1789. local_flags = 0;
  1790. if (*arg == '^') {
  1791. local_flags = UNINTERESTING | BOTTOM;
  1792. arg++;
  1793. }
  1794. if (revarg_opt & REVARG_COMMITTISH)
  1795. get_sha1_flags |= GET_OID_COMMITTISH;
  1796. if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
  1797. return revs->ignore_missing ? 0 : -1;
  1798. if (!cant_be_filename)
  1799. verify_non_filename(revs->prefix, arg);
  1800. object = get_reference(revs, arg, &oid, flags ^ local_flags);
  1801. if (!object)
  1802. return revs->ignore_missing ? 0 : -1;
  1803. add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
  1804. add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
  1805. free(oc.path);
  1806. return 0;
  1807. }
  1808. int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
  1809. {
  1810. int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
  1811. if (!ret)
  1812. revs->rev_input_given = 1;
  1813. return ret;
  1814. }
  1815. static void read_pathspec_from_stdin(struct strbuf *sb,
  1816. struct strvec *prune)
  1817. {
  1818. while (strbuf_getline(sb, stdin) != EOF)
  1819. strvec_push(prune, sb->buf);
  1820. }
  1821. static void read_revisions_from_stdin(struct rev_info *revs,
  1822. struct strvec *prune)
  1823. {
  1824. struct strbuf sb;
  1825. int seen_dashdash = 0;
  1826. int save_warning;
  1827. save_warning = warn_on_object_refname_ambiguity;
  1828. warn_on_object_refname_ambiguity = 0;
  1829. strbuf_init(&sb, 1000);
  1830. while (strbuf_getline(&sb, stdin) != EOF) {
  1831. int len = sb.len;
  1832. if (!len)
  1833. break;
  1834. if (sb.buf[0] == '-') {
  1835. if (len == 2 && sb.buf[1] == '-') {
  1836. seen_dashdash = 1;
  1837. break;
  1838. }
  1839. die("options not supported in --stdin mode");
  1840. }
  1841. if (handle_revision_arg(sb.buf, revs, 0,
  1842. REVARG_CANNOT_BE_FILENAME))
  1843. die("bad revision '%s'", sb.buf);
  1844. }
  1845. if (seen_dashdash)
  1846. read_pathspec_from_stdin(&sb, prune);
  1847. strbuf_release(&sb);
  1848. warn_on_object_refname_ambiguity = save_warning;
  1849. }
  1850. static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
  1851. {
  1852. append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
  1853. }
  1854. static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
  1855. {
  1856. append_header_grep_pattern(&revs->grep_filter, field, pattern);
  1857. }
  1858. static void add_message_grep(struct rev_info *revs, const char *pattern)
  1859. {
  1860. add_grep(revs, pattern, GREP_PATTERN_BODY);
  1861. }
  1862. static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
  1863. int *unkc, const char **unkv,
  1864. const struct setup_revision_opt* opt)
  1865. {
  1866. const char *arg = argv[0];
  1867. const char *optarg;
  1868. int argcount;
  1869. const unsigned hexsz = the_hash_algo->hexsz;
  1870. /* pseudo revision arguments */
  1871. if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
  1872. !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
  1873. !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
  1874. !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
  1875. !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
  1876. !strcmp(arg, "--indexed-objects") ||
  1877. !strcmp(arg, "--alternate-refs") ||
  1878. starts_with(arg, "--exclude=") ||
  1879. starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
  1880. starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
  1881. {
  1882. unkv[(*unkc)++] = arg;
  1883. return 1;
  1884. }
  1885. if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
  1886. revs->max_count = atoi(optarg);
  1887. revs->no_walk = 0;
  1888. return argcount;
  1889. } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
  1890. revs->skip_count = atoi(optarg);
  1891. return argcount;
  1892. } else if ((*arg == '-') && isdigit(arg[1])) {
  1893. /* accept -<digit>, like traditional "head" */
  1894. if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
  1895. revs->max_count < 0)
  1896. die("'%s': not a non-negative integer", arg + 1);
  1897. revs->no_walk = 0;
  1898. } else if (!strcmp(arg, "-n")) {
  1899. if (argc <= 1)
  1900. return error("-n requires an argument");
  1901. revs->max_count = atoi(argv[1]);
  1902. revs->no_walk = 0;
  1903. return 2;
  1904. } else if (skip_prefix(arg, "-n", &optarg)) {
  1905. revs->max_count = atoi(optarg);
  1906. revs->no_walk = 0;
  1907. } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
  1908. revs->max_age = atoi(optarg);
  1909. return argcount;
  1910. } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
  1911. revs->max_age = approxidate(optarg);
  1912. return argcount;
  1913. } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
  1914. revs->max_age = approxidate(optarg);
  1915. return argcount;
  1916. } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
  1917. revs->min_age = atoi(optarg);
  1918. return argcount;
  1919. } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
  1920. revs->min_age = approxidate(optarg);
  1921. return argcount;
  1922. } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
  1923. revs->min_age = approxidate(optarg);
  1924. return argcount;
  1925. } else if (!strcmp(arg, "--first-parent")) {
  1926. revs->first_parent_only = 1;
  1927. } else if (!strcmp(arg, "--ancestry-path")) {
  1928. revs->ancestry_path = 1;
  1929. revs->simplify_history = 0;
  1930. revs->limited = 1;
  1931. } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
  1932. init_reflog_walk(&revs->reflog_info);
  1933. } else if (!strcmp(arg, "--default")) {
  1934. if (argc <= 1)
  1935. return error("bad --default argument");
  1936. revs->def = argv[1];
  1937. return 2;
  1938. } else if (!strcmp(arg, "--merge")) {
  1939. revs->show_merge = 1;
  1940. } else if (!strcmp(arg, "--topo-order")) {
  1941. revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
  1942. revs->topo_order = 1;
  1943. } else if (!strcmp(arg, "--simplify-merges")) {
  1944. revs->simplify_merges = 1;
  1945. revs->topo_order = 1;
  1946. revs->rewrite_parents = 1;
  1947. revs->simplify_history = 0;
  1948. revs->limited = 1;
  1949. } else if (!strcmp(arg, "--simplify-by-decoration")) {
  1950. revs->simplify_merges = 1;
  1951. revs->topo_order = 1;
  1952. revs->rewrite_parents = 1;
  1953. revs->simplify_history = 0;
  1954. revs->simplify_by_decoration = 1;
  1955. revs->limited = 1;
  1956. revs->prune = 1;
  1957. } else if (!strcmp(arg, "--date-order")) {
  1958. revs->sort_order = REV_SORT_BY_COMMIT_DATE;
  1959. revs->topo_order = 1;
  1960. } else if (!strcmp(arg, "--author-date-order")) {
  1961. revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
  1962. revs->topo_order = 1;
  1963. } else if (!strcmp(arg, "--early-output")) {
  1964. revs->early_output = 100;
  1965. revs->topo_order = 1;
  1966. } else if (skip_prefix(arg, "--early-output=", &optarg)) {
  1967. if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
  1968. die("'%s': not a non-negative integer", optarg);
  1969. revs->topo_order = 1;
  1970. } else if (!strcmp(arg, "--parents")) {
  1971. revs->rewrite_parents = 1;
  1972. revs->print_parents = 1;
  1973. } else if (!strcmp(arg, "--dense")) {
  1974. revs->dense = 1;
  1975. } else if (!strcmp(arg, "--sparse")) {
  1976. revs->dense = 0;
  1977. } else if (!strcmp(arg, "--in-commit-order")) {
  1978. revs->tree_blobs_in_commit_order = 1;
  1979. } else if (!strcmp(arg, "--remove-empty")) {
  1980. revs->remove_empty_trees = 1;
  1981. } else if (!strcmp(arg, "--merges")) {
  1982. revs->min_parents = 2;
  1983. } else if (!strcmp(arg, "--no-merges")) {
  1984. revs->max_parents = 1;
  1985. } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
  1986. revs->min_parents = atoi(optarg);
  1987. } else if (!strcmp(arg, "--no-min-parents")) {
  1988. revs->min_parents = 0;
  1989. } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
  1990. revs->max_parents = atoi(optarg);
  1991. } else if (!strcmp(arg, "--no-max-parents")) {
  1992. revs->max_parents = -1;
  1993. } else if (!strcmp(arg, "--boundary")) {
  1994. revs->boundary = 1;
  1995. } else if (!strcmp(arg, "--left-right")) {
  1996. revs->left_right = 1;
  1997. } else if (!strcmp(arg, "--left-only")) {
  1998. if (revs->right_only)
  1999. die("--left-only is incompatible with --right-only"
  2000. " or --cherry");
  2001. revs->left_only = 1;
  2002. } else if (!strcmp(arg, "--right-only")) {
  2003. if (revs->left_only)
  2004. die("--right-only is incompatible with --left-only");
  2005. revs->right_only = 1;
  2006. } else if (!strcmp(arg, "--cherry")) {
  2007. if (revs->left_only)
  2008. die("--cherry is incompatible with --left-only");
  2009. revs->cherry_mark = 1;
  2010. revs->right_only = 1;
  2011. revs->max_parents = 1;
  2012. revs->limited = 1;
  2013. } else if (!strcmp(arg, "--count")) {
  2014. revs->count = 1;
  2015. } else if (!strcmp(arg, "--cherry-mark")) {
  2016. if (revs->cherry_pick)
  2017. die("--cherry-mark is incompatible with --cherry-pick");
  2018. revs->cherry_mark = 1;
  2019. revs->limited = 1; /* needs limit_list() */
  2020. } else if (!strcmp(arg, "--cherry-pick")) {
  2021. if (revs->cherry_mark)
  2022. die("--cherry-pick is incompatible with --cherry-mark");
  2023. revs->cherry_pick = 1;
  2024. revs->limited = 1;
  2025. } else if (!strcmp(arg, "--objects")) {
  2026. revs->tag_objects = 1;
  2027. revs->tree_objects = 1;
  2028. revs->blob_objects = 1;
  2029. } else if (!strcmp(arg, "--objects-edge")) {
  2030. revs->tag_objects = 1;
  2031. revs->tree_objects = 1;
  2032. revs->blob_objects = 1;
  2033. revs->edge_hint = 1;
  2034. } else if (!strcmp(arg, "--objects-edge-aggressive")) {
  2035. revs->tag_objects = 1;
  2036. revs->tree_objects = 1;
  2037. revs->blob_objects = 1;
  2038. revs->edge_hint = 1;
  2039. revs->edge_hint_aggressive = 1;
  2040. } else if (!strcmp(arg, "--verify-objects")) {
  2041. revs->tag_objects = 1;
  2042. revs->tree_objects = 1;
  2043. revs->blob_objects = 1;
  2044. revs->verify_objects = 1;
  2045. } else if (!strcmp(arg, "--unpacked")) {
  2046. revs->unpacked = 1;
  2047. } else if (starts_with(arg, "--unpacked=")) {
  2048. die(_("--unpacked=<packfile> no longer supported"));
  2049. } else if (!strcmp(arg, "-r")) {
  2050. revs->diff = 1;
  2051. revs->diffopt.flags.recursive = 1;
  2052. } else if (!strcmp(arg, "-t")) {
  2053. revs->diff = 1;
  2054. revs->diffopt.flags.recursive = 1;
  2055. revs->diffopt.flags.tree_in_recursive = 1;
  2056. } else if (!strcmp(arg, "-m")) {
  2057. /*
  2058. * To "diff-index", "-m" means "match missing", and to the "log"
  2059. * family of commands, it means "show full diff for merges". Set
  2060. * both fields appropriately.
  2061. */
  2062. revs->ignore_merges = 0;
  2063. revs->match_missing = 1;
  2064. } else if ((argcount = parse_long_opt("diff-merges", argv, &optarg))) {
  2065. if (!strcmp(optarg, "off")) {
  2066. revs->ignore_merges = 1;
  2067. } else {
  2068. die(_("unknown value for --diff-merges: %s"), optarg);
  2069. }
  2070. return argcount;
  2071. } else if (!strcmp(arg, "--no-diff-merges")) {
  2072. revs->ignore_merges = 1;
  2073. } else if (!strcmp(arg, "-c")) {
  2074. revs->diff = 1;
  2075. revs->dense_combined_merges = 0;
  2076. revs->combine_merges = 1;
  2077. } else if (!strcmp(arg, "--combined-all-paths")) {
  2078. revs->diff = 1;
  2079. revs->combined_all_paths = 1;
  2080. } else if (!strcmp(arg, "--cc")) {
  2081. revs->diff = 1;
  2082. revs->dense_combined_merges = 1;
  2083. revs->combine_merges = 1;
  2084. } else if (!strcmp(arg, "-v")) {
  2085. revs->verbose_header = 1;
  2086. } else if (!strcmp(arg, "--pretty")) {
  2087. revs->verbose_header = 1;
  2088. revs->pretty_given = 1;
  2089. get_commit_format(NULL, revs);
  2090. } else if (skip_prefix(arg, "--pretty=", &optarg) ||
  2091. skip_prefix(arg, "--format=", &optarg)) {
  2092. /*
  2093. * Detached form ("--pretty X" as opposed to "--pretty=X")
  2094. * not allowed, since the argument is optional.
  2095. */
  2096. revs->verbose_header = 1;
  2097. revs->pretty_given = 1;
  2098. get_commit_format(optarg, revs);
  2099. } else if (!strcmp(arg, "--expand-tabs")) {
  2100. revs->expand_tabs_in_log = 8;
  2101. } else if (!strcmp(arg, "--no-expand-tabs")) {
  2102. revs->expand_tabs_in_log = 0;
  2103. } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
  2104. int val;
  2105. if (strtol_i(arg, 10, &val) < 0 || val < 0)
  2106. die("'%s': not a non-negative integer", arg);
  2107. revs->expand_tabs_in_log = val;
  2108. } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
  2109. enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
  2110. revs->show_notes_given = 1;
  2111. } else if (!strcmp(arg, "--show-signature")) {
  2112. revs->show_signature = 1;
  2113. } else if (!strcmp(arg, "--no-show-signature")) {
  2114. revs->show_signature = 0;
  2115. } else if (!strcmp(arg, "--show-linear-break")) {
  2116. revs->break_bar = " ..........";
  2117. revs->track_linear = 1;
  2118. revs->track_first_time = 1;
  2119. } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
  2120. revs->break_bar = xstrdup(optarg);
  2121. revs->track_linear = 1;
  2122. revs->track_first_time = 1;
  2123. } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
  2124. skip_prefix(arg, "--notes=", &optarg)) {
  2125. if (starts_with(arg, "--show-notes=") &&
  2126. revs->notes_opt.use_default_notes < 0)
  2127. revs->notes_opt.use_default_notes = 1;
  2128. enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
  2129. revs->show_notes_given = 1;
  2130. } else if (!strcmp(arg, "--no-notes")) {
  2131. disable_display_notes(&revs->notes_opt, &revs->show_notes);
  2132. revs->show_notes_given = 1;
  2133. } else if (!strcmp(arg, "--standard-notes")) {
  2134. revs->show_notes_given = 1;
  2135. revs->notes_opt.use_default_notes = 1;
  2136. } else if (!strcmp(arg, "--no-standard-notes")) {
  2137. revs->notes_opt.use_default_notes = 0;
  2138. } else if (!strcmp(arg, "--oneline")) {
  2139. revs->verbose_header = 1;
  2140. get_commit_format("oneline", revs);
  2141. revs->pretty_given = 1;
  2142. revs->abbrev_commit = 1;
  2143. } else if (!strcmp(arg, "--graph")) {
  2144. revs->topo_order = 1;
  2145. revs->rewrite_parents = 1;
  2146. revs->graph = graph_init(revs);
  2147. } else if (!strcmp(arg, "--encode-email-headers")) {
  2148. revs->encode_email_headers = 1;
  2149. } else if (!strcmp(arg, "--no-encode-email-headers")) {
  2150. revs->encode_email_headers = 0;
  2151. } else if (!strcmp(arg, "--root")) {
  2152. revs->show_root_diff = 1;
  2153. } else if (!strcmp(arg, "--no-commit-id")) {
  2154. revs->no_commit_id = 1;
  2155. } else if (!strcmp(arg, "--always")) {
  2156. revs->always_show_header = 1;
  2157. } else if (!strcmp(arg, "--no-abbrev")) {
  2158. revs->abbrev = 0;
  2159. } else if (!strcmp(arg, "--abbrev")) {
  2160. revs->abbrev = DEFAULT_ABBREV;
  2161. } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
  2162. revs->abbrev = strtoul(optarg, NULL, 10);
  2163. if (revs->abbrev < MINIMUM_ABBREV)
  2164. revs->abbrev = MINIMUM_ABBREV;
  2165. else if (revs->abbrev > hexsz)
  2166. revs->abbrev = hexsz;
  2167. } else if (!strcmp(arg, "--abbrev-commit")) {
  2168. revs->abbrev_commit = 1;
  2169. revs->abbrev_commit_given = 1;
  2170. } else if (!strcmp(arg, "--no-abbrev-commit")) {
  2171. revs->abbrev_commit = 0;
  2172. } else if (!strcmp(arg, "--full-diff")) {
  2173. revs->diff = 1;
  2174. revs->full_diff = 1;
  2175. } else if (!strcmp(arg, "--show-pulls")) {
  2176. revs->show_pulls = 1;
  2177. } else if (!strcmp(arg, "--full-history")) {
  2178. revs->simplify_history = 0;
  2179. } else if (!strcmp(arg, "--relative-date")) {
  2180. revs->date_mode.type = DATE_RELATIVE;
  2181. revs->date_mode_explicit = 1;
  2182. } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
  2183. parse_date_format(optarg, &revs->date_mode);
  2184. revs->date_mode_explicit = 1;
  2185. return argcount;
  2186. } else if (!strcmp(arg, "--log-size")) {
  2187. revs->show_log_size = 1;
  2188. }
  2189. /*
  2190. * Grepping the commit log
  2191. */
  2192. else if ((argcount = parse_long_opt("author", argv, &optarg))) {
  2193. add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
  2194. return argcount;
  2195. } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
  2196. add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
  2197. return argcount;
  2198. } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
  2199. add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
  2200. return argcount;
  2201. } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
  2202. add_message_grep(revs, optarg);
  2203. return argcount;
  2204. } else if (!strcmp(arg, "--grep-debug")) {
  2205. revs->grep_filter.debug = 1;
  2206. } else if (!strcmp(arg, "--basic-regexp")) {
  2207. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
  2208. } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
  2209. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
  2210. } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
  2211. revs->grep_filter.ignore_case = 1;
  2212. revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
  2213. } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
  2214. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
  2215. } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
  2216. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
  2217. } else if (!strcmp(arg, "--all-match")) {
  2218. revs->grep_filter.all_match = 1;
  2219. } else if (!strcmp(arg, "--invert-grep")) {
  2220. revs->invert_grep = 1;
  2221. } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
  2222. if (strcmp(optarg, "none"))
  2223. git_log_output_encoding = xstrdup(optarg);
  2224. else
  2225. git_log_output_encoding = "";
  2226. return argcount;
  2227. } else if (!strcmp(arg, "--reverse")) {
  2228. revs->reverse ^= 1;
  2229. } else if (!strcmp(arg, "--children")) {
  2230. revs->children.name = "children";
  2231. revs->limited = 1;
  2232. } else if (!strcmp(arg, "--ignore-missing")) {
  2233. revs->ignore_missing = 1;
  2234. } else if (opt && opt->allow_exclude_promisor_objects &&
  2235. !strcmp(arg, "--exclude-promisor-objects")) {
  2236. if (fetch_if_missing)
  2237. BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
  2238. revs->exclude_promisor_objects = 1;
  2239. } else {
  2240. int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
  2241. if (!opts)
  2242. unkv[(*unkc)++] = arg;
  2243. return opts;
  2244. }
  2245. if (revs->graph && revs->track_linear)
  2246. die("--show-linear-break and --graph are incompatible");
  2247. return 1;
  2248. }
  2249. void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
  2250. const struct option *options,
  2251. const char * const usagestr[])
  2252. {
  2253. int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
  2254. &ctx->cpidx, ctx->out, NULL);
  2255. if (n <= 0) {
  2256. error("unknown option `%s'", ctx->argv[0]);
  2257. usage_with_options(usagestr, options);
  2258. }
  2259. ctx->argv += n;
  2260. ctx->argc -= n;
  2261. }
  2262. static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
  2263. void *cb_data, const char *term)
  2264. {
  2265. struct strbuf bisect_refs = STRBUF_INIT;
  2266. int status;
  2267. strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
  2268. status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
  2269. strbuf_release(&bisect_refs);
  2270. return status;
  2271. }
  2272. static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
  2273. {
  2274. return for_each_bisect_ref(refs, fn, cb_data, term_bad);
  2275. }
  2276. static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
  2277. {
  2278. return for_each_bisect_ref(refs, fn, cb_data, term_good);
  2279. }
  2280. static int handle_revision_pseudo_opt(const char *submodule,
  2281. struct rev_info *revs,
  2282. const char **argv, int *flags)
  2283. {
  2284. const char *arg = argv[0];
  2285. const char *optarg;
  2286. struct ref_store *refs;
  2287. int argcount;
  2288. if (submodule) {
  2289. /*
  2290. * We need some something like get_submodule_worktrees()
  2291. * before we can go through all worktrees of a submodule,
  2292. * .e.g with adding all HEADs from --all, which is not
  2293. * supported right now, so stick to single worktree.
  2294. */
  2295. if (!revs->single_worktree)
  2296. BUG("--single-worktree cannot be used together with submodule");
  2297. refs = get_submodule_ref_store(submodule);
  2298. } else
  2299. refs = get_main_ref_store(revs->repo);
  2300. /*
  2301. * NOTE!
  2302. *
  2303. * Commands like "git shortlog" will not accept the options below
  2304. * unless parse_revision_opt queues them (as opposed to erroring
  2305. * out).
  2306. *
  2307. * When implementing your new pseudo-option, remember to
  2308. * register it in the list at the top of handle_revision_opt.
  2309. */
  2310. if (!strcmp(arg, "--all")) {
  2311. handle_refs(refs, revs, *flags, refs_for_each_ref);
  2312. handle_refs(refs, revs, *flags, refs_head_ref);
  2313. if (!revs->single_worktree) {
  2314. struct all_refs_cb cb;
  2315. init_all_refs_cb(&cb, revs, *flags);
  2316. other_head_refs(handle_one_ref, &cb);
  2317. }
  2318. clear_ref_exclusion(&revs->ref_excludes);
  2319. } else if (!strcmp(arg, "--branches")) {
  2320. handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
  2321. clear_ref_exclusion(&revs->ref_excludes);
  2322. } else if (!strcmp(arg, "--bisect")) {
  2323. read_bisect_terms(&term_bad, &term_good);
  2324. handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
  2325. handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
  2326. for_each_good_bisect_ref);
  2327. revs->bisect = 1;
  2328. } else if (!strcmp(arg, "--tags")) {
  2329. handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
  2330. clear_ref_exclusion(&revs->ref_excludes);
  2331. } else if (!strcmp(arg, "--remotes")) {
  2332. handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
  2333. clear_ref_exclusion(&revs->ref_excludes);
  2334. } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
  2335. struct all_refs_cb cb;
  2336. init_all_refs_cb(&cb, revs, *flags);
  2337. for_each_glob_ref(handle_one_ref, optarg, &cb);
  2338. clear_ref_exclusion(&revs->ref_excludes);
  2339. return argcount;
  2340. } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
  2341. add_ref_exclusion(&revs->ref_excludes, optarg);
  2342. return argcount;
  2343. } else if (skip_prefix(arg, "--branches=", &optarg)) {
  2344. struct all_refs_cb cb;
  2345. init_all_refs_cb(&cb, revs, *flags);
  2346. for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
  2347. clear_ref_exclusion(&revs->ref_excludes);
  2348. } else if (skip_prefix(arg, "--tags=", &optarg)) {
  2349. struct all_refs_cb cb;
  2350. init_all_refs_cb(&cb, revs, *flags);
  2351. for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
  2352. clear_ref_exclusion(&revs->ref_excludes);
  2353. } else if (skip_prefix(arg, "--remotes=", &optarg)) {
  2354. struct all_refs_cb cb;
  2355. init_all_refs_cb(&cb, revs, *flags);
  2356. for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
  2357. clear_ref_exclusion(&revs->ref_excludes);
  2358. } else if (!strcmp(arg, "--reflog")) {
  2359. add_reflogs_to_pending(revs, *flags);
  2360. } else if (!strcmp(arg, "--indexed-objects")) {
  2361. add_index_objects_to_pending(revs, *flags);
  2362. } else if (!strcmp(arg, "--alternate-refs")) {
  2363. add_alternate_refs_to_pending(revs, *flags);
  2364. } else if (!strcmp(arg, "--not")) {
  2365. *flags ^= UNINTERESTING | BOTTOM;
  2366. } else if (!strcmp(arg, "--no-walk")) {
  2367. revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
  2368. } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
  2369. /*
  2370. * Detached form ("--no-walk X" as opposed to "--no-walk=X")
  2371. * not allowed, since the argument is optional.
  2372. */
  2373. if (!strcmp(optarg, "sorted"))
  2374. revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
  2375. else if (!strcmp(optarg, "unsorted"))
  2376. revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
  2377. else
  2378. return error("invalid argument to --no-walk");
  2379. } else if (!strcmp(arg, "--do-walk")) {
  2380. revs->no_walk = 0;
  2381. } else if (!strcmp(arg, "--single-worktree")) {
  2382. revs->single_worktree = 1;
  2383. } else {
  2384. return 0;
  2385. }
  2386. return 1;
  2387. }
  2388. static void NORETURN diagnose_missing_default(const char *def)
  2389. {
  2390. int flags;
  2391. const char *refname;
  2392. refname = resolve_ref_unsafe(def, 0, NULL, &flags);
  2393. if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
  2394. die(_("your current branch appears to be broken"));
  2395. skip_prefix(refname, "refs/heads/", &refname);
  2396. die(_("your current branch '%s' does not have any commits yet"),
  2397. refname);
  2398. }
  2399. /*
  2400. * Parse revision information, filling in the "rev_info" structure,
  2401. * and removing the used arguments from the argument list.
  2402. *
  2403. * Returns the number of arguments left that weren't recognized
  2404. * (which are also moved to the head of the argument list)
  2405. */
  2406. int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
  2407. {
  2408. int i, flags, left, seen_dashdash, revarg_opt;
  2409. struct strvec prune_data = STRVEC_INIT;
  2410. const char *submodule = NULL;
  2411. int seen_end_of_options = 0;
  2412. if (opt)
  2413. submodule = opt->submodule;
  2414. /* First, search for "--" */
  2415. if (opt && opt->assume_dashdash) {
  2416. seen_dashdash = 1;
  2417. } else {
  2418. seen_dashdash = 0;
  2419. for (i = 1; i < argc; i++) {
  2420. const char *arg = argv[i];
  2421. if (strcmp(arg, "--"))
  2422. continue;
  2423. argv[i] = NULL;
  2424. argc = i;
  2425. if (argv[i + 1])
  2426. strvec_pushv(&prune_data, argv + i + 1);
  2427. seen_dashdash = 1;
  2428. break;
  2429. }
  2430. }
  2431. /* Second, deal with arguments and options */
  2432. flags = 0;
  2433. revarg_opt = opt ? opt->revarg_opt : 0;
  2434. if (seen_dashdash)
  2435. revarg_opt |= REVARG_CANNOT_BE_FILENAME;
  2436. for (left = i = 1; i < argc; i++) {
  2437. const char *arg = argv[i];
  2438. if (!seen_end_of_options && *arg == '-') {
  2439. int opts;
  2440. opts = handle_revision_pseudo_opt(submodule,
  2441. revs, argv + i,
  2442. &flags);
  2443. if (opts > 0) {
  2444. i += opts - 1;
  2445. continue;
  2446. }
  2447. if (!strcmp(arg, "--stdin")) {
  2448. if (revs->disable_stdin) {
  2449. argv[left++] = arg;
  2450. continue;
  2451. }
  2452. if (revs->read_from_stdin++)
  2453. die("--stdin given twice?");
  2454. read_revisions_from_stdin(revs, &prune_data);
  2455. continue;
  2456. }
  2457. if (!strcmp(arg, "--end-of-options")) {
  2458. seen_end_of_options = 1;
  2459. continue;
  2460. }
  2461. opts = handle_revision_opt(revs, argc - i, argv + i,
  2462. &left, argv, opt);
  2463. if (opts > 0) {
  2464. i += opts - 1;
  2465. continue;
  2466. }
  2467. if (opts < 0)
  2468. exit(128);
  2469. continue;
  2470. }
  2471. if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
  2472. int j;
  2473. if (seen_dashdash || *arg == '^')
  2474. die("bad revision '%s'", arg);
  2475. /* If we didn't have a "--":
  2476. * (1) all filenames must exist;
  2477. * (2) all rev-args must not be interpretable
  2478. * as a valid filename.
  2479. * but the latter we have checked in the main loop.
  2480. */
  2481. for (j = i; j < argc; j++)
  2482. verify_filename(revs->prefix, argv[j], j == i);
  2483. strvec_pushv(&prune_data, argv + i);
  2484. break;
  2485. }
  2486. }
  2487. if (prune_data.nr) {
  2488. /*
  2489. * If we need to introduce the magic "a lone ':' means no
  2490. * pathspec whatsoever", here is the place to do so.
  2491. *
  2492. * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
  2493. * prune_data.nr = 0;
  2494. * prune_data.alloc = 0;
  2495. * free(prune_data.path);
  2496. * prune_data.path = NULL;
  2497. * } else {
  2498. * terminate prune_data.alloc with NULL and
  2499. * call init_pathspec() to set revs->prune_data here.
  2500. * }
  2501. */
  2502. parse_pathspec(&revs->prune_data, 0, 0,
  2503. revs->prefix, prune_data.v);
  2504. }
  2505. strvec_clear(&prune_data);
  2506. if (revs->def == NULL)
  2507. revs->def = opt ? opt->def : NULL;
  2508. if (opt && opt->tweak)
  2509. opt->tweak(revs, opt);
  2510. if (revs->show_merge)
  2511. prepare_show_merge(revs);
  2512. if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
  2513. struct object_id oid;
  2514. struct object *object;
  2515. struct object_context oc;
  2516. if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
  2517. diagnose_missing_default(revs->def);
  2518. object = get_reference(revs, revs->def, &oid, 0);
  2519. add_pending_object_with_mode(revs, object, revs->def, oc.mode);
  2520. }
  2521. /* Did the user ask for any diff output? Run the diff! */
  2522. if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
  2523. revs->diff = 1;
  2524. /* Pickaxe, diff-filter and rename following need diffs */
  2525. if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
  2526. revs->diffopt.filter ||
  2527. revs->diffopt.flags.follow_renames)
  2528. revs->diff = 1;
  2529. if (revs->diffopt.objfind)
  2530. revs->simplify_history = 0;
  2531. if (revs->line_level_traverse) {
  2532. if (want_ancestry(revs))
  2533. revs->limited = 1;
  2534. revs->topo_order = 1;
  2535. }
  2536. if (revs->topo_order && !generation_numbers_enabled(the_repository))
  2537. revs->limited = 1;
  2538. if (revs->prune_data.nr) {
  2539. copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
  2540. /* Can't prune commits with rename following: the paths change.. */
  2541. if (!revs->diffopt.flags.follow_renames)
  2542. revs->prune = 1;
  2543. if (!revs->full_diff)
  2544. copy_pathspec(&revs->diffopt.pathspec,
  2545. &revs->prune_data);
  2546. }
  2547. if (revs->combine_merges && revs->ignore_merges < 0)
  2548. revs->ignore_merges = 0;
  2549. if (revs->ignore_merges < 0)
  2550. revs->ignore_merges = 1;
  2551. if (revs->combined_all_paths && !revs->combine_merges)
  2552. die("--combined-all-paths makes no sense without -c or --cc");
  2553. revs->diffopt.abbrev = revs->abbrev;
  2554. diff_setup_done(&revs->diffopt);
  2555. grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
  2556. &revs->grep_filter);
  2557. if (!is_encoding_utf8(get_log_output_encoding()))
  2558. revs->grep_filter.ignore_locale = 1;
  2559. compile_grep_patterns(&revs->grep_filter);
  2560. if (revs->reverse && revs->reflog_info)
  2561. die("cannot combine --reverse with --walk-reflogs");
  2562. if (revs->reflog_info && revs->limited)
  2563. die("cannot combine --walk-reflogs with history-limiting options");
  2564. if (revs->rewrite_parents && revs->children.name)
  2565. die("cannot combine --parents and --children");
  2566. /*
  2567. * Limitations on the graph functionality
  2568. */
  2569. if (revs->reverse && revs->graph)
  2570. die("cannot combine --reverse with --graph");
  2571. if (revs->reflog_info && revs->graph)
  2572. die("cannot combine --walk-reflogs with --graph");
  2573. if (revs->no_walk && revs->graph)
  2574. die("cannot combine --no-walk with --graph");
  2575. if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
  2576. die("cannot use --grep-reflog without --walk-reflogs");
  2577. if (revs->line_level_traverse &&
  2578. (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
  2579. die(_("-L does not yet support diff formats besides -p and -s"));
  2580. if (revs->expand_tabs_in_log < 0)
  2581. revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
  2582. return left;
  2583. }
  2584. static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
  2585. {
  2586. struct commit_list *l = xcalloc(1, sizeof(*l));
  2587. l->item = child;
  2588. l->next = add_decoration(&revs->children, &parent->object, l);
  2589. }
  2590. static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
  2591. {
  2592. struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
  2593. struct commit_list **pp, *p;
  2594. int surviving_parents;
  2595. /* Examine existing parents while marking ones we have seen... */
  2596. pp = &commit->parents;
  2597. surviving_parents = 0;
  2598. while ((p = *pp) != NULL) {
  2599. struct commit *parent = p->item;
  2600. if (parent->object.flags & TMP_MARK) {
  2601. *pp = p->next;
  2602. if (ts)
  2603. compact_treesame(revs, commit, surviving_parents);
  2604. continue;
  2605. }
  2606. parent->object.flags |= TMP_MARK;
  2607. surviving_parents++;
  2608. pp = &p->next;
  2609. }
  2610. /* clear the temporary mark */
  2611. for (p = commit->parents; p; p = p->next) {
  2612. p->item->object.flags &= ~TMP_MARK;
  2613. }
  2614. /* no update_treesame() - removing duplicates can't affect TREESAME */
  2615. return surviving_parents;
  2616. }
  2617. struct merge_simplify_state {
  2618. struct commit *simplified;
  2619. };
  2620. static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
  2621. {
  2622. struct merge_simplify_state *st;
  2623. st = lookup_decoration(&revs->merge_simplification, &commit->object);
  2624. if (!st) {
  2625. st = xcalloc(1, sizeof(*st));
  2626. add_decoration(&revs->merge_simplification, &commit->object, st);
  2627. }
  2628. return st;
  2629. }
  2630. static int mark_redundant_parents(struct commit *commit)
  2631. {
  2632. struct commit_list *h = reduce_heads(commit->parents);
  2633. int i = 0, marked = 0;
  2634. struct commit_list *po, *pn;
  2635. /* Want these for sanity-checking only */
  2636. int orig_cnt = commit_list_count(commit->parents);
  2637. int cnt = commit_list_count(h);
  2638. /*
  2639. * Not ready to remove items yet, just mark them for now, based
  2640. * on the output of reduce_heads(). reduce_heads outputs the reduced
  2641. * set in its original order, so this isn't too hard.
  2642. */
  2643. po = commit->parents;
  2644. pn = h;
  2645. while (po) {
  2646. if (pn && po->item == pn->item) {
  2647. pn = pn->next;
  2648. i++;
  2649. } else {
  2650. po->item->object.flags |= TMP_MARK;
  2651. marked++;
  2652. }
  2653. po=po->next;
  2654. }
  2655. if (i != cnt || cnt+marked != orig_cnt)
  2656. die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
  2657. free_commit_list(h);
  2658. return marked;
  2659. }
  2660. static int mark_treesame_root_parents(struct commit *commit)
  2661. {
  2662. struct commit_list *p;
  2663. int marked = 0;
  2664. for (p = commit->parents; p; p = p->next) {
  2665. struct commit *parent = p->item;
  2666. if (!parent->parents && (parent->object.flags & TREESAME)) {
  2667. parent->object.flags |= TMP_MARK;
  2668. marked++;
  2669. }
  2670. }
  2671. return marked;
  2672. }
  2673. /*
  2674. * Awkward naming - this means one parent we are TREESAME to.
  2675. * cf mark_treesame_root_parents: root parents that are TREESAME (to an
  2676. * empty tree). Better name suggestions?
  2677. */
  2678. static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
  2679. {
  2680. struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
  2681. struct commit *unmarked = NULL, *marked = NULL;
  2682. struct commit_list *p;
  2683. unsigned n;
  2684. for (p = commit->parents, n = 0; p; p = p->next, n++) {
  2685. if (ts->treesame[n]) {
  2686. if (p->item->object.flags & TMP_MARK) {
  2687. if (!marked)
  2688. marked = p->item;
  2689. } else {
  2690. if (!unmarked) {
  2691. unmarked = p->item;
  2692. break;
  2693. }
  2694. }
  2695. }
  2696. }
  2697. /*
  2698. * If we are TREESAME to a marked-for-deletion parent, but not to any
  2699. * unmarked parents, unmark the first TREESAME parent. This is the
  2700. * parent that the default simplify_history==1 scan would have followed,
  2701. * and it doesn't make sense to omit that path when asking for a
  2702. * simplified full history. Retaining it improves the chances of
  2703. * understanding odd missed merges that took an old version of a file.
  2704. *
  2705. * Example:
  2706. *
  2707. * I--------*X A modified the file, but mainline merge X used
  2708. * \ / "-s ours", so took the version from I. X is
  2709. * `-*A--' TREESAME to I and !TREESAME to A.
  2710. *
  2711. * Default log from X would produce "I". Without this check,
  2712. * --full-history --simplify-merges would produce "I-A-X", showing
  2713. * the merge commit X and that it changed A, but not making clear that
  2714. * it had just taken the I version. With this check, the topology above
  2715. * is retained.
  2716. *
  2717. * Note that it is possible that the simplification chooses a different
  2718. * TREESAME parent from the default, in which case this test doesn't
  2719. * activate, and we _do_ drop the default parent. Example:
  2720. *
  2721. * I------X A modified the file, but it was reverted in B,
  2722. * \ / meaning mainline merge X is TREESAME to both
  2723. * *A-*B parents.
  2724. *
  2725. * Default log would produce "I" by following the first parent;
  2726. * --full-history --simplify-merges will produce "I-A-B". But this is a
  2727. * reasonable result - it presents a logical full history leading from
  2728. * I to X, and X is not an important merge.
  2729. */
  2730. if (!unmarked && marked) {
  2731. marked->object.flags &= ~TMP_MARK;
  2732. return 1;
  2733. }
  2734. return 0;
  2735. }
  2736. static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
  2737. {
  2738. struct commit_list **pp, *p;
  2739. int nth_parent, removed = 0;
  2740. pp = &commit->parents;
  2741. nth_parent = 0;
  2742. while ((p = *pp) != NULL) {
  2743. struct commit *parent = p->item;
  2744. if (parent->object.flags & TMP_MARK) {
  2745. parent->object.flags &= ~TMP_MARK;
  2746. *pp = p->next;
  2747. free(p);
  2748. removed++;
  2749. compact_treesame(revs, commit, nth_parent);
  2750. continue;
  2751. }
  2752. pp = &p->next;
  2753. nth_parent++;
  2754. }
  2755. /* Removing parents can only increase TREESAMEness */
  2756. if (removed && !(commit->object.flags & TREESAME))
  2757. update_treesame(revs, commit);
  2758. return nth_parent;
  2759. }
  2760. static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
  2761. {
  2762. struct commit_list *p;
  2763. struct commit *parent;
  2764. struct merge_simplify_state *st, *pst;
  2765. int cnt;
  2766. st = locate_simplify_state(revs, commit);
  2767. /*
  2768. * Have we handled this one?
  2769. */
  2770. if (st->simplified)
  2771. return tail;
  2772. /*
  2773. * An UNINTERESTING commit simplifies to itself, so does a
  2774. * root commit. We do not rewrite parents of such commit
  2775. * anyway.
  2776. */
  2777. if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
  2778. st->simplified = commit;
  2779. return tail;
  2780. }
  2781. /*
  2782. * Do we know what commit all of our parents that matter
  2783. * should be rewritten to? Otherwise we are not ready to
  2784. * rewrite this one yet.
  2785. */
  2786. for (cnt = 0, p = commit->parents; p; p = p->next) {
  2787. pst = locate_simplify_state(revs, p->item);
  2788. if (!pst->simplified) {
  2789. tail = &commit_list_insert(p->item, tail)->next;
  2790. cnt++;
  2791. }
  2792. if (revs->first_parent_only)
  2793. break;
  2794. }
  2795. if (cnt) {
  2796. tail = &commit_list_insert(commit, tail)->next;
  2797. return tail;
  2798. }
  2799. /*
  2800. * Rewrite our list of parents. Note that this cannot
  2801. * affect our TREESAME flags in any way - a commit is
  2802. * always TREESAME to its simplification.
  2803. */
  2804. for (p = commit->parents; p; p = p->next) {
  2805. pst = locate_simplify_state(revs, p->item);
  2806. p->item = pst->simplified;
  2807. if (revs->first_parent_only)
  2808. break;
  2809. }
  2810. if (revs->first_parent_only)
  2811. cnt = 1;
  2812. else
  2813. cnt = remove_duplicate_parents(revs, commit);
  2814. /*
  2815. * It is possible that we are a merge and one side branch
  2816. * does not have any commit that touches the given paths;
  2817. * in such a case, the immediate parent from that branch
  2818. * will be rewritten to be the merge base.
  2819. *
  2820. * o----X X: the commit we are looking at;
  2821. * / / o: a commit that touches the paths;
  2822. * ---o----'
  2823. *
  2824. * Further, a merge of an independent branch that doesn't
  2825. * touch the path will reduce to a treesame root parent:
  2826. *
  2827. * ----o----X X: the commit we are looking at;
  2828. * / o: a commit that touches the paths;
  2829. * r r: a root commit not touching the paths
  2830. *
  2831. * Detect and simplify both cases.
  2832. */
  2833. if (1 < cnt) {
  2834. int marked = mark_redundant_parents(commit);
  2835. marked += mark_treesame_root_parents(commit);
  2836. if (marked)
  2837. marked -= leave_one_treesame_to_parent(revs, commit);
  2838. if (marked)
  2839. cnt = remove_marked_parents(revs, commit);
  2840. }
  2841. /*
  2842. * A commit simplifies to itself if it is a root, if it is
  2843. * UNINTERESTING, if it touches the given paths, or if it is a
  2844. * merge and its parents don't simplify to one relevant commit
  2845. * (the first two cases are already handled at the beginning of
  2846. * this function).
  2847. *
  2848. * Otherwise, it simplifies to what its sole relevant parent
  2849. * simplifies to.
  2850. */
  2851. if (!cnt ||
  2852. (commit->object.flags & UNINTERESTING) ||
  2853. !(commit->object.flags & TREESAME) ||
  2854. (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
  2855. (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
  2856. st->simplified = commit;
  2857. else {
  2858. pst = locate_simplify_state(revs, parent);
  2859. st->simplified = pst->simplified;
  2860. }
  2861. return tail;
  2862. }
  2863. static void simplify_merges(struct rev_info *revs)
  2864. {
  2865. struct commit_list *list, *next;
  2866. struct commit_list *yet_to_do, **tail;
  2867. struct commit *commit;
  2868. if (!revs->prune)
  2869. return;
  2870. /* feed the list reversed */
  2871. yet_to_do = NULL;
  2872. for (list = revs->commits; list; list = next) {
  2873. commit = list->item;
  2874. next = list->next;
  2875. /*
  2876. * Do not free(list) here yet; the original list
  2877. * is used later in this function.
  2878. */
  2879. commit_list_insert(commit, &yet_to_do);
  2880. }
  2881. while (yet_to_do) {
  2882. list = yet_to_do;
  2883. yet_to_do = NULL;
  2884. tail = &yet_to_do;
  2885. while (list) {
  2886. commit = pop_commit(&list);
  2887. tail = simplify_one(revs, commit, tail);
  2888. }
  2889. }
  2890. /* clean up the result, removing the simplified ones */
  2891. list = revs->commits;
  2892. revs->commits = NULL;
  2893. tail = &revs->commits;
  2894. while (list) {
  2895. struct merge_simplify_state *st;
  2896. commit = pop_commit(&list);
  2897. st = locate_simplify_state(revs, commit);
  2898. if (st->simplified == commit)
  2899. tail = &commit_list_insert(commit, tail)->next;
  2900. }
  2901. }
  2902. static void set_children(struct rev_info *revs)
  2903. {
  2904. struct commit_list *l;
  2905. for (l = revs->commits; l; l = l->next) {
  2906. struct commit *commit = l->item;
  2907. struct commit_list *p;
  2908. for (p = commit->parents; p; p = p->next)
  2909. add_child(revs, p->item, commit);
  2910. }
  2911. }
  2912. void reset_revision_walk(void)
  2913. {
  2914. clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
  2915. }
  2916. static int mark_uninteresting(const struct object_id *oid,
  2917. struct packed_git *pack,
  2918. uint32_t pos,
  2919. void *cb)
  2920. {
  2921. struct rev_info *revs = cb;
  2922. struct object *o = parse_object(revs->repo, oid);
  2923. o->flags |= UNINTERESTING | SEEN;
  2924. return 0;
  2925. }
  2926. define_commit_slab(indegree_slab, int);
  2927. define_commit_slab(author_date_slab, timestamp_t);
  2928. struct topo_walk_info {
  2929. uint32_t min_generation;
  2930. struct prio_queue explore_queue;
  2931. struct prio_queue indegree_queue;
  2932. struct prio_queue topo_queue;
  2933. struct indegree_slab indegree;
  2934. struct author_date_slab author_date;
  2935. };
  2936. static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
  2937. {
  2938. if (c->object.flags & flag)
  2939. return;
  2940. c->object.flags |= flag;
  2941. prio_queue_put(q, c);
  2942. }
  2943. static void explore_walk_step(struct rev_info *revs)
  2944. {
  2945. struct topo_walk_info *info = revs->topo_walk_info;
  2946. struct commit_list *p;
  2947. struct commit *c = prio_queue_get(&info->explore_queue);
  2948. if (!c)
  2949. return;
  2950. if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
  2951. return;
  2952. if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
  2953. record_author_date(&info->author_date, c);
  2954. if (revs->max_age != -1 && (c->date < revs->max_age))
  2955. c->object.flags |= UNINTERESTING;
  2956. if (process_parents(revs, c, NULL, NULL) < 0)
  2957. return;
  2958. if (c->object.flags & UNINTERESTING)
  2959. mark_parents_uninteresting(c);
  2960. for (p = c->parents; p; p = p->next)
  2961. test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
  2962. }
  2963. static void explore_to_depth(struct rev_info *revs,
  2964. uint32_t gen_cutoff)
  2965. {
  2966. struct topo_walk_info *info = revs->topo_walk_info;
  2967. struct commit *c;
  2968. while ((c = prio_queue_peek(&info->explore_queue)) &&
  2969. commit_graph_generation(c) >= gen_cutoff)
  2970. explore_walk_step(revs);
  2971. }
  2972. static void indegree_walk_step(struct rev_info *revs)
  2973. {
  2974. struct commit_list *p;
  2975. struct topo_walk_info *info = revs->topo_walk_info;
  2976. struct commit *c = prio_queue_get(&info->indegree_queue);
  2977. if (!c)
  2978. return;
  2979. if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
  2980. return;
  2981. explore_to_depth(revs, commit_graph_generation(c));
  2982. for (p = c->parents; p; p = p->next) {
  2983. struct commit *parent = p->item;
  2984. int *pi = indegree_slab_at(&info->indegree, parent);
  2985. if (*pi)
  2986. (*pi)++;
  2987. else
  2988. *pi = 2;
  2989. test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
  2990. if (revs->first_parent_only)
  2991. return;
  2992. }
  2993. }
  2994. static void compute_indegrees_to_depth(struct rev_info *revs,
  2995. uint32_t gen_cutoff)
  2996. {
  2997. struct topo_walk_info *info = revs->topo_walk_info;
  2998. struct commit *c;
  2999. while ((c = prio_queue_peek(&info->indegree_queue)) &&
  3000. commit_graph_generation(c) >= gen_cutoff)
  3001. indegree_walk_step(revs);
  3002. }
  3003. static void reset_topo_walk(struct rev_info *revs)
  3004. {
  3005. struct topo_walk_info *info = revs->topo_walk_info;
  3006. clear_prio_queue(&info->explore_queue);
  3007. clear_prio_queue(&info->indegree_queue);
  3008. clear_prio_queue(&info->topo_queue);
  3009. clear_indegree_slab(&info->indegree);
  3010. clear_author_date_slab(&info->author_date);
  3011. FREE_AND_NULL(revs->topo_walk_info);
  3012. }
  3013. static void init_topo_walk(struct rev_info *revs)
  3014. {
  3015. struct topo_walk_info *info;
  3016. struct commit_list *list;
  3017. if (revs->topo_walk_info)
  3018. reset_topo_walk(revs);
  3019. revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
  3020. info = revs->topo_walk_info;
  3021. memset(info, 0, sizeof(struct topo_walk_info));
  3022. init_indegree_slab(&info->indegree);
  3023. memset(&info->explore_queue, 0, sizeof(info->explore_queue));
  3024. memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
  3025. memset(&info->topo_queue, 0, sizeof(info->topo_queue));
  3026. switch (revs->sort_order) {
  3027. default: /* REV_SORT_IN_GRAPH_ORDER */
  3028. info->topo_queue.compare = NULL;
  3029. break;
  3030. case REV_SORT_BY_COMMIT_DATE:
  3031. info->topo_queue.compare = compare_commits_by_commit_date;
  3032. break;
  3033. case REV_SORT_BY_AUTHOR_DATE:
  3034. init_author_date_slab(&info->author_date);
  3035. info->topo_queue.compare = compare_commits_by_author_date;
  3036. info->topo_queue.cb_data = &info->author_date;
  3037. break;
  3038. }
  3039. info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
  3040. info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
  3041. info->min_generation = GENERATION_NUMBER_INFINITY;
  3042. for (list = revs->commits; list; list = list->next) {
  3043. struct commit *c = list->item;
  3044. uint32_t generation;
  3045. if (repo_parse_commit_gently(revs->repo, c, 1))
  3046. continue;
  3047. test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
  3048. test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
  3049. generation = commit_graph_generation(c);
  3050. if (generation < info->min_generation)
  3051. info->min_generation = generation;
  3052. *(indegree_slab_at(&info->indegree, c)) = 1;
  3053. if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
  3054. record_author_date(&info->author_date, c);
  3055. }
  3056. compute_indegrees_to_depth(revs, info->min_generation);
  3057. for (list = revs->commits; list; list = list->next) {
  3058. struct commit *c = list->item;
  3059. if (*(indegree_slab_at(&info->indegree, c)) == 1)
  3060. prio_queue_put(&info->topo_queue, c);
  3061. }
  3062. /*
  3063. * This is unfortunate; the initial tips need to be shown
  3064. * in the order given from the revision traversal machinery.
  3065. */
  3066. if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
  3067. prio_queue_reverse(&info->topo_queue);
  3068. }
  3069. static struct commit *next_topo_commit(struct rev_info *revs)
  3070. {
  3071. struct commit *c;
  3072. struct topo_walk_info *info = revs->topo_walk_info;
  3073. /* pop next off of topo_queue */
  3074. c = prio_queue_get(&info->topo_queue);
  3075. if (c)
  3076. *(indegree_slab_at(&info->indegree, c)) = 0;
  3077. return c;
  3078. }
  3079. static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
  3080. {
  3081. struct commit_list *p;
  3082. struct topo_walk_info *info = revs->topo_walk_info;
  3083. if (process_parents(revs, commit, NULL, NULL) < 0) {
  3084. if (!revs->ignore_missing_links)
  3085. die("Failed to traverse parents of commit %s",
  3086. oid_to_hex(&commit->object.oid));
  3087. }
  3088. for (p = commit->parents; p; p = p->next) {
  3089. struct commit *parent = p->item;
  3090. int *pi;
  3091. uint32_t generation;
  3092. if (parent->object.flags & UNINTERESTING)
  3093. continue;
  3094. if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
  3095. continue;
  3096. generation = commit_graph_generation(parent);
  3097. if (generation < info->min_generation) {
  3098. info->min_generation = generation;
  3099. compute_indegrees_to_depth(revs, info->min_generation);
  3100. }
  3101. pi = indegree_slab_at(&info->indegree, parent);
  3102. (*pi)--;
  3103. if (*pi == 1)
  3104. prio_queue_put(&info->topo_queue, parent);
  3105. if (revs->first_parent_only)
  3106. return;
  3107. }
  3108. }
  3109. int prepare_revision_walk(struct rev_info *revs)
  3110. {
  3111. int i;
  3112. struct object_array old_pending;
  3113. struct commit_list **next = &revs->commits;
  3114. memcpy(&old_pending, &revs->pending, sizeof(old_pending));
  3115. revs->pending.nr = 0;
  3116. revs->pending.alloc = 0;
  3117. revs->pending.objects = NULL;
  3118. for (i = 0; i < old_pending.nr; i++) {
  3119. struct object_array_entry *e = old_pending.objects + i;
  3120. struct commit *commit = handle_commit(revs, e);
  3121. if (commit) {
  3122. if (!(commit->object.flags & SEEN)) {
  3123. commit->object.flags |= SEEN;
  3124. next = commit_list_append(commit, next);
  3125. }
  3126. }
  3127. }
  3128. object_array_clear(&old_pending);
  3129. /* Signal whether we need per-parent treesame decoration */
  3130. if (revs->simplify_merges ||
  3131. (revs->limited && limiting_can_increase_treesame(revs)))
  3132. revs->treesame.name = "treesame";
  3133. if (revs->exclude_promisor_objects) {
  3134. for_each_packed_object(mark_uninteresting, revs,
  3135. FOR_EACH_OBJECT_PROMISOR_ONLY);
  3136. }
  3137. if (!revs->reflog_info)
  3138. prepare_to_use_bloom_filter(revs);
  3139. if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
  3140. commit_list_sort_by_date(&revs->commits);
  3141. if (revs->no_walk)
  3142. return 0;
  3143. if (revs->limited) {
  3144. if (limit_list(revs) < 0)
  3145. return -1;
  3146. if (revs->topo_order)
  3147. sort_in_topological_order(&revs->commits, revs->sort_order);
  3148. } else if (revs->topo_order)
  3149. init_topo_walk(revs);
  3150. if (revs->line_level_traverse && want_ancestry(revs))
  3151. /*
  3152. * At the moment we can only do line-level log with parent
  3153. * rewriting by performing this expensive pre-filtering step.
  3154. * If parent rewriting is not requested, then we rather
  3155. * perform the line-level log filtering during the regular
  3156. * history traversal.
  3157. */
  3158. line_log_filter(revs);
  3159. if (revs->simplify_merges)
  3160. simplify_merges(revs);
  3161. if (revs->children.name)
  3162. set_children(revs);
  3163. return 0;
  3164. }
  3165. static enum rewrite_result rewrite_one_1(struct rev_info *revs,
  3166. struct commit **pp,
  3167. struct prio_queue *queue)
  3168. {
  3169. for (;;) {
  3170. struct commit *p = *pp;
  3171. if (!revs->limited)
  3172. if (process_parents(revs, p, NULL, queue) < 0)
  3173. return rewrite_one_error;
  3174. if (p->object.flags & UNINTERESTING)
  3175. return rewrite_one_ok;
  3176. if (!(p->object.flags & TREESAME))
  3177. return rewrite_one_ok;
  3178. if (!p->parents)
  3179. return rewrite_one_noparents;
  3180. if ((p = one_relevant_parent(revs, p->parents)) == NULL)
  3181. return rewrite_one_ok;
  3182. *pp = p;
  3183. }
  3184. }
  3185. static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
  3186. {
  3187. while (q->nr) {
  3188. struct commit *item = prio_queue_peek(q);
  3189. struct commit_list *p = *list;
  3190. if (p && p->item->date >= item->date)
  3191. list = &p->next;
  3192. else {
  3193. p = commit_list_insert(item, list);
  3194. list = &p->next; /* skip newly added item */
  3195. prio_queue_get(q); /* pop item */
  3196. }
  3197. }
  3198. }
  3199. static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
  3200. {
  3201. struct prio_queue queue = { compare_commits_by_commit_date };
  3202. enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
  3203. merge_queue_into_list(&queue, &revs->commits);
  3204. clear_prio_queue(&queue);
  3205. return ret;
  3206. }
  3207. int rewrite_parents(struct rev_info *revs, struct commit *commit,
  3208. rewrite_parent_fn_t rewrite_parent)
  3209. {
  3210. struct commit_list **pp = &commit->parents;
  3211. while (*pp) {
  3212. struct commit_list *parent = *pp;
  3213. switch (rewrite_parent(revs, &parent->item)) {
  3214. case rewrite_one_ok:
  3215. break;
  3216. case rewrite_one_noparents:
  3217. *pp = parent->next;
  3218. continue;
  3219. case rewrite_one_error:
  3220. return -1;
  3221. }
  3222. pp = &parent->next;
  3223. }
  3224. remove_duplicate_parents(revs, commit);
  3225. return 0;
  3226. }
  3227. static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
  3228. {
  3229. char *person, *endp;
  3230. size_t len, namelen, maillen;
  3231. const char *name;
  3232. const char *mail;
  3233. struct ident_split ident;
  3234. person = strstr(buf->buf, what);
  3235. if (!person)
  3236. return 0;
  3237. person += strlen(what);
  3238. endp = strchr(person, '\n');
  3239. if (!endp)
  3240. return 0;
  3241. len = endp - person;
  3242. if (split_ident_line(&ident, person, len))
  3243. return 0;
  3244. mail = ident.mail_begin;
  3245. maillen = ident.mail_end - ident.mail_begin;
  3246. name = ident.name_begin;
  3247. namelen = ident.name_end - ident.name_begin;
  3248. if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
  3249. struct strbuf namemail = STRBUF_INIT;
  3250. strbuf_addf(&namemail, "%.*s <%.*s>",
  3251. (int)namelen, name, (int)maillen, mail);
  3252. strbuf_splice(buf, ident.name_begin - buf->buf,
  3253. ident.mail_end - ident.name_begin + 1,
  3254. namemail.buf, namemail.len);
  3255. strbuf_release(&namemail);
  3256. return 1;
  3257. }
  3258. return 0;
  3259. }
  3260. static int commit_match(struct commit *commit, struct rev_info *opt)
  3261. {
  3262. int retval;
  3263. const char *encoding;
  3264. const char *message;
  3265. struct strbuf buf = STRBUF_INIT;
  3266. if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
  3267. return 1;
  3268. /* Prepend "fake" headers as needed */
  3269. if (opt->grep_filter.use_reflog_filter) {
  3270. strbuf_addstr(&buf, "reflog ");
  3271. get_reflog_message(&buf, opt->reflog_info);
  3272. strbuf_addch(&buf, '\n');
  3273. }
  3274. /*
  3275. * We grep in the user's output encoding, under the assumption that it
  3276. * is the encoding they are most likely to write their grep pattern
  3277. * for. In addition, it means we will match the "notes" encoding below,
  3278. * so we will not end up with a buffer that has two different encodings
  3279. * in it.
  3280. */
  3281. encoding = get_log_output_encoding();
  3282. message = logmsg_reencode(commit, NULL, encoding);
  3283. /* Copy the commit to temporary if we are using "fake" headers */
  3284. if (buf.len)
  3285. strbuf_addstr(&buf, message);
  3286. if (opt->grep_filter.header_list && opt->mailmap) {
  3287. if (!buf.len)
  3288. strbuf_addstr(&buf, message);
  3289. commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
  3290. commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
  3291. }
  3292. /* Append "fake" message parts as needed */
  3293. if (opt->show_notes) {
  3294. if (!buf.len)
  3295. strbuf_addstr(&buf, message);
  3296. format_display_notes(&commit->object.oid, &buf, encoding, 1);
  3297. }
  3298. /*
  3299. * Find either in the original commit message, or in the temporary.
  3300. * Note that we cast away the constness of "message" here. It is
  3301. * const because it may come from the cached commit buffer. That's OK,
  3302. * because we know that it is modifiable heap memory, and that while
  3303. * grep_buffer may modify it for speed, it will restore any
  3304. * changes before returning.
  3305. */
  3306. if (buf.len)
  3307. retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
  3308. else
  3309. retval = grep_buffer(&opt->grep_filter,
  3310. (char *)message, strlen(message));
  3311. strbuf_release(&buf);
  3312. unuse_commit_buffer(commit, message);
  3313. return opt->invert_grep ? !retval : retval;
  3314. }
  3315. static inline int want_ancestry(const struct rev_info *revs)
  3316. {
  3317. return (revs->rewrite_parents || revs->children.name);
  3318. }
  3319. /*
  3320. * Return a timestamp to be used for --since/--until comparisons for this
  3321. * commit, based on the revision options.
  3322. */
  3323. static timestamp_t comparison_date(const struct rev_info *revs,
  3324. struct commit *commit)
  3325. {
  3326. return revs->reflog_info ?
  3327. get_reflog_timestamp(revs->reflog_info) :
  3328. commit->date;
  3329. }
  3330. enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
  3331. {
  3332. if (commit->object.flags & SHOWN)
  3333. return commit_ignore;
  3334. if (revs->unpacked && has_object_pack(&commit->object.oid))
  3335. return commit_ignore;
  3336. if (commit->object.flags & UNINTERESTING)
  3337. return commit_ignore;
  3338. if (revs->line_level_traverse && !want_ancestry(revs)) {
  3339. /*
  3340. * In case of line-level log with parent rewriting
  3341. * prepare_revision_walk() already took care of all line-level
  3342. * log filtering, and there is nothing left to do here.
  3343. *
  3344. * If parent rewriting was not requested, then this is the
  3345. * place to perform the line-level log filtering. Notably,
  3346. * this check, though expensive, must come before the other,
  3347. * cheaper filtering conditions, because the tracked line
  3348. * ranges must be adjusted even when the commit will end up
  3349. * being ignored based on other conditions.
  3350. */
  3351. if (!line_log_process_ranges_arbitrary_commit(revs, commit))
  3352. return commit_ignore;
  3353. }
  3354. if (revs->min_age != -1 &&
  3355. comparison_date(revs, commit) > revs->min_age)
  3356. return commit_ignore;
  3357. if (revs->min_parents || (revs->max_parents >= 0)) {
  3358. int n = commit_list_count(commit->parents);
  3359. if ((n < revs->min_parents) ||
  3360. ((revs->max_parents >= 0) && (n > revs->max_parents)))
  3361. return commit_ignore;
  3362. }
  3363. if (!commit_match(commit, revs))
  3364. return commit_ignore;
  3365. if (revs->prune && revs->dense) {
  3366. /* Commit without changes? */
  3367. if (commit->object.flags & TREESAME) {
  3368. int n;
  3369. struct commit_list *p;
  3370. /* drop merges unless we want parenthood */
  3371. if (!want_ancestry(revs))
  3372. return commit_ignore;
  3373. if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
  3374. return commit_show;
  3375. /*
  3376. * If we want ancestry, then need to keep any merges
  3377. * between relevant commits to tie together topology.
  3378. * For consistency with TREESAME and simplification
  3379. * use "relevant" here rather than just INTERESTING,
  3380. * to treat bottom commit(s) as part of the topology.
  3381. */
  3382. for (n = 0, p = commit->parents; p; p = p->next)
  3383. if (relevant_commit(p->item))
  3384. if (++n >= 2)
  3385. return commit_show;
  3386. return commit_ignore;
  3387. }
  3388. }
  3389. return commit_show;
  3390. }
  3391. define_commit_slab(saved_parents, struct commit_list *);
  3392. #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
  3393. /*
  3394. * You may only call save_parents() once per commit (this is checked
  3395. * for non-root commits).
  3396. */
  3397. static void save_parents(struct rev_info *revs, struct commit *commit)
  3398. {
  3399. struct commit_list **pp;
  3400. if (!revs->saved_parents_slab) {
  3401. revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
  3402. init_saved_parents(revs->saved_parents_slab);
  3403. }
  3404. pp = saved_parents_at(revs->saved_parents_slab, commit);
  3405. /*
  3406. * When walking with reflogs, we may visit the same commit
  3407. * several times: once for each appearance in the reflog.
  3408. *
  3409. * In this case, save_parents() will be called multiple times.
  3410. * We want to keep only the first set of parents. We need to
  3411. * store a sentinel value for an empty (i.e., NULL) parent
  3412. * list to distinguish it from a not-yet-saved list, however.
  3413. */
  3414. if (*pp)
  3415. return;
  3416. if (commit->parents)
  3417. *pp = copy_commit_list(commit->parents);
  3418. else
  3419. *pp = EMPTY_PARENT_LIST;
  3420. }
  3421. static void free_saved_parents(struct rev_info *revs)
  3422. {
  3423. if (revs->saved_parents_slab)
  3424. clear_saved_parents(revs->saved_parents_slab);
  3425. }
  3426. struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
  3427. {
  3428. struct commit_list *parents;
  3429. if (!revs->saved_parents_slab)
  3430. return commit->parents;
  3431. parents = *saved_parents_at(revs->saved_parents_slab, commit);
  3432. if (parents == EMPTY_PARENT_LIST)
  3433. return NULL;
  3434. return parents;
  3435. }
  3436. enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
  3437. {
  3438. enum commit_action action = get_commit_action(revs, commit);
  3439. if (action == commit_show &&
  3440. revs->prune && revs->dense && want_ancestry(revs)) {
  3441. /*
  3442. * --full-diff on simplified parents is no good: it
  3443. * will show spurious changes from the commits that
  3444. * were elided. So we save the parents on the side
  3445. * when --full-diff is in effect.
  3446. */
  3447. if (revs->full_diff)
  3448. save_parents(revs, commit);
  3449. if (rewrite_parents(revs, commit, rewrite_one) < 0)
  3450. return commit_error;
  3451. }
  3452. return action;
  3453. }
  3454. static void track_linear(struct rev_info *revs, struct commit *commit)
  3455. {
  3456. if (revs->track_first_time) {
  3457. revs->linear = 1;
  3458. revs->track_first_time = 0;
  3459. } else {
  3460. struct commit_list *p;
  3461. for (p = revs->previous_parents; p; p = p->next)
  3462. if (p->item == NULL || /* first commit */
  3463. oideq(&p->item->object.oid, &commit->object.oid))
  3464. break;
  3465. revs->linear = p != NULL;
  3466. }
  3467. if (revs->reverse) {
  3468. if (revs->linear)
  3469. commit->object.flags |= TRACK_LINEAR;
  3470. }
  3471. free_commit_list(revs->previous_parents);
  3472. revs->previous_parents = copy_commit_list(commit->parents);
  3473. }
  3474. static struct commit *get_revision_1(struct rev_info *revs)
  3475. {
  3476. while (1) {
  3477. struct commit *commit;
  3478. if (revs->reflog_info)
  3479. commit = next_reflog_entry(revs->reflog_info);
  3480. else if (revs->topo_walk_info)
  3481. commit = next_topo_commit(revs);
  3482. else
  3483. commit = pop_commit(&revs->commits);
  3484. if (!commit)
  3485. return NULL;
  3486. if (revs->reflog_info)
  3487. commit->object.flags &= ~(ADDED | SEEN | SHOWN);
  3488. /*
  3489. * If we haven't done the list limiting, we need to look at
  3490. * the parents here. We also need to do the date-based limiting
  3491. * that we'd otherwise have done in limit_list().
  3492. */
  3493. if (!revs->limited) {
  3494. if (revs->max_age != -1 &&
  3495. comparison_date(revs, commit) < revs->max_age)
  3496. continue;
  3497. if (revs->reflog_info)
  3498. try_to_simplify_commit(revs, commit);
  3499. else if (revs->topo_walk_info)
  3500. expand_topo_walk(revs, commit);
  3501. else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
  3502. if (!revs->ignore_missing_links)
  3503. die("Failed to traverse parents of commit %s",
  3504. oid_to_hex(&commit->object.oid));
  3505. }
  3506. }
  3507. switch (simplify_commit(revs, commit)) {
  3508. case commit_ignore:
  3509. continue;
  3510. case commit_error:
  3511. die("Failed to simplify parents of commit %s",
  3512. oid_to_hex(&commit->object.oid));
  3513. default:
  3514. if (revs->track_linear)
  3515. track_linear(revs, commit);
  3516. return commit;
  3517. }
  3518. }
  3519. }
  3520. /*
  3521. * Return true for entries that have not yet been shown. (This is an
  3522. * object_array_each_func_t.)
  3523. */
  3524. static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
  3525. {
  3526. return !(entry->item->flags & SHOWN);
  3527. }
  3528. /*
  3529. * If array is on the verge of a realloc, garbage-collect any entries
  3530. * that have already been shown to try to free up some space.
  3531. */
  3532. static void gc_boundary(struct object_array *array)
  3533. {
  3534. if (array->nr == array->alloc)
  3535. object_array_filter(array, entry_unshown, NULL);
  3536. }
  3537. static void create_boundary_commit_list(struct rev_info *revs)
  3538. {
  3539. unsigned i;
  3540. struct commit *c;
  3541. struct object_array *array = &revs->boundary_commits;
  3542. struct object_array_entry *objects = array->objects;
  3543. /*
  3544. * If revs->commits is non-NULL at this point, an error occurred in
  3545. * get_revision_1(). Ignore the error and continue printing the
  3546. * boundary commits anyway. (This is what the code has always
  3547. * done.)
  3548. */
  3549. if (revs->commits) {
  3550. free_commit_list(revs->commits);
  3551. revs->commits = NULL;
  3552. }
  3553. /*
  3554. * Put all of the actual boundary commits from revs->boundary_commits
  3555. * into revs->commits
  3556. */
  3557. for (i = 0; i < array->nr; i++) {
  3558. c = (struct commit *)(objects[i].item);
  3559. if (!c)
  3560. continue;
  3561. if (!(c->object.flags & CHILD_SHOWN))
  3562. continue;
  3563. if (c->object.flags & (SHOWN | BOUNDARY))
  3564. continue;
  3565. c->object.flags |= BOUNDARY;
  3566. commit_list_insert(c, &revs->commits);
  3567. }
  3568. /*
  3569. * If revs->topo_order is set, sort the boundary commits
  3570. * in topological order
  3571. */
  3572. sort_in_topological_order(&revs->commits, revs->sort_order);
  3573. }
  3574. static struct commit *get_revision_internal(struct rev_info *revs)
  3575. {
  3576. struct commit *c = NULL;
  3577. struct commit_list *l;
  3578. if (revs->boundary == 2) {
  3579. /*
  3580. * All of the normal commits have already been returned,
  3581. * and we are now returning boundary commits.
  3582. * create_boundary_commit_list() has populated
  3583. * revs->commits with the remaining commits to return.
  3584. */
  3585. c = pop_commit(&revs->commits);
  3586. if (c)
  3587. c->object.flags |= SHOWN;
  3588. return c;
  3589. }
  3590. /*
  3591. * If our max_count counter has reached zero, then we are done. We
  3592. * don't simply return NULL because we still might need to show
  3593. * boundary commits. But we want to avoid calling get_revision_1, which
  3594. * might do a considerable amount of work finding the next commit only
  3595. * for us to throw it away.
  3596. *
  3597. * If it is non-zero, then either we don't have a max_count at all
  3598. * (-1), or it is still counting, in which case we decrement.
  3599. */
  3600. if (revs->max_count) {
  3601. c = get_revision_1(revs);
  3602. if (c) {
  3603. while (revs->skip_count > 0) {
  3604. revs->skip_count--;
  3605. c = get_revision_1(revs);
  3606. if (!c)
  3607. break;
  3608. }
  3609. }
  3610. if (revs->max_count > 0)
  3611. revs->max_count--;
  3612. }
  3613. if (c)
  3614. c->object.flags |= SHOWN;
  3615. if (!revs->boundary)
  3616. return c;
  3617. if (!c) {
  3618. /*
  3619. * get_revision_1() runs out the commits, and
  3620. * we are done computing the boundaries.
  3621. * switch to boundary commits output mode.
  3622. */
  3623. revs->boundary = 2;
  3624. /*
  3625. * Update revs->commits to contain the list of
  3626. * boundary commits.
  3627. */
  3628. create_boundary_commit_list(revs);
  3629. return get_revision_internal(revs);
  3630. }
  3631. /*
  3632. * boundary commits are the commits that are parents of the
  3633. * ones we got from get_revision_1() but they themselves are
  3634. * not returned from get_revision_1(). Before returning
  3635. * 'c', we need to mark its parents that they could be boundaries.
  3636. */
  3637. for (l = c->parents; l; l = l->next) {
  3638. struct object *p;
  3639. p = &(l->item->object);
  3640. if (p->flags & (CHILD_SHOWN | SHOWN))
  3641. continue;
  3642. p->flags |= CHILD_SHOWN;
  3643. gc_boundary(&revs->boundary_commits);
  3644. add_object_array(p, NULL, &revs->boundary_commits);
  3645. }
  3646. return c;
  3647. }
  3648. struct commit *get_revision(struct rev_info *revs)
  3649. {
  3650. struct commit *c;
  3651. struct commit_list *reversed;
  3652. if (revs->reverse) {
  3653. reversed = NULL;
  3654. while ((c = get_revision_internal(revs)))
  3655. commit_list_insert(c, &reversed);
  3656. revs->commits = reversed;
  3657. revs->reverse = 0;
  3658. revs->reverse_output_stage = 1;
  3659. }
  3660. if (revs->reverse_output_stage) {
  3661. c = pop_commit(&revs->commits);
  3662. if (revs->track_linear)
  3663. revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
  3664. return c;
  3665. }
  3666. c = get_revision_internal(revs);
  3667. if (c && revs->graph)
  3668. graph_update(revs->graph, c);
  3669. if (!c) {
  3670. free_saved_parents(revs);
  3671. if (revs->previous_parents) {
  3672. free_commit_list(revs->previous_parents);
  3673. revs->previous_parents = NULL;
  3674. }
  3675. }
  3676. return c;
  3677. }
  3678. const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
  3679. {
  3680. if (commit->object.flags & BOUNDARY)
  3681. return "-";
  3682. else if (commit->object.flags & UNINTERESTING)
  3683. return "^";
  3684. else if (commit->object.flags & PATCHSAME)
  3685. return "=";
  3686. else if (!revs || revs->left_right) {
  3687. if (commit->object.flags & SYMMETRIC_LEFT)
  3688. return "<";
  3689. else
  3690. return ">";
  3691. } else if (revs->graph)
  3692. return "*";
  3693. else if (revs->cherry_mark)
  3694. return "+";
  3695. return "";
  3696. }
  3697. void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
  3698. {
  3699. const char *mark = get_revision_mark(revs, commit);
  3700. if (!strlen(mark))
  3701. return;
  3702. fputs(mark, stdout);
  3703. putchar(' ');
  3704. }