You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

revision.c 107KB

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