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 108KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970
  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. struct add_alternate_refs_data {
  1359. struct rev_info *revs;
  1360. unsigned int flags;
  1361. };
  1362. static void add_one_alternate_ref(const struct object_id *oid,
  1363. void *vdata)
  1364. {
  1365. const char *name = ".alternate";
  1366. struct add_alternate_refs_data *data = vdata;
  1367. struct object *obj;
  1368. obj = get_reference(data->revs, name, oid, data->flags);
  1369. add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
  1370. add_pending_object(data->revs, obj, name);
  1371. }
  1372. static void add_alternate_refs_to_pending(struct rev_info *revs,
  1373. unsigned int flags)
  1374. {
  1375. struct add_alternate_refs_data data;
  1376. data.revs = revs;
  1377. data.flags = flags;
  1378. for_each_alternate_ref(add_one_alternate_ref, &data);
  1379. }
  1380. static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
  1381. int exclude_parent)
  1382. {
  1383. struct object_id oid;
  1384. struct object *it;
  1385. struct commit *commit;
  1386. struct commit_list *parents;
  1387. int parent_number;
  1388. const char *arg = arg_;
  1389. if (*arg == '^') {
  1390. flags ^= UNINTERESTING | BOTTOM;
  1391. arg++;
  1392. }
  1393. if (get_oid_committish(arg, &oid))
  1394. return 0;
  1395. while (1) {
  1396. it = get_reference(revs, arg, &oid, 0);
  1397. if (!it && revs->ignore_missing)
  1398. return 0;
  1399. if (it->type != OBJ_TAG)
  1400. break;
  1401. if (!((struct tag*)it)->tagged)
  1402. return 0;
  1403. oidcpy(&oid, &((struct tag*)it)->tagged->oid);
  1404. }
  1405. if (it->type != OBJ_COMMIT)
  1406. return 0;
  1407. commit = (struct commit *)it;
  1408. if (exclude_parent &&
  1409. exclude_parent > commit_list_count(commit->parents))
  1410. return 0;
  1411. for (parents = commit->parents, parent_number = 1;
  1412. parents;
  1413. parents = parents->next, parent_number++) {
  1414. if (exclude_parent && parent_number != exclude_parent)
  1415. continue;
  1416. it = &parents->item->object;
  1417. it->flags |= flags;
  1418. add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
  1419. add_pending_object(revs, it, arg);
  1420. }
  1421. return 1;
  1422. }
  1423. void repo_init_revisions(struct repository *r,
  1424. struct rev_info *revs,
  1425. const char *prefix)
  1426. {
  1427. memset(revs, 0, sizeof(*revs));
  1428. revs->repo = r;
  1429. revs->abbrev = DEFAULT_ABBREV;
  1430. revs->ignore_merges = 1;
  1431. revs->simplify_history = 1;
  1432. revs->pruning.repo = r;
  1433. revs->pruning.flags.recursive = 1;
  1434. revs->pruning.flags.quick = 1;
  1435. revs->pruning.add_remove = file_add_remove;
  1436. revs->pruning.change = file_change;
  1437. revs->pruning.change_fn_data = revs;
  1438. revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
  1439. revs->dense = 1;
  1440. revs->prefix = prefix;
  1441. revs->max_age = -1;
  1442. revs->min_age = -1;
  1443. revs->skip_count = -1;
  1444. revs->max_count = -1;
  1445. revs->max_parents = -1;
  1446. revs->expand_tabs_in_log = -1;
  1447. revs->commit_format = CMIT_FMT_DEFAULT;
  1448. revs->expand_tabs_in_log_default = 8;
  1449. init_grep_defaults(revs->repo);
  1450. grep_init(&revs->grep_filter, revs->repo, prefix);
  1451. revs->grep_filter.status_only = 1;
  1452. repo_diff_setup(revs->repo, &revs->diffopt);
  1453. if (prefix && !revs->diffopt.prefix) {
  1454. revs->diffopt.prefix = prefix;
  1455. revs->diffopt.prefix_length = strlen(prefix);
  1456. }
  1457. revs->notes_opt.use_default_notes = -1;
  1458. }
  1459. static void add_pending_commit_list(struct rev_info *revs,
  1460. struct commit_list *commit_list,
  1461. unsigned int flags)
  1462. {
  1463. while (commit_list) {
  1464. struct object *object = &commit_list->item->object;
  1465. object->flags |= flags;
  1466. add_pending_object(revs, object, oid_to_hex(&object->oid));
  1467. commit_list = commit_list->next;
  1468. }
  1469. }
  1470. static void prepare_show_merge(struct rev_info *revs)
  1471. {
  1472. struct commit_list *bases;
  1473. struct commit *head, *other;
  1474. struct object_id oid;
  1475. const char **prune = NULL;
  1476. int i, prune_num = 1; /* counting terminating NULL */
  1477. struct index_state *istate = revs->repo->index;
  1478. if (get_oid("HEAD", &oid))
  1479. die("--merge without HEAD?");
  1480. head = lookup_commit_or_die(&oid, "HEAD");
  1481. if (get_oid("MERGE_HEAD", &oid))
  1482. die("--merge without MERGE_HEAD?");
  1483. other = lookup_commit_or_die(&oid, "MERGE_HEAD");
  1484. add_pending_object(revs, &head->object, "HEAD");
  1485. add_pending_object(revs, &other->object, "MERGE_HEAD");
  1486. bases = get_merge_bases(head, other);
  1487. add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
  1488. add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
  1489. free_commit_list(bases);
  1490. head->object.flags |= SYMMETRIC_LEFT;
  1491. if (!istate->cache_nr)
  1492. repo_read_index(revs->repo);
  1493. for (i = 0; i < istate->cache_nr; i++) {
  1494. const struct cache_entry *ce = istate->cache[i];
  1495. if (!ce_stage(ce))
  1496. continue;
  1497. if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
  1498. prune_num++;
  1499. REALLOC_ARRAY(prune, prune_num);
  1500. prune[prune_num-2] = ce->name;
  1501. prune[prune_num-1] = NULL;
  1502. }
  1503. while ((i+1 < istate->cache_nr) &&
  1504. ce_same_name(ce, istate->cache[i+1]))
  1505. i++;
  1506. }
  1507. clear_pathspec(&revs->prune_data);
  1508. parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
  1509. PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
  1510. revs->limited = 1;
  1511. }
  1512. static int dotdot_missing(const char *arg, char *dotdot,
  1513. struct rev_info *revs, int symmetric)
  1514. {
  1515. if (revs->ignore_missing)
  1516. return 0;
  1517. /* de-munge so we report the full argument */
  1518. *dotdot = '.';
  1519. die(symmetric
  1520. ? "Invalid symmetric difference expression %s"
  1521. : "Invalid revision range %s", arg);
  1522. }
  1523. static int handle_dotdot_1(const char *arg, char *dotdot,
  1524. struct rev_info *revs, int flags,
  1525. int cant_be_filename,
  1526. struct object_context *a_oc,
  1527. struct object_context *b_oc)
  1528. {
  1529. const char *a_name, *b_name;
  1530. struct object_id a_oid, b_oid;
  1531. struct object *a_obj, *b_obj;
  1532. unsigned int a_flags, b_flags;
  1533. int symmetric = 0;
  1534. unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
  1535. unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
  1536. a_name = arg;
  1537. if (!*a_name)
  1538. a_name = "HEAD";
  1539. b_name = dotdot + 2;
  1540. if (*b_name == '.') {
  1541. symmetric = 1;
  1542. b_name++;
  1543. }
  1544. if (!*b_name)
  1545. b_name = "HEAD";
  1546. if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
  1547. get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
  1548. return -1;
  1549. if (!cant_be_filename) {
  1550. *dotdot = '.';
  1551. verify_non_filename(revs->prefix, arg);
  1552. *dotdot = '\0';
  1553. }
  1554. a_obj = parse_object(revs->repo, &a_oid);
  1555. b_obj = parse_object(revs->repo, &b_oid);
  1556. if (!a_obj || !b_obj)
  1557. return dotdot_missing(arg, dotdot, revs, symmetric);
  1558. if (!symmetric) {
  1559. /* just A..B */
  1560. b_flags = flags;
  1561. a_flags = flags_exclude;
  1562. } else {
  1563. /* A...B -- find merge bases between the two */
  1564. struct commit *a, *b;
  1565. struct commit_list *exclude;
  1566. a = lookup_commit_reference(revs->repo, &a_obj->oid);
  1567. b = lookup_commit_reference(revs->repo, &b_obj->oid);
  1568. if (!a || !b)
  1569. return dotdot_missing(arg, dotdot, revs, symmetric);
  1570. exclude = get_merge_bases(a, b);
  1571. add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
  1572. flags_exclude);
  1573. add_pending_commit_list(revs, exclude, flags_exclude);
  1574. free_commit_list(exclude);
  1575. b_flags = flags;
  1576. a_flags = flags | SYMMETRIC_LEFT;
  1577. }
  1578. a_obj->flags |= a_flags;
  1579. b_obj->flags |= b_flags;
  1580. add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
  1581. add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
  1582. add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
  1583. add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
  1584. return 0;
  1585. }
  1586. static int handle_dotdot(const char *arg,
  1587. struct rev_info *revs, int flags,
  1588. int cant_be_filename)
  1589. {
  1590. struct object_context a_oc, b_oc;
  1591. char *dotdot = strstr(arg, "..");
  1592. int ret;
  1593. if (!dotdot)
  1594. return -1;
  1595. memset(&a_oc, 0, sizeof(a_oc));
  1596. memset(&b_oc, 0, sizeof(b_oc));
  1597. *dotdot = '\0';
  1598. ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
  1599. &a_oc, &b_oc);
  1600. *dotdot = '.';
  1601. free(a_oc.path);
  1602. free(b_oc.path);
  1603. return ret;
  1604. }
  1605. int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
  1606. {
  1607. struct object_context oc;
  1608. char *mark;
  1609. struct object *object;
  1610. struct object_id oid;
  1611. int local_flags;
  1612. const char *arg = arg_;
  1613. int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
  1614. unsigned get_sha1_flags = GET_OID_RECORD_PATH;
  1615. flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
  1616. if (!cant_be_filename && !strcmp(arg, "..")) {
  1617. /*
  1618. * Just ".."? That is not a range but the
  1619. * pathspec for the parent directory.
  1620. */
  1621. return -1;
  1622. }
  1623. if (!handle_dotdot(arg, revs, flags, revarg_opt))
  1624. return 0;
  1625. mark = strstr(arg, "^@");
  1626. if (mark && !mark[2]) {
  1627. *mark = 0;
  1628. if (add_parents_only(revs, arg, flags, 0))
  1629. return 0;
  1630. *mark = '^';
  1631. }
  1632. mark = strstr(arg, "^!");
  1633. if (mark && !mark[2]) {
  1634. *mark = 0;
  1635. if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
  1636. *mark = '^';
  1637. }
  1638. mark = strstr(arg, "^-");
  1639. if (mark) {
  1640. int exclude_parent = 1;
  1641. if (mark[2]) {
  1642. char *end;
  1643. exclude_parent = strtoul(mark + 2, &end, 10);
  1644. if (*end != '\0' || !exclude_parent)
  1645. return -1;
  1646. }
  1647. *mark = 0;
  1648. if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
  1649. *mark = '^';
  1650. }
  1651. local_flags = 0;
  1652. if (*arg == '^') {
  1653. local_flags = UNINTERESTING | BOTTOM;
  1654. arg++;
  1655. }
  1656. if (revarg_opt & REVARG_COMMITTISH)
  1657. get_sha1_flags |= GET_OID_COMMITTISH;
  1658. if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
  1659. return revs->ignore_missing ? 0 : -1;
  1660. if (!cant_be_filename)
  1661. verify_non_filename(revs->prefix, arg);
  1662. object = get_reference(revs, arg, &oid, flags ^ local_flags);
  1663. if (!object)
  1664. return revs->ignore_missing ? 0 : -1;
  1665. add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
  1666. add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
  1667. free(oc.path);
  1668. return 0;
  1669. }
  1670. static void read_pathspec_from_stdin(struct strbuf *sb,
  1671. struct argv_array *prune)
  1672. {
  1673. while (strbuf_getline(sb, stdin) != EOF)
  1674. argv_array_push(prune, sb->buf);
  1675. }
  1676. static void read_revisions_from_stdin(struct rev_info *revs,
  1677. struct argv_array *prune)
  1678. {
  1679. struct strbuf sb;
  1680. int seen_dashdash = 0;
  1681. int save_warning;
  1682. save_warning = warn_on_object_refname_ambiguity;
  1683. warn_on_object_refname_ambiguity = 0;
  1684. strbuf_init(&sb, 1000);
  1685. while (strbuf_getline(&sb, stdin) != EOF) {
  1686. int len = sb.len;
  1687. if (!len)
  1688. break;
  1689. if (sb.buf[0] == '-') {
  1690. if (len == 2 && sb.buf[1] == '-') {
  1691. seen_dashdash = 1;
  1692. break;
  1693. }
  1694. die("options not supported in --stdin mode");
  1695. }
  1696. if (handle_revision_arg(sb.buf, revs, 0,
  1697. REVARG_CANNOT_BE_FILENAME))
  1698. die("bad revision '%s'", sb.buf);
  1699. }
  1700. if (seen_dashdash)
  1701. read_pathspec_from_stdin(&sb, prune);
  1702. strbuf_release(&sb);
  1703. warn_on_object_refname_ambiguity = save_warning;
  1704. }
  1705. static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
  1706. {
  1707. append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
  1708. }
  1709. static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
  1710. {
  1711. append_header_grep_pattern(&revs->grep_filter, field, pattern);
  1712. }
  1713. static void add_message_grep(struct rev_info *revs, const char *pattern)
  1714. {
  1715. add_grep(revs, pattern, GREP_PATTERN_BODY);
  1716. }
  1717. static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
  1718. int *unkc, const char **unkv,
  1719. const struct setup_revision_opt* opt)
  1720. {
  1721. const char *arg = argv[0];
  1722. const char *optarg;
  1723. int argcount;
  1724. const unsigned hexsz = the_hash_algo->hexsz;
  1725. /* pseudo revision arguments */
  1726. if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
  1727. !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
  1728. !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
  1729. !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
  1730. !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
  1731. !strcmp(arg, "--indexed-objects") ||
  1732. !strcmp(arg, "--alternate-refs") ||
  1733. starts_with(arg, "--exclude=") ||
  1734. starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
  1735. starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
  1736. {
  1737. unkv[(*unkc)++] = arg;
  1738. return 1;
  1739. }
  1740. if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
  1741. revs->max_count = atoi(optarg);
  1742. revs->no_walk = 0;
  1743. return argcount;
  1744. } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
  1745. revs->skip_count = atoi(optarg);
  1746. return argcount;
  1747. } else if ((*arg == '-') && isdigit(arg[1])) {
  1748. /* accept -<digit>, like traditional "head" */
  1749. if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
  1750. revs->max_count < 0)
  1751. die("'%s': not a non-negative integer", arg + 1);
  1752. revs->no_walk = 0;
  1753. } else if (!strcmp(arg, "-n")) {
  1754. if (argc <= 1)
  1755. return error("-n requires an argument");
  1756. revs->max_count = atoi(argv[1]);
  1757. revs->no_walk = 0;
  1758. return 2;
  1759. } else if (skip_prefix(arg, "-n", &optarg)) {
  1760. revs->max_count = atoi(optarg);
  1761. revs->no_walk = 0;
  1762. } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
  1763. revs->max_age = atoi(optarg);
  1764. return argcount;
  1765. } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
  1766. revs->max_age = approxidate(optarg);
  1767. return argcount;
  1768. } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
  1769. revs->max_age = approxidate(optarg);
  1770. return argcount;
  1771. } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
  1772. revs->min_age = atoi(optarg);
  1773. return argcount;
  1774. } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
  1775. revs->min_age = approxidate(optarg);
  1776. return argcount;
  1777. } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
  1778. revs->min_age = approxidate(optarg);
  1779. return argcount;
  1780. } else if (!strcmp(arg, "--first-parent")) {
  1781. revs->first_parent_only = 1;
  1782. } else if (!strcmp(arg, "--ancestry-path")) {
  1783. revs->ancestry_path = 1;
  1784. revs->simplify_history = 0;
  1785. revs->limited = 1;
  1786. } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
  1787. init_reflog_walk(&revs->reflog_info);
  1788. } else if (!strcmp(arg, "--default")) {
  1789. if (argc <= 1)
  1790. return error("bad --default argument");
  1791. revs->def = argv[1];
  1792. return 2;
  1793. } else if (!strcmp(arg, "--merge")) {
  1794. revs->show_merge = 1;
  1795. } else if (!strcmp(arg, "--topo-order")) {
  1796. revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
  1797. revs->topo_order = 1;
  1798. } else if (!strcmp(arg, "--simplify-merges")) {
  1799. revs->simplify_merges = 1;
  1800. revs->topo_order = 1;
  1801. revs->rewrite_parents = 1;
  1802. revs->simplify_history = 0;
  1803. revs->limited = 1;
  1804. } else if (!strcmp(arg, "--simplify-by-decoration")) {
  1805. revs->simplify_merges = 1;
  1806. revs->topo_order = 1;
  1807. revs->rewrite_parents = 1;
  1808. revs->simplify_history = 0;
  1809. revs->simplify_by_decoration = 1;
  1810. revs->limited = 1;
  1811. revs->prune = 1;
  1812. load_ref_decorations(NULL, DECORATE_SHORT_REFS);
  1813. } else if (!strcmp(arg, "--date-order")) {
  1814. revs->sort_order = REV_SORT_BY_COMMIT_DATE;
  1815. revs->topo_order = 1;
  1816. } else if (!strcmp(arg, "--author-date-order")) {
  1817. revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
  1818. revs->topo_order = 1;
  1819. } else if (!strcmp(arg, "--early-output")) {
  1820. revs->early_output = 100;
  1821. revs->topo_order = 1;
  1822. } else if (skip_prefix(arg, "--early-output=", &optarg)) {
  1823. if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
  1824. die("'%s': not a non-negative integer", optarg);
  1825. revs->topo_order = 1;
  1826. } else if (!strcmp(arg, "--parents")) {
  1827. revs->rewrite_parents = 1;
  1828. revs->print_parents = 1;
  1829. } else if (!strcmp(arg, "--dense")) {
  1830. revs->dense = 1;
  1831. } else if (!strcmp(arg, "--sparse")) {
  1832. revs->dense = 0;
  1833. } else if (!strcmp(arg, "--in-commit-order")) {
  1834. revs->tree_blobs_in_commit_order = 1;
  1835. } else if (!strcmp(arg, "--remove-empty")) {
  1836. revs->remove_empty_trees = 1;
  1837. } else if (!strcmp(arg, "--merges")) {
  1838. revs->min_parents = 2;
  1839. } else if (!strcmp(arg, "--no-merges")) {
  1840. revs->max_parents = 1;
  1841. } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
  1842. revs->min_parents = atoi(optarg);
  1843. } else if (!strcmp(arg, "--no-min-parents")) {
  1844. revs->min_parents = 0;
  1845. } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
  1846. revs->max_parents = atoi(optarg);
  1847. } else if (!strcmp(arg, "--no-max-parents")) {
  1848. revs->max_parents = -1;
  1849. } else if (!strcmp(arg, "--boundary")) {
  1850. revs->boundary = 1;
  1851. } else if (!strcmp(arg, "--left-right")) {
  1852. revs->left_right = 1;
  1853. } else if (!strcmp(arg, "--left-only")) {
  1854. if (revs->right_only)
  1855. die("--left-only is incompatible with --right-only"
  1856. " or --cherry");
  1857. revs->left_only = 1;
  1858. } else if (!strcmp(arg, "--right-only")) {
  1859. if (revs->left_only)
  1860. die("--right-only is incompatible with --left-only");
  1861. revs->right_only = 1;
  1862. } else if (!strcmp(arg, "--cherry")) {
  1863. if (revs->left_only)
  1864. die("--cherry is incompatible with --left-only");
  1865. revs->cherry_mark = 1;
  1866. revs->right_only = 1;
  1867. revs->max_parents = 1;
  1868. revs->limited = 1;
  1869. } else if (!strcmp(arg, "--count")) {
  1870. revs->count = 1;
  1871. } else if (!strcmp(arg, "--cherry-mark")) {
  1872. if (revs->cherry_pick)
  1873. die("--cherry-mark is incompatible with --cherry-pick");
  1874. revs->cherry_mark = 1;
  1875. revs->limited = 1; /* needs limit_list() */
  1876. } else if (!strcmp(arg, "--cherry-pick")) {
  1877. if (revs->cherry_mark)
  1878. die("--cherry-pick is incompatible with --cherry-mark");
  1879. revs->cherry_pick = 1;
  1880. revs->limited = 1;
  1881. } else if (!strcmp(arg, "--objects")) {
  1882. revs->tag_objects = 1;
  1883. revs->tree_objects = 1;
  1884. revs->blob_objects = 1;
  1885. } else if (!strcmp(arg, "--objects-edge")) {
  1886. revs->tag_objects = 1;
  1887. revs->tree_objects = 1;
  1888. revs->blob_objects = 1;
  1889. revs->edge_hint = 1;
  1890. } else if (!strcmp(arg, "--objects-edge-aggressive")) {
  1891. revs->tag_objects = 1;
  1892. revs->tree_objects = 1;
  1893. revs->blob_objects = 1;
  1894. revs->edge_hint = 1;
  1895. revs->edge_hint_aggressive = 1;
  1896. } else if (!strcmp(arg, "--verify-objects")) {
  1897. revs->tag_objects = 1;
  1898. revs->tree_objects = 1;
  1899. revs->blob_objects = 1;
  1900. revs->verify_objects = 1;
  1901. } else if (!strcmp(arg, "--unpacked")) {
  1902. revs->unpacked = 1;
  1903. } else if (starts_with(arg, "--unpacked=")) {
  1904. die("--unpacked=<packfile> no longer supported.");
  1905. } else if (!strcmp(arg, "-r")) {
  1906. revs->diff = 1;
  1907. revs->diffopt.flags.recursive = 1;
  1908. } else if (!strcmp(arg, "-t")) {
  1909. revs->diff = 1;
  1910. revs->diffopt.flags.recursive = 1;
  1911. revs->diffopt.flags.tree_in_recursive = 1;
  1912. } else if (!strcmp(arg, "-m")) {
  1913. revs->ignore_merges = 0;
  1914. } else if (!strcmp(arg, "-c")) {
  1915. revs->diff = 1;
  1916. revs->dense_combined_merges = 0;
  1917. revs->combine_merges = 1;
  1918. } else if (!strcmp(arg, "--combined-all-paths")) {
  1919. revs->diff = 1;
  1920. revs->combined_all_paths = 1;
  1921. } else if (!strcmp(arg, "--cc")) {
  1922. revs->diff = 1;
  1923. revs->dense_combined_merges = 1;
  1924. revs->combine_merges = 1;
  1925. } else if (!strcmp(arg, "-v")) {
  1926. revs->verbose_header = 1;
  1927. } else if (!strcmp(arg, "--pretty")) {
  1928. revs->verbose_header = 1;
  1929. revs->pretty_given = 1;
  1930. get_commit_format(NULL, revs);
  1931. } else if (skip_prefix(arg, "--pretty=", &optarg) ||
  1932. skip_prefix(arg, "--format=", &optarg)) {
  1933. /*
  1934. * Detached form ("--pretty X" as opposed to "--pretty=X")
  1935. * not allowed, since the argument is optional.
  1936. */
  1937. revs->verbose_header = 1;
  1938. revs->pretty_given = 1;
  1939. get_commit_format(optarg, revs);
  1940. } else if (!strcmp(arg, "--expand-tabs")) {
  1941. revs->expand_tabs_in_log = 8;
  1942. } else if (!strcmp(arg, "--no-expand-tabs")) {
  1943. revs->expand_tabs_in_log = 0;
  1944. } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
  1945. int val;
  1946. if (strtol_i(arg, 10, &val) < 0 || val < 0)
  1947. die("'%s': not a non-negative integer", arg);
  1948. revs->expand_tabs_in_log = val;
  1949. } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
  1950. revs->show_notes = 1;
  1951. revs->show_notes_given = 1;
  1952. revs->notes_opt.use_default_notes = 1;
  1953. } else if (!strcmp(arg, "--show-signature")) {
  1954. revs->show_signature = 1;
  1955. } else if (!strcmp(arg, "--no-show-signature")) {
  1956. revs->show_signature = 0;
  1957. } else if (!strcmp(arg, "--show-linear-break")) {
  1958. revs->break_bar = " ..........";
  1959. revs->track_linear = 1;
  1960. revs->track_first_time = 1;
  1961. } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
  1962. revs->break_bar = xstrdup(optarg);
  1963. revs->track_linear = 1;
  1964. revs->track_first_time = 1;
  1965. } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
  1966. skip_prefix(arg, "--notes=", &optarg)) {
  1967. struct strbuf buf = STRBUF_INIT;
  1968. revs->show_notes = 1;
  1969. revs->show_notes_given = 1;
  1970. if (starts_with(arg, "--show-notes=") &&
  1971. revs->notes_opt.use_default_notes < 0)
  1972. revs->notes_opt.use_default_notes = 1;
  1973. strbuf_addstr(&buf, optarg);
  1974. expand_notes_ref(&buf);
  1975. string_list_append(&revs->notes_opt.extra_notes_refs,
  1976. strbuf_detach(&buf, NULL));
  1977. } else if (!strcmp(arg, "--no-notes")) {
  1978. revs->show_notes = 0;
  1979. revs->show_notes_given = 1;
  1980. revs->notes_opt.use_default_notes = -1;
  1981. /* we have been strdup'ing ourselves, so trick
  1982. * string_list into free()ing strings */
  1983. revs->notes_opt.extra_notes_refs.strdup_strings = 1;
  1984. string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
  1985. revs->notes_opt.extra_notes_refs.strdup_strings = 0;
  1986. } else if (!strcmp(arg, "--standard-notes")) {
  1987. revs->show_notes_given = 1;
  1988. revs->notes_opt.use_default_notes = 1;
  1989. } else if (!strcmp(arg, "--no-standard-notes")) {
  1990. revs->notes_opt.use_default_notes = 0;
  1991. } else if (!strcmp(arg, "--oneline")) {
  1992. revs->verbose_header = 1;
  1993. get_commit_format("oneline", revs);
  1994. revs->pretty_given = 1;
  1995. revs->abbrev_commit = 1;
  1996. } else if (!strcmp(arg, "--graph")) {
  1997. revs->topo_order = 1;
  1998. revs->rewrite_parents = 1;
  1999. revs->graph = graph_init(revs);
  2000. } else if (!strcmp(arg, "--root")) {
  2001. revs->show_root_diff = 1;
  2002. } else if (!strcmp(arg, "--no-commit-id")) {
  2003. revs->no_commit_id = 1;
  2004. } else if (!strcmp(arg, "--always")) {
  2005. revs->always_show_header = 1;
  2006. } else if (!strcmp(arg, "--no-abbrev")) {
  2007. revs->abbrev = 0;
  2008. } else if (!strcmp(arg, "--abbrev")) {
  2009. revs->abbrev = DEFAULT_ABBREV;
  2010. } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
  2011. revs->abbrev = strtoul(optarg, NULL, 10);
  2012. if (revs->abbrev < MINIMUM_ABBREV)
  2013. revs->abbrev = MINIMUM_ABBREV;
  2014. else if (revs->abbrev > hexsz)
  2015. revs->abbrev = hexsz;
  2016. } else if (!strcmp(arg, "--abbrev-commit")) {
  2017. revs->abbrev_commit = 1;
  2018. revs->abbrev_commit_given = 1;
  2019. } else if (!strcmp(arg, "--no-abbrev-commit")) {
  2020. revs->abbrev_commit = 0;
  2021. } else if (!strcmp(arg, "--full-diff")) {
  2022. revs->diff = 1;
  2023. revs->full_diff = 1;
  2024. } else if (!strcmp(arg, "--full-history")) {
  2025. revs->simplify_history = 0;
  2026. } else if (!strcmp(arg, "--relative-date")) {
  2027. revs->date_mode.type = DATE_RELATIVE;
  2028. revs->date_mode_explicit = 1;
  2029. } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
  2030. parse_date_format(optarg, &revs->date_mode);
  2031. revs->date_mode_explicit = 1;
  2032. return argcount;
  2033. } else if (!strcmp(arg, "--log-size")) {
  2034. revs->show_log_size = 1;
  2035. }
  2036. /*
  2037. * Grepping the commit log
  2038. */
  2039. else if ((argcount = parse_long_opt("author", argv, &optarg))) {
  2040. add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
  2041. return argcount;
  2042. } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
  2043. add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
  2044. return argcount;
  2045. } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
  2046. add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
  2047. return argcount;
  2048. } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
  2049. add_message_grep(revs, optarg);
  2050. return argcount;
  2051. } else if (!strcmp(arg, "--grep-debug")) {
  2052. revs->grep_filter.debug = 1;
  2053. } else if (!strcmp(arg, "--basic-regexp")) {
  2054. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
  2055. } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
  2056. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
  2057. } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
  2058. revs->grep_filter.ignore_case = 1;
  2059. revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
  2060. } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
  2061. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
  2062. } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
  2063. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
  2064. } else if (!strcmp(arg, "--all-match")) {
  2065. revs->grep_filter.all_match = 1;
  2066. } else if (!strcmp(arg, "--invert-grep")) {
  2067. revs->invert_grep = 1;
  2068. } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
  2069. if (strcmp(optarg, "none"))
  2070. git_log_output_encoding = xstrdup(optarg);
  2071. else
  2072. git_log_output_encoding = "";
  2073. return argcount;
  2074. } else if (!strcmp(arg, "--reverse")) {
  2075. revs->reverse ^= 1;
  2076. } else if (!strcmp(arg, "--children")) {
  2077. revs->children.name = "children";
  2078. revs->limited = 1;
  2079. } else if (!strcmp(arg, "--ignore-missing")) {
  2080. revs->ignore_missing = 1;
  2081. } else if (opt && opt->allow_exclude_promisor_objects &&
  2082. !strcmp(arg, "--exclude-promisor-objects")) {
  2083. if (fetch_if_missing)
  2084. BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
  2085. revs->exclude_promisor_objects = 1;
  2086. } else {
  2087. int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
  2088. if (!opts)
  2089. unkv[(*unkc)++] = arg;
  2090. return opts;
  2091. }
  2092. if (revs->graph && revs->track_linear)
  2093. die("--show-linear-break and --graph are incompatible");
  2094. return 1;
  2095. }
  2096. void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
  2097. const struct option *options,
  2098. const char * const usagestr[])
  2099. {
  2100. int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
  2101. &ctx->cpidx, ctx->out, NULL);
  2102. if (n <= 0) {
  2103. error("unknown option `%s'", ctx->argv[0]);
  2104. usage_with_options(usagestr, options);
  2105. }
  2106. ctx->argv += n;
  2107. ctx->argc -= n;
  2108. }
  2109. static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
  2110. void *cb_data, const char *term)
  2111. {
  2112. struct strbuf bisect_refs = STRBUF_INIT;
  2113. int status;
  2114. strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
  2115. status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
  2116. strbuf_release(&bisect_refs);
  2117. return status;
  2118. }
  2119. static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
  2120. {
  2121. return for_each_bisect_ref(refs, fn, cb_data, term_bad);
  2122. }
  2123. static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
  2124. {
  2125. return for_each_bisect_ref(refs, fn, cb_data, term_good);
  2126. }
  2127. static int handle_revision_pseudo_opt(const char *submodule,
  2128. struct rev_info *revs,
  2129. int argc, const char **argv, int *flags)
  2130. {
  2131. const char *arg = argv[0];
  2132. const char *optarg;
  2133. struct ref_store *refs;
  2134. int argcount;
  2135. if (submodule) {
  2136. /*
  2137. * We need some something like get_submodule_worktrees()
  2138. * before we can go through all worktrees of a submodule,
  2139. * .e.g with adding all HEADs from --all, which is not
  2140. * supported right now, so stick to single worktree.
  2141. */
  2142. if (!revs->single_worktree)
  2143. BUG("--single-worktree cannot be used together with submodule");
  2144. refs = get_submodule_ref_store(submodule);
  2145. } else
  2146. refs = get_main_ref_store(revs->repo);
  2147. /*
  2148. * NOTE!
  2149. *
  2150. * Commands like "git shortlog" will not accept the options below
  2151. * unless parse_revision_opt queues them (as opposed to erroring
  2152. * out).
  2153. *
  2154. * When implementing your new pseudo-option, remember to
  2155. * register it in the list at the top of handle_revision_opt.
  2156. */
  2157. if (!strcmp(arg, "--all")) {
  2158. handle_refs(refs, revs, *flags, refs_for_each_ref);
  2159. handle_refs(refs, revs, *flags, refs_head_ref);
  2160. if (!revs->single_worktree) {
  2161. struct all_refs_cb cb;
  2162. init_all_refs_cb(&cb, revs, *flags);
  2163. other_head_refs(handle_one_ref, &cb);
  2164. }
  2165. clear_ref_exclusion(&revs->ref_excludes);
  2166. } else if (!strcmp(arg, "--branches")) {
  2167. handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
  2168. clear_ref_exclusion(&revs->ref_excludes);
  2169. } else if (!strcmp(arg, "--bisect")) {
  2170. read_bisect_terms(&term_bad, &term_good);
  2171. handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
  2172. handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
  2173. for_each_good_bisect_ref);
  2174. revs->bisect = 1;
  2175. } else if (!strcmp(arg, "--tags")) {
  2176. handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
  2177. clear_ref_exclusion(&revs->ref_excludes);
  2178. } else if (!strcmp(arg, "--remotes")) {
  2179. handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
  2180. clear_ref_exclusion(&revs->ref_excludes);
  2181. } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
  2182. struct all_refs_cb cb;
  2183. init_all_refs_cb(&cb, revs, *flags);
  2184. for_each_glob_ref(handle_one_ref, optarg, &cb);
  2185. clear_ref_exclusion(&revs->ref_excludes);
  2186. return argcount;
  2187. } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
  2188. add_ref_exclusion(&revs->ref_excludes, optarg);
  2189. return argcount;
  2190. } else if (skip_prefix(arg, "--branches=", &optarg)) {
  2191. struct all_refs_cb cb;
  2192. init_all_refs_cb(&cb, revs, *flags);
  2193. for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
  2194. clear_ref_exclusion(&revs->ref_excludes);
  2195. } else if (skip_prefix(arg, "--tags=", &optarg)) {
  2196. struct all_refs_cb cb;
  2197. init_all_refs_cb(&cb, revs, *flags);
  2198. for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
  2199. clear_ref_exclusion(&revs->ref_excludes);
  2200. } else if (skip_prefix(arg, "--remotes=", &optarg)) {
  2201. struct all_refs_cb cb;
  2202. init_all_refs_cb(&cb, revs, *flags);
  2203. for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
  2204. clear_ref_exclusion(&revs->ref_excludes);
  2205. } else if (!strcmp(arg, "--reflog")) {
  2206. add_reflogs_to_pending(revs, *flags);
  2207. } else if (!strcmp(arg, "--indexed-objects")) {
  2208. add_index_objects_to_pending(revs, *flags);
  2209. } else if (!strcmp(arg, "--alternate-refs")) {
  2210. add_alternate_refs_to_pending(revs, *flags);
  2211. } else if (!strcmp(arg, "--not")) {
  2212. *flags ^= UNINTERESTING | BOTTOM;
  2213. } else if (!strcmp(arg, "--no-walk")) {
  2214. revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
  2215. } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
  2216. /*
  2217. * Detached form ("--no-walk X" as opposed to "--no-walk=X")
  2218. * not allowed, since the argument is optional.
  2219. */
  2220. if (!strcmp(optarg, "sorted"))
  2221. revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
  2222. else if (!strcmp(optarg, "unsorted"))
  2223. revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
  2224. else
  2225. return error("invalid argument to --no-walk");
  2226. } else if (!strcmp(arg, "--do-walk")) {
  2227. revs->no_walk = 0;
  2228. } else if (!strcmp(arg, "--single-worktree")) {
  2229. revs->single_worktree = 1;
  2230. } else {
  2231. return 0;
  2232. }
  2233. return 1;
  2234. }
  2235. static void NORETURN diagnose_missing_default(const char *def)
  2236. {
  2237. int flags;
  2238. const char *refname;
  2239. refname = resolve_ref_unsafe(def, 0, NULL, &flags);
  2240. if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
  2241. die(_("your current branch appears to be broken"));
  2242. skip_prefix(refname, "refs/heads/", &refname);
  2243. die(_("your current branch '%s' does not have any commits yet"),
  2244. refname);
  2245. }
  2246. /*
  2247. * Parse revision information, filling in the "rev_info" structure,
  2248. * and removing the used arguments from the argument list.
  2249. *
  2250. * Returns the number of arguments left that weren't recognized
  2251. * (which are also moved to the head of the argument list)
  2252. */
  2253. int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
  2254. {
  2255. int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
  2256. struct argv_array prune_data = ARGV_ARRAY_INIT;
  2257. const char *submodule = NULL;
  2258. int seen_end_of_options = 0;
  2259. if (opt)
  2260. submodule = opt->submodule;
  2261. /* First, search for "--" */
  2262. if (opt && opt->assume_dashdash) {
  2263. seen_dashdash = 1;
  2264. } else {
  2265. seen_dashdash = 0;
  2266. for (i = 1; i < argc; i++) {
  2267. const char *arg = argv[i];
  2268. if (strcmp(arg, "--"))
  2269. continue;
  2270. argv[i] = NULL;
  2271. argc = i;
  2272. if (argv[i + 1])
  2273. argv_array_pushv(&prune_data, argv + i + 1);
  2274. seen_dashdash = 1;
  2275. break;
  2276. }
  2277. }
  2278. /* Second, deal with arguments and options */
  2279. flags = 0;
  2280. revarg_opt = opt ? opt->revarg_opt : 0;
  2281. if (seen_dashdash)
  2282. revarg_opt |= REVARG_CANNOT_BE_FILENAME;
  2283. for (left = i = 1; i < argc; i++) {
  2284. const char *arg = argv[i];
  2285. if (!seen_end_of_options && *arg == '-') {
  2286. int opts;
  2287. opts = handle_revision_pseudo_opt(submodule,
  2288. revs, argc - i, argv + i,
  2289. &flags);
  2290. if (opts > 0) {
  2291. i += opts - 1;
  2292. continue;
  2293. }
  2294. if (!strcmp(arg, "--stdin")) {
  2295. if (revs->disable_stdin) {
  2296. argv[left++] = arg;
  2297. continue;
  2298. }
  2299. if (revs->read_from_stdin++)
  2300. die("--stdin given twice?");
  2301. read_revisions_from_stdin(revs, &prune_data);
  2302. continue;
  2303. }
  2304. if (!strcmp(arg, "--end-of-options")) {
  2305. seen_end_of_options = 1;
  2306. continue;
  2307. }
  2308. opts = handle_revision_opt(revs, argc - i, argv + i,
  2309. &left, argv, opt);
  2310. if (opts > 0) {
  2311. i += opts - 1;
  2312. continue;
  2313. }
  2314. if (opts < 0)
  2315. exit(128);
  2316. continue;
  2317. }
  2318. if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
  2319. int j;
  2320. if (seen_dashdash || *arg == '^')
  2321. die("bad revision '%s'", arg);
  2322. /* If we didn't have a "--":
  2323. * (1) all filenames must exist;
  2324. * (2) all rev-args must not be interpretable
  2325. * as a valid filename.
  2326. * but the latter we have checked in the main loop.
  2327. */
  2328. for (j = i; j < argc; j++)
  2329. verify_filename(revs->prefix, argv[j], j == i);
  2330. argv_array_pushv(&prune_data, argv + i);
  2331. break;
  2332. }
  2333. else
  2334. got_rev_arg = 1;
  2335. }
  2336. if (prune_data.argc) {
  2337. /*
  2338. * If we need to introduce the magic "a lone ':' means no
  2339. * pathspec whatsoever", here is the place to do so.
  2340. *
  2341. * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
  2342. * prune_data.nr = 0;
  2343. * prune_data.alloc = 0;
  2344. * free(prune_data.path);
  2345. * prune_data.path = NULL;
  2346. * } else {
  2347. * terminate prune_data.alloc with NULL and
  2348. * call init_pathspec() to set revs->prune_data here.
  2349. * }
  2350. */
  2351. parse_pathspec(&revs->prune_data, 0, 0,
  2352. revs->prefix, prune_data.argv);
  2353. }
  2354. argv_array_clear(&prune_data);
  2355. if (revs->def == NULL)
  2356. revs->def = opt ? opt->def : NULL;
  2357. if (opt && opt->tweak)
  2358. opt->tweak(revs, opt);
  2359. if (revs->show_merge)
  2360. prepare_show_merge(revs);
  2361. if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
  2362. struct object_id oid;
  2363. struct object *object;
  2364. struct object_context oc;
  2365. if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
  2366. diagnose_missing_default(revs->def);
  2367. object = get_reference(revs, revs->def, &oid, 0);
  2368. add_pending_object_with_mode(revs, object, revs->def, oc.mode);
  2369. }
  2370. /* Did the user ask for any diff output? Run the diff! */
  2371. if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
  2372. revs->diff = 1;
  2373. /* Pickaxe, diff-filter and rename following need diffs */
  2374. if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
  2375. revs->diffopt.filter ||
  2376. revs->diffopt.flags.follow_renames)
  2377. revs->diff = 1;
  2378. if (revs->diffopt.objfind)
  2379. revs->simplify_history = 0;
  2380. if (revs->topo_order && !generation_numbers_enabled(the_repository))
  2381. revs->limited = 1;
  2382. if (revs->prune_data.nr) {
  2383. copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
  2384. /* Can't prune commits with rename following: the paths change.. */
  2385. if (!revs->diffopt.flags.follow_renames)
  2386. revs->prune = 1;
  2387. if (!revs->full_diff)
  2388. copy_pathspec(&revs->diffopt.pathspec,
  2389. &revs->prune_data);
  2390. }
  2391. if (revs->combine_merges)
  2392. revs->ignore_merges = 0;
  2393. if (revs->combined_all_paths && !revs->combine_merges)
  2394. die("--combined-all-paths makes no sense without -c or --cc");
  2395. revs->diffopt.abbrev = revs->abbrev;
  2396. if (revs->line_level_traverse) {
  2397. revs->limited = 1;
  2398. revs->topo_order = 1;
  2399. }
  2400. diff_setup_done(&revs->diffopt);
  2401. grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
  2402. &revs->grep_filter);
  2403. compile_grep_patterns(&revs->grep_filter);
  2404. if (revs->reverse && revs->reflog_info)
  2405. die("cannot combine --reverse with --walk-reflogs");
  2406. if (revs->reflog_info && revs->limited)
  2407. die("cannot combine --walk-reflogs with history-limiting options");
  2408. if (revs->rewrite_parents && revs->children.name)
  2409. die("cannot combine --parents and --children");
  2410. /*
  2411. * Limitations on the graph functionality
  2412. */
  2413. if (revs->reverse && revs->graph)
  2414. die("cannot combine --reverse with --graph");
  2415. if (revs->reflog_info && revs->graph)
  2416. die("cannot combine --walk-reflogs with --graph");
  2417. if (revs->no_walk && revs->graph)
  2418. die("cannot combine --no-walk with --graph");
  2419. if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
  2420. die("cannot use --grep-reflog without --walk-reflogs");
  2421. if (revs->first_parent_only && revs->bisect)
  2422. die(_("--first-parent is incompatible with --bisect"));
  2423. if (revs->line_level_traverse &&
  2424. (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
  2425. die(_("-L does not yet support diff formats besides -p and -s"));
  2426. if (revs->expand_tabs_in_log < 0)
  2427. revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
  2428. return left;
  2429. }
  2430. static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
  2431. {
  2432. struct commit_list *l = xcalloc(1, sizeof(*l));
  2433. l->item = child;
  2434. l->next = add_decoration(&revs->children, &parent->object, l);
  2435. }
  2436. static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
  2437. {
  2438. struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
  2439. struct commit_list **pp, *p;
  2440. int surviving_parents;
  2441. /* Examine existing parents while marking ones we have seen... */
  2442. pp = &commit->parents;
  2443. surviving_parents = 0;
  2444. while ((p = *pp) != NULL) {
  2445. struct commit *parent = p->item;
  2446. if (parent->object.flags & TMP_MARK) {
  2447. *pp = p->next;
  2448. if (ts)
  2449. compact_treesame(revs, commit, surviving_parents);
  2450. continue;
  2451. }
  2452. parent->object.flags |= TMP_MARK;
  2453. surviving_parents++;
  2454. pp = &p->next;
  2455. }
  2456. /* clear the temporary mark */
  2457. for (p = commit->parents; p; p = p->next) {
  2458. p->item->object.flags &= ~TMP_MARK;
  2459. }
  2460. /* no update_treesame() - removing duplicates can't affect TREESAME */
  2461. return surviving_parents;
  2462. }
  2463. struct merge_simplify_state {
  2464. struct commit *simplified;
  2465. };
  2466. static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
  2467. {
  2468. struct merge_simplify_state *st;
  2469. st = lookup_decoration(&revs->merge_simplification, &commit->object);
  2470. if (!st) {
  2471. st = xcalloc(1, sizeof(*st));
  2472. add_decoration(&revs->merge_simplification, &commit->object, st);
  2473. }
  2474. return st;
  2475. }
  2476. static int mark_redundant_parents(struct commit *commit)
  2477. {
  2478. struct commit_list *h = reduce_heads(commit->parents);
  2479. int i = 0, marked = 0;
  2480. struct commit_list *po, *pn;
  2481. /* Want these for sanity-checking only */
  2482. int orig_cnt = commit_list_count(commit->parents);
  2483. int cnt = commit_list_count(h);
  2484. /*
  2485. * Not ready to remove items yet, just mark them for now, based
  2486. * on the output of reduce_heads(). reduce_heads outputs the reduced
  2487. * set in its original order, so this isn't too hard.
  2488. */
  2489. po = commit->parents;
  2490. pn = h;
  2491. while (po) {
  2492. if (pn && po->item == pn->item) {
  2493. pn = pn->next;
  2494. i++;
  2495. } else {
  2496. po->item->object.flags |= TMP_MARK;
  2497. marked++;
  2498. }
  2499. po=po->next;
  2500. }
  2501. if (i != cnt || cnt+marked != orig_cnt)
  2502. die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
  2503. free_commit_list(h);
  2504. return marked;
  2505. }
  2506. static int mark_treesame_root_parents(struct commit *commit)
  2507. {
  2508. struct commit_list *p;
  2509. int marked = 0;
  2510. for (p = commit->parents; p; p = p->next) {
  2511. struct commit *parent = p->item;
  2512. if (!parent->parents && (parent->object.flags & TREESAME)) {
  2513. parent->object.flags |= TMP_MARK;
  2514. marked++;
  2515. }
  2516. }
  2517. return marked;
  2518. }
  2519. /*
  2520. * Awkward naming - this means one parent we are TREESAME to.
  2521. * cf mark_treesame_root_parents: root parents that are TREESAME (to an
  2522. * empty tree). Better name suggestions?
  2523. */
  2524. static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
  2525. {
  2526. struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
  2527. struct commit *unmarked = NULL, *marked = NULL;
  2528. struct commit_list *p;
  2529. unsigned n;
  2530. for (p = commit->parents, n = 0; p; p = p->next, n++) {
  2531. if (ts->treesame[n]) {
  2532. if (p->item->object.flags & TMP_MARK) {
  2533. if (!marked)
  2534. marked = p->item;
  2535. } else {
  2536. if (!unmarked) {
  2537. unmarked = p->item;
  2538. break;
  2539. }
  2540. }
  2541. }
  2542. }
  2543. /*
  2544. * If we are TREESAME to a marked-for-deletion parent, but not to any
  2545. * unmarked parents, unmark the first TREESAME parent. This is the
  2546. * parent that the default simplify_history==1 scan would have followed,
  2547. * and it doesn't make sense to omit that path when asking for a
  2548. * simplified full history. Retaining it improves the chances of
  2549. * understanding odd missed merges that took an old version of a file.
  2550. *
  2551. * Example:
  2552. *
  2553. * I--------*X A modified the file, but mainline merge X used
  2554. * \ / "-s ours", so took the version from I. X is
  2555. * `-*A--' TREESAME to I and !TREESAME to A.
  2556. *
  2557. * Default log from X would produce "I". Without this check,
  2558. * --full-history --simplify-merges would produce "I-A-X", showing
  2559. * the merge commit X and that it changed A, but not making clear that
  2560. * it had just taken the I version. With this check, the topology above
  2561. * is retained.
  2562. *
  2563. * Note that it is possible that the simplification chooses a different
  2564. * TREESAME parent from the default, in which case this test doesn't
  2565. * activate, and we _do_ drop the default parent. Example:
  2566. *
  2567. * I------X A modified the file, but it was reverted in B,
  2568. * \ / meaning mainline merge X is TREESAME to both
  2569. * *A-*B parents.
  2570. *
  2571. * Default log would produce "I" by following the first parent;
  2572. * --full-history --simplify-merges will produce "I-A-B". But this is a
  2573. * reasonable result - it presents a logical full history leading from
  2574. * I to X, and X is not an important merge.
  2575. */
  2576. if (!unmarked && marked) {
  2577. marked->object.flags &= ~TMP_MARK;
  2578. return 1;
  2579. }
  2580. return 0;
  2581. }
  2582. static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
  2583. {
  2584. struct commit_list **pp, *p;
  2585. int nth_parent, removed = 0;
  2586. pp = &commit->parents;
  2587. nth_parent = 0;
  2588. while ((p = *pp) != NULL) {
  2589. struct commit *parent = p->item;
  2590. if (parent->object.flags & TMP_MARK) {
  2591. parent->object.flags &= ~TMP_MARK;
  2592. *pp = p->next;
  2593. free(p);
  2594. removed++;
  2595. compact_treesame(revs, commit, nth_parent);
  2596. continue;
  2597. }
  2598. pp = &p->next;
  2599. nth_parent++;
  2600. }
  2601. /* Removing parents can only increase TREESAMEness */
  2602. if (removed && !(commit->object.flags & TREESAME))
  2603. update_treesame(revs, commit);
  2604. return nth_parent;
  2605. }
  2606. static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
  2607. {
  2608. struct commit_list *p;
  2609. struct commit *parent;
  2610. struct merge_simplify_state *st, *pst;
  2611. int cnt;
  2612. st = locate_simplify_state(revs, commit);
  2613. /*
  2614. * Have we handled this one?
  2615. */
  2616. if (st->simplified)
  2617. return tail;
  2618. /*
  2619. * An UNINTERESTING commit simplifies to itself, so does a
  2620. * root commit. We do not rewrite parents of such commit
  2621. * anyway.
  2622. */
  2623. if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
  2624. st->simplified = commit;
  2625. return tail;
  2626. }
  2627. /*
  2628. * Do we know what commit all of our parents that matter
  2629. * should be rewritten to? Otherwise we are not ready to
  2630. * rewrite this one yet.
  2631. */
  2632. for (cnt = 0, p = commit->parents; p; p = p->next) {
  2633. pst = locate_simplify_state(revs, p->item);
  2634. if (!pst->simplified) {
  2635. tail = &commit_list_insert(p->item, tail)->next;
  2636. cnt++;
  2637. }
  2638. if (revs->first_parent_only)
  2639. break;
  2640. }
  2641. if (cnt) {
  2642. tail = &commit_list_insert(commit, tail)->next;
  2643. return tail;
  2644. }
  2645. /*
  2646. * Rewrite our list of parents. Note that this cannot
  2647. * affect our TREESAME flags in any way - a commit is
  2648. * always TREESAME to its simplification.
  2649. */
  2650. for (p = commit->parents; p; p = p->next) {
  2651. pst = locate_simplify_state(revs, p->item);
  2652. p->item = pst->simplified;
  2653. if (revs->first_parent_only)
  2654. break;
  2655. }
  2656. if (revs->first_parent_only)
  2657. cnt = 1;
  2658. else
  2659. cnt = remove_duplicate_parents(revs, commit);
  2660. /*
  2661. * It is possible that we are a merge and one side branch
  2662. * does not have any commit that touches the given paths;
  2663. * in such a case, the immediate parent from that branch
  2664. * will be rewritten to be the merge base.
  2665. *
  2666. * o----X X: the commit we are looking at;
  2667. * / / o: a commit that touches the paths;
  2668. * ---o----'
  2669. *
  2670. * Further, a merge of an independent branch that doesn't
  2671. * touch the path will reduce to a treesame root parent:
  2672. *
  2673. * ----o----X X: the commit we are looking at;
  2674. * / o: a commit that touches the paths;
  2675. * r r: a root commit not touching the paths
  2676. *
  2677. * Detect and simplify both cases.
  2678. */
  2679. if (1 < cnt) {
  2680. int marked = mark_redundant_parents(commit);
  2681. marked += mark_treesame_root_parents(commit);
  2682. if (marked)
  2683. marked -= leave_one_treesame_to_parent(revs, commit);
  2684. if (marked)
  2685. cnt = remove_marked_parents(revs, commit);
  2686. }
  2687. /*
  2688. * A commit simplifies to itself if it is a root, if it is
  2689. * UNINTERESTING, if it touches the given paths, or if it is a
  2690. * merge and its parents don't simplify to one relevant commit
  2691. * (the first two cases are already handled at the beginning of
  2692. * this function).
  2693. *
  2694. * Otherwise, it simplifies to what its sole relevant parent
  2695. * simplifies to.
  2696. */
  2697. if (!cnt ||
  2698. (commit->object.flags & UNINTERESTING) ||
  2699. !(commit->object.flags & TREESAME) ||
  2700. (parent = one_relevant_parent(revs, commit->parents)) == NULL)
  2701. st->simplified = commit;
  2702. else {
  2703. pst = locate_simplify_state(revs, parent);
  2704. st->simplified = pst->simplified;
  2705. }
  2706. return tail;
  2707. }
  2708. static void simplify_merges(struct rev_info *revs)
  2709. {
  2710. struct commit_list *list, *next;
  2711. struct commit_list *yet_to_do, **tail;
  2712. struct commit *commit;
  2713. if (!revs->prune)
  2714. return;
  2715. /* feed the list reversed */
  2716. yet_to_do = NULL;
  2717. for (list = revs->commits; list; list = next) {
  2718. commit = list->item;
  2719. next = list->next;
  2720. /*
  2721. * Do not free(list) here yet; the original list
  2722. * is used later in this function.
  2723. */
  2724. commit_list_insert(commit, &yet_to_do);
  2725. }
  2726. while (yet_to_do) {
  2727. list = yet_to_do;
  2728. yet_to_do = NULL;
  2729. tail = &yet_to_do;
  2730. while (list) {
  2731. commit = pop_commit(&list);
  2732. tail = simplify_one(revs, commit, tail);
  2733. }
  2734. }
  2735. /* clean up the result, removing the simplified ones */
  2736. list = revs->commits;
  2737. revs->commits = NULL;
  2738. tail = &revs->commits;
  2739. while (list) {
  2740. struct merge_simplify_state *st;
  2741. commit = pop_commit(&list);
  2742. st = locate_simplify_state(revs, commit);
  2743. if (st->simplified == commit)
  2744. tail = &commit_list_insert(commit, tail)->next;
  2745. }
  2746. }
  2747. static void set_children(struct rev_info *revs)
  2748. {
  2749. struct commit_list *l;
  2750. for (l = revs->commits; l; l = l->next) {
  2751. struct commit *commit = l->item;
  2752. struct commit_list *p;
  2753. for (p = commit->parents; p; p = p->next)
  2754. add_child(revs, p->item, commit);
  2755. }
  2756. }
  2757. void reset_revision_walk(void)
  2758. {
  2759. clear_object_flags(SEEN | ADDED | SHOWN);
  2760. }
  2761. static int mark_uninteresting(const struct object_id *oid,
  2762. struct packed_git *pack,
  2763. uint32_t pos,
  2764. void *cb)
  2765. {
  2766. struct rev_info *revs = cb;
  2767. struct object *o = parse_object(revs->repo, oid);
  2768. o->flags |= UNINTERESTING | SEEN;
  2769. return 0;
  2770. }
  2771. define_commit_slab(indegree_slab, int);
  2772. define_commit_slab(author_date_slab, timestamp_t);
  2773. struct topo_walk_info {
  2774. uint32_t min_generation;
  2775. struct prio_queue explore_queue;
  2776. struct prio_queue indegree_queue;
  2777. struct prio_queue topo_queue;
  2778. struct indegree_slab indegree;
  2779. struct author_date_slab author_date;
  2780. };
  2781. static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
  2782. {
  2783. if (c->object.flags & flag)
  2784. return;
  2785. c->object.flags |= flag;
  2786. prio_queue_put(q, c);
  2787. }
  2788. static void explore_walk_step(struct rev_info *revs)
  2789. {
  2790. struct topo_walk_info *info = revs->topo_walk_info;
  2791. struct commit_list *p;
  2792. struct commit *c = prio_queue_get(&info->explore_queue);
  2793. if (!c)
  2794. return;
  2795. if (parse_commit_gently(c, 1) < 0)
  2796. return;
  2797. if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
  2798. record_author_date(&info->author_date, c);
  2799. if (revs->max_age != -1 && (c->date < revs->max_age))
  2800. c->object.flags |= UNINTERESTING;
  2801. if (process_parents(revs, c, NULL, NULL) < 0)
  2802. return;
  2803. if (c->object.flags & UNINTERESTING)
  2804. mark_parents_uninteresting(c);
  2805. for (p = c->parents; p; p = p->next)
  2806. test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
  2807. }
  2808. static void explore_to_depth(struct rev_info *revs,
  2809. uint32_t gen_cutoff)
  2810. {
  2811. struct topo_walk_info *info = revs->topo_walk_info;
  2812. struct commit *c;
  2813. while ((c = prio_queue_peek(&info->explore_queue)) &&
  2814. c->generation >= gen_cutoff)
  2815. explore_walk_step(revs);
  2816. }
  2817. static void indegree_walk_step(struct rev_info *revs)
  2818. {
  2819. struct commit_list *p;
  2820. struct topo_walk_info *info = revs->topo_walk_info;
  2821. struct commit *c = prio_queue_get(&info->indegree_queue);
  2822. if (!c)
  2823. return;
  2824. if (parse_commit_gently(c, 1) < 0)
  2825. return;
  2826. explore_to_depth(revs, c->generation);
  2827. for (p = c->parents; p; p = p->next) {
  2828. struct commit *parent = p->item;
  2829. int *pi = indegree_slab_at(&info->indegree, parent);
  2830. if (*pi)
  2831. (*pi)++;
  2832. else
  2833. *pi = 2;
  2834. test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
  2835. if (revs->first_parent_only)
  2836. return;
  2837. }
  2838. }
  2839. static void compute_indegrees_to_depth(struct rev_info *revs,
  2840. uint32_t gen_cutoff)
  2841. {
  2842. struct topo_walk_info *info = revs->topo_walk_info;
  2843. struct commit *c;
  2844. while ((c = prio_queue_peek(&info->indegree_queue)) &&
  2845. c->generation >= gen_cutoff)
  2846. indegree_walk_step(revs);
  2847. }
  2848. static void init_topo_walk(struct rev_info *revs)
  2849. {
  2850. struct topo_walk_info *info;
  2851. struct commit_list *list;
  2852. revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
  2853. info = revs->topo_walk_info;
  2854. memset(info, 0, sizeof(struct topo_walk_info));
  2855. init_indegree_slab(&info->indegree);
  2856. memset(&info->explore_queue, 0, sizeof(info->explore_queue));
  2857. memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
  2858. memset(&info->topo_queue, 0, sizeof(info->topo_queue));
  2859. switch (revs->sort_order) {
  2860. default: /* REV_SORT_IN_GRAPH_ORDER */
  2861. info->topo_queue.compare = NULL;
  2862. break;
  2863. case REV_SORT_BY_COMMIT_DATE:
  2864. info->topo_queue.compare = compare_commits_by_commit_date;
  2865. break;
  2866. case REV_SORT_BY_AUTHOR_DATE:
  2867. init_author_date_slab(&info->author_date);
  2868. info->topo_queue.compare = compare_commits_by_author_date;
  2869. info->topo_queue.cb_data = &info->author_date;
  2870. break;
  2871. }
  2872. info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
  2873. info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
  2874. info->min_generation = GENERATION_NUMBER_INFINITY;
  2875. for (list = revs->commits; list; list = list->next) {
  2876. struct commit *c = list->item;
  2877. if (parse_commit_gently(c, 1))
  2878. continue;
  2879. test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
  2880. test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
  2881. if (c->generation < info->min_generation)
  2882. info->min_generation = c->generation;
  2883. *(indegree_slab_at(&info->indegree, c)) = 1;
  2884. if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
  2885. record_author_date(&info->author_date, c);
  2886. }
  2887. compute_indegrees_to_depth(revs, info->min_generation);
  2888. for (list = revs->commits; list; list = list->next) {
  2889. struct commit *c = list->item;
  2890. if (*(indegree_slab_at(&info->indegree, c)) == 1)
  2891. prio_queue_put(&info->topo_queue, c);
  2892. }
  2893. /*
  2894. * This is unfortunate; the initial tips need to be shown
  2895. * in the order given from the revision traversal machinery.
  2896. */
  2897. if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
  2898. prio_queue_reverse(&info->topo_queue);
  2899. }
  2900. static struct commit *next_topo_commit(struct rev_info *revs)
  2901. {
  2902. struct commit *c;
  2903. struct topo_walk_info *info = revs->topo_walk_info;
  2904. /* pop next off of topo_queue */
  2905. c = prio_queue_get(&info->topo_queue);
  2906. if (c)
  2907. *(indegree_slab_at(&info->indegree, c)) = 0;
  2908. return c;
  2909. }
  2910. static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
  2911. {
  2912. struct commit_list *p;
  2913. struct topo_walk_info *info = revs->topo_walk_info;
  2914. if (process_parents(revs, commit, NULL, NULL) < 0) {
  2915. if (!revs->ignore_missing_links)
  2916. die("Failed to traverse parents of commit %s",
  2917. oid_to_hex(&commit->object.oid));
  2918. }
  2919. for (p = commit->parents; p; p = p->next) {
  2920. struct commit *parent = p->item;
  2921. int *pi;
  2922. if (parent->object.flags & UNINTERESTING)
  2923. continue;
  2924. if (parse_commit_gently(parent, 1) < 0)
  2925. continue;
  2926. if (parent->generation < info->min_generation) {
  2927. info->min_generation = parent->generation;
  2928. compute_indegrees_to_depth(revs, info->min_generation);
  2929. }
  2930. pi = indegree_slab_at(&info->indegree, parent);
  2931. (*pi)--;
  2932. if (*pi == 1)
  2933. prio_queue_put(&info->topo_queue, parent);
  2934. if (revs->first_parent_only)
  2935. return;
  2936. }
  2937. }
  2938. int prepare_revision_walk(struct rev_info *revs)
  2939. {
  2940. int i;
  2941. struct object_array old_pending;
  2942. struct commit_list **next = &revs->commits;
  2943. memcpy(&old_pending, &revs->pending, sizeof(old_pending));
  2944. revs->pending.nr = 0;
  2945. revs->pending.alloc = 0;
  2946. revs->pending.objects = NULL;
  2947. for (i = 0; i < old_pending.nr; i++) {
  2948. struct object_array_entry *e = old_pending.objects + i;
  2949. struct commit *commit = handle_commit(revs, e);
  2950. if (commit) {
  2951. if (!(commit->object.flags & SEEN)) {
  2952. commit->object.flags |= SEEN;
  2953. next = commit_list_append(commit, next);
  2954. }
  2955. }
  2956. }
  2957. object_array_clear(&old_pending);
  2958. /* Signal whether we need per-parent treesame decoration */
  2959. if (revs->simplify_merges ||
  2960. (revs->limited && limiting_can_increase_treesame(revs)))
  2961. revs->treesame.name = "treesame";
  2962. if (revs->exclude_promisor_objects) {
  2963. for_each_packed_object(mark_uninteresting, revs,
  2964. FOR_EACH_OBJECT_PROMISOR_ONLY);
  2965. }
  2966. if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
  2967. commit_list_sort_by_date(&revs->commits);
  2968. if (revs->no_walk)
  2969. return 0;
  2970. if (revs->limited) {
  2971. if (limit_list(revs) < 0)
  2972. return -1;
  2973. if (revs->topo_order)
  2974. sort_in_topological_order(&revs->commits, revs->sort_order);
  2975. } else if (revs->topo_order)
  2976. init_topo_walk(revs);
  2977. if (revs->line_level_traverse)
  2978. line_log_filter(revs);
  2979. if (revs->simplify_merges)
  2980. simplify_merges(revs);
  2981. if (revs->children.name)
  2982. set_children(revs);
  2983. return 0;
  2984. }
  2985. static enum rewrite_result rewrite_one_1(struct rev_info *revs,
  2986. struct commit **pp,
  2987. struct prio_queue *queue)
  2988. {
  2989. for (;;) {
  2990. struct commit *p = *pp;
  2991. if (!revs->limited)
  2992. if (process_parents(revs, p, NULL, queue) < 0)
  2993. return rewrite_one_error;
  2994. if (p->object.flags & UNINTERESTING)
  2995. return rewrite_one_ok;
  2996. if (!(p->object.flags & TREESAME))
  2997. return rewrite_one_ok;
  2998. if (!p->parents)
  2999. return rewrite_one_noparents;
  3000. if ((p = one_relevant_parent(revs, p->parents)) == NULL)
  3001. return rewrite_one_ok;
  3002. *pp = p;
  3003. }
  3004. }
  3005. static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
  3006. {
  3007. while (q->nr) {
  3008. struct commit *item = prio_queue_peek(q);
  3009. struct commit_list *p = *list;
  3010. if (p && p->item->date >= item->date)
  3011. list = &p->next;
  3012. else {
  3013. p = commit_list_insert(item, list);
  3014. list = &p->next; /* skip newly added item */
  3015. prio_queue_get(q); /* pop item */
  3016. }
  3017. }
  3018. }
  3019. static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
  3020. {
  3021. struct prio_queue queue = { compare_commits_by_commit_date };
  3022. enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
  3023. merge_queue_into_list(&queue, &revs->commits);
  3024. clear_prio_queue(&queue);
  3025. return ret;
  3026. }
  3027. int rewrite_parents(struct rev_info *revs, struct commit *commit,
  3028. rewrite_parent_fn_t rewrite_parent)
  3029. {
  3030. struct commit_list **pp = &commit->parents;
  3031. while (*pp) {
  3032. struct commit_list *parent = *pp;
  3033. switch (rewrite_parent(revs, &parent->item)) {
  3034. case rewrite_one_ok:
  3035. break;
  3036. case rewrite_one_noparents:
  3037. *pp = parent->next;
  3038. continue;
  3039. case rewrite_one_error:
  3040. return -1;
  3041. }
  3042. pp = &parent->next;
  3043. }
  3044. remove_duplicate_parents(revs, commit);
  3045. return 0;
  3046. }
  3047. static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
  3048. {
  3049. char *person, *endp;
  3050. size_t len, namelen, maillen;
  3051. const char *name;
  3052. const char *mail;
  3053. struct ident_split ident;
  3054. person = strstr(buf->buf, what);
  3055. if (!person)
  3056. return 0;
  3057. person += strlen(what);
  3058. endp = strchr(person, '\n');
  3059. if (!endp)
  3060. return 0;
  3061. len = endp - person;
  3062. if (split_ident_line(&ident, person, len))
  3063. return 0;
  3064. mail = ident.mail_begin;
  3065. maillen = ident.mail_end - ident.mail_begin;
  3066. name = ident.name_begin;
  3067. namelen = ident.name_end - ident.name_begin;
  3068. if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
  3069. struct strbuf namemail = STRBUF_INIT;
  3070. strbuf_addf(&namemail, "%.*s <%.*s>",
  3071. (int)namelen, name, (int)maillen, mail);
  3072. strbuf_splice(buf, ident.name_begin - buf->buf,
  3073. ident.mail_end - ident.name_begin + 1,
  3074. namemail.buf, namemail.len);
  3075. strbuf_release(&namemail);
  3076. return 1;
  3077. }
  3078. return 0;
  3079. }
  3080. static int commit_match(struct commit *commit, struct rev_info *opt)
  3081. {
  3082. int retval;
  3083. const char *encoding;
  3084. const char *message;
  3085. struct strbuf buf = STRBUF_INIT;
  3086. if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
  3087. return 1;
  3088. /* Prepend "fake" headers as needed */
  3089. if (opt->grep_filter.use_reflog_filter) {
  3090. strbuf_addstr(&buf, "reflog ");
  3091. get_reflog_message(&buf, opt->reflog_info);
  3092. strbuf_addch(&buf, '\n');
  3093. }
  3094. /*
  3095. * We grep in the user's output encoding, under the assumption that it
  3096. * is the encoding they are most likely to write their grep pattern
  3097. * for. In addition, it means we will match the "notes" encoding below,
  3098. * so we will not end up with a buffer that has two different encodings
  3099. * in it.
  3100. */
  3101. encoding = get_log_output_encoding();
  3102. message = logmsg_reencode(commit, NULL, encoding);
  3103. /* Copy the commit to temporary if we are using "fake" headers */
  3104. if (buf.len)
  3105. strbuf_addstr(&buf, message);
  3106. if (opt->grep_filter.header_list && opt->mailmap) {
  3107. if (!buf.len)
  3108. strbuf_addstr(&buf, message);
  3109. commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
  3110. commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
  3111. }
  3112. /* Append "fake" message parts as needed */
  3113. if (opt->show_notes) {
  3114. if (!buf.len)
  3115. strbuf_addstr(&buf, message);
  3116. format_display_notes(&commit->object.oid, &buf, encoding, 1);
  3117. }
  3118. /*
  3119. * Find either in the original commit message, or in the temporary.
  3120. * Note that we cast away the constness of "message" here. It is
  3121. * const because it may come from the cached commit buffer. That's OK,
  3122. * because we know that it is modifiable heap memory, and that while
  3123. * grep_buffer may modify it for speed, it will restore any
  3124. * changes before returning.
  3125. */
  3126. if (buf.len)
  3127. retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
  3128. else
  3129. retval = grep_buffer(&opt->grep_filter,
  3130. (char *)message, strlen(message));
  3131. strbuf_release(&buf);
  3132. unuse_commit_buffer(commit, message);
  3133. return opt->invert_grep ? !retval : retval;
  3134. }
  3135. static inline int want_ancestry(const struct rev_info *revs)
  3136. {
  3137. return (revs->rewrite_parents || revs->children.name);
  3138. }
  3139. /*
  3140. * Return a timestamp to be used for --since/--until comparisons for this
  3141. * commit, based on the revision options.
  3142. */
  3143. static timestamp_t comparison_date(const struct rev_info *revs,
  3144. struct commit *commit)
  3145. {
  3146. return revs->reflog_info ?
  3147. get_reflog_timestamp(revs->reflog_info) :
  3148. commit->date;
  3149. }
  3150. enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
  3151. {
  3152. if (commit->object.flags & SHOWN)
  3153. return commit_ignore;
  3154. if (revs->unpacked && has_object_pack(&commit->object.oid))
  3155. return commit_ignore;
  3156. if (commit->object.flags & UNINTERESTING)
  3157. return commit_ignore;
  3158. if (revs->min_age != -1 &&
  3159. comparison_date(revs, commit) > revs->min_age)
  3160. return commit_ignore;
  3161. if (revs->min_parents || (revs->max_parents >= 0)) {
  3162. int n = commit_list_count(commit->parents);
  3163. if ((n < revs->min_parents) ||
  3164. ((revs->max_parents >= 0) && (n > revs->max_parents)))
  3165. return commit_ignore;
  3166. }
  3167. if (!commit_match(commit, revs))
  3168. return commit_ignore;
  3169. if (revs->prune && revs->dense) {
  3170. /* Commit without changes? */
  3171. if (commit->object.flags & TREESAME) {
  3172. int n;
  3173. struct commit_list *p;
  3174. /* drop merges unless we want parenthood */
  3175. if (!want_ancestry(revs))
  3176. return commit_ignore;
  3177. /*
  3178. * If we want ancestry, then need to keep any merges
  3179. * between relevant commits to tie together topology.
  3180. * For consistency with TREESAME and simplification
  3181. * use "relevant" here rather than just INTERESTING,
  3182. * to treat bottom commit(s) as part of the topology.
  3183. */
  3184. for (n = 0, p = commit->parents; p; p = p->next)
  3185. if (relevant_commit(p->item))
  3186. if (++n >= 2)
  3187. return commit_show;
  3188. return commit_ignore;
  3189. }
  3190. }
  3191. return commit_show;
  3192. }
  3193. define_commit_slab(saved_parents, struct commit_list *);
  3194. #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
  3195. /*
  3196. * You may only call save_parents() once per commit (this is checked
  3197. * for non-root commits).
  3198. */
  3199. static void save_parents(struct rev_info *revs, struct commit *commit)
  3200. {
  3201. struct commit_list **pp;
  3202. if (!revs->saved_parents_slab) {
  3203. revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
  3204. init_saved_parents(revs->saved_parents_slab);
  3205. }
  3206. pp = saved_parents_at(revs->saved_parents_slab, commit);
  3207. /*
  3208. * When walking with reflogs, we may visit the same commit
  3209. * several times: once for each appearance in the reflog.
  3210. *
  3211. * In this case, save_parents() will be called multiple times.
  3212. * We want to keep only the first set of parents. We need to
  3213. * store a sentinel value for an empty (i.e., NULL) parent
  3214. * list to distinguish it from a not-yet-saved list, however.
  3215. */
  3216. if (*pp)
  3217. return;
  3218. if (commit->parents)
  3219. *pp = copy_commit_list(commit->parents);
  3220. else
  3221. *pp = EMPTY_PARENT_LIST;
  3222. }
  3223. static void free_saved_parents(struct rev_info *revs)
  3224. {
  3225. if (revs->saved_parents_slab)
  3226. clear_saved_parents(revs->saved_parents_slab);
  3227. }
  3228. struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
  3229. {
  3230. struct commit_list *parents;
  3231. if (!revs->saved_parents_slab)
  3232. return commit->parents;
  3233. parents = *saved_parents_at(revs->saved_parents_slab, commit);
  3234. if (parents == EMPTY_PARENT_LIST)
  3235. return NULL;
  3236. return parents;
  3237. }
  3238. enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
  3239. {
  3240. enum commit_action action = get_commit_action(revs, commit);
  3241. if (action == commit_show &&
  3242. revs->prune && revs->dense && want_ancestry(revs)) {
  3243. /*
  3244. * --full-diff on simplified parents is no good: it
  3245. * will show spurious changes from the commits that
  3246. * were elided. So we save the parents on the side
  3247. * when --full-diff is in effect.
  3248. */
  3249. if (revs->full_diff)
  3250. save_parents(revs, commit);
  3251. if (rewrite_parents(revs, commit, rewrite_one) < 0)
  3252. return commit_error;
  3253. }
  3254. return action;
  3255. }
  3256. static void track_linear(struct rev_info *revs, struct commit *commit)
  3257. {
  3258. if (revs->track_first_time) {
  3259. revs->linear = 1;
  3260. revs->track_first_time = 0;
  3261. } else {
  3262. struct commit_list *p;
  3263. for (p = revs->previous_parents; p; p = p->next)
  3264. if (p->item == NULL || /* first commit */
  3265. oideq(&p->item->object.oid, &commit->object.oid))
  3266. break;
  3267. revs->linear = p != NULL;
  3268. }
  3269. if (revs->reverse) {
  3270. if (revs->linear)
  3271. commit->object.flags |= TRACK_LINEAR;
  3272. }
  3273. free_commit_list(revs->previous_parents);
  3274. revs->previous_parents = copy_commit_list(commit->parents);
  3275. }
  3276. static struct commit *get_revision_1(struct rev_info *revs)
  3277. {
  3278. while (1) {
  3279. struct commit *commit;
  3280. if (revs->reflog_info)
  3281. commit = next_reflog_entry(revs->reflog_info);
  3282. else if (revs->topo_walk_info)
  3283. commit = next_topo_commit(revs);
  3284. else
  3285. commit = pop_commit(&revs->commits);
  3286. if (!commit)
  3287. return NULL;
  3288. if (revs->reflog_info)
  3289. commit->object.flags &= ~(ADDED | SEEN | SHOWN);
  3290. /*
  3291. * If we haven't done the list limiting, we need to look at
  3292. * the parents here. We also need to do the date-based limiting
  3293. * that we'd otherwise have done in limit_list().
  3294. */
  3295. if (!revs->limited) {
  3296. if (revs->max_age != -1 &&
  3297. comparison_date(revs, commit) < revs->max_age)
  3298. continue;
  3299. if (revs->reflog_info)
  3300. try_to_simplify_commit(revs, commit);
  3301. else if (revs->topo_walk_info)
  3302. expand_topo_walk(revs, commit);
  3303. else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
  3304. if (!revs->ignore_missing_links)
  3305. die("Failed to traverse parents of commit %s",
  3306. oid_to_hex(&commit->object.oid));
  3307. }
  3308. }
  3309. switch (simplify_commit(revs, commit)) {
  3310. case commit_ignore:
  3311. continue;
  3312. case commit_error:
  3313. die("Failed to simplify parents of commit %s",
  3314. oid_to_hex(&commit->object.oid));
  3315. default:
  3316. if (revs->track_linear)
  3317. track_linear(revs, commit);
  3318. return commit;
  3319. }
  3320. }
  3321. }
  3322. /*
  3323. * Return true for entries that have not yet been shown. (This is an
  3324. * object_array_each_func_t.)
  3325. */
  3326. static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
  3327. {
  3328. return !(entry->item->flags & SHOWN);
  3329. }
  3330. /*
  3331. * If array is on the verge of a realloc, garbage-collect any entries
  3332. * that have already been shown to try to free up some space.
  3333. */
  3334. static void gc_boundary(struct object_array *array)
  3335. {
  3336. if (array->nr == array->alloc)
  3337. object_array_filter(array, entry_unshown, NULL);
  3338. }
  3339. static void create_boundary_commit_list(struct rev_info *revs)
  3340. {
  3341. unsigned i;
  3342. struct commit *c;
  3343. struct object_array *array = &revs->boundary_commits;
  3344. struct object_array_entry *objects = array->objects;
  3345. /*
  3346. * If revs->commits is non-NULL at this point, an error occurred in
  3347. * get_revision_1(). Ignore the error and continue printing the
  3348. * boundary commits anyway. (This is what the code has always
  3349. * done.)
  3350. */
  3351. if (revs->commits) {
  3352. free_commit_list(revs->commits);
  3353. revs->commits = NULL;
  3354. }
  3355. /*
  3356. * Put all of the actual boundary commits from revs->boundary_commits
  3357. * into revs->commits
  3358. */
  3359. for (i = 0; i < array->nr; i++) {
  3360. c = (struct commit *)(objects[i].item);
  3361. if (!c)
  3362. continue;
  3363. if (!(c->object.flags & CHILD_SHOWN))
  3364. continue;
  3365. if (c->object.flags & (SHOWN | BOUNDARY))
  3366. continue;
  3367. c->object.flags |= BOUNDARY;
  3368. commit_list_insert(c, &revs->commits);
  3369. }
  3370. /*
  3371. * If revs->topo_order is set, sort the boundary commits
  3372. * in topological order
  3373. */
  3374. sort_in_topological_order(&revs->commits, revs->sort_order);
  3375. }
  3376. static struct commit *get_revision_internal(struct rev_info *revs)
  3377. {
  3378. struct commit *c = NULL;
  3379. struct commit_list *l;
  3380. if (revs->boundary == 2) {
  3381. /*
  3382. * All of the normal commits have already been returned,
  3383. * and we are now returning boundary commits.
  3384. * create_boundary_commit_list() has populated
  3385. * revs->commits with the remaining commits to return.
  3386. */
  3387. c = pop_commit(&revs->commits);
  3388. if (c)
  3389. c->object.flags |= SHOWN;
  3390. return c;
  3391. }
  3392. /*
  3393. * If our max_count counter has reached zero, then we are done. We
  3394. * don't simply return NULL because we still might need to show
  3395. * boundary commits. But we want to avoid calling get_revision_1, which
  3396. * might do a considerable amount of work finding the next commit only
  3397. * for us to throw it away.
  3398. *
  3399. * If it is non-zero, then either we don't have a max_count at all
  3400. * (-1), or it is still counting, in which case we decrement.
  3401. */
  3402. if (revs->max_count) {
  3403. c = get_revision_1(revs);
  3404. if (c) {
  3405. while (revs->skip_count > 0) {
  3406. revs->skip_count--;
  3407. c = get_revision_1(revs);
  3408. if (!c)
  3409. break;
  3410. }
  3411. }
  3412. if (revs->max_count > 0)
  3413. revs->max_count--;
  3414. }
  3415. if (c)
  3416. c->object.flags |= SHOWN;
  3417. if (!revs->boundary)
  3418. return c;
  3419. if (!c) {
  3420. /*
  3421. * get_revision_1() runs out the commits, and
  3422. * we are done computing the boundaries.
  3423. * switch to boundary commits output mode.
  3424. */
  3425. revs->boundary = 2;
  3426. /*
  3427. * Update revs->commits to contain the list of
  3428. * boundary commits.
  3429. */
  3430. create_boundary_commit_list(revs);
  3431. return get_revision_internal(revs);
  3432. }
  3433. /*
  3434. * boundary commits are the commits that are parents of the
  3435. * ones we got from get_revision_1() but they themselves are
  3436. * not returned from get_revision_1(). Before returning
  3437. * 'c', we need to mark its parents that they could be boundaries.
  3438. */
  3439. for (l = c->parents; l; l = l->next) {
  3440. struct object *p;
  3441. p = &(l->item->object);
  3442. if (p->flags & (CHILD_SHOWN | SHOWN))
  3443. continue;
  3444. p->flags |= CHILD_SHOWN;
  3445. gc_boundary(&revs->boundary_commits);
  3446. add_object_array(p, NULL, &revs->boundary_commits);
  3447. }
  3448. return c;
  3449. }
  3450. struct commit *get_revision(struct rev_info *revs)
  3451. {
  3452. struct commit *c;
  3453. struct commit_list *reversed;
  3454. if (revs->reverse) {
  3455. reversed = NULL;
  3456. while ((c = get_revision_internal(revs)))
  3457. commit_list_insert(c, &reversed);
  3458. revs->commits = reversed;
  3459. revs->reverse = 0;
  3460. revs->reverse_output_stage = 1;
  3461. }
  3462. if (revs->reverse_output_stage) {
  3463. c = pop_commit(&revs->commits);
  3464. if (revs->track_linear)
  3465. revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
  3466. return c;
  3467. }
  3468. c = get_revision_internal(revs);
  3469. if (c && revs->graph)
  3470. graph_update(revs->graph, c);
  3471. if (!c) {
  3472. free_saved_parents(revs);
  3473. if (revs->previous_parents) {
  3474. free_commit_list(revs->previous_parents);
  3475. revs->previous_parents = NULL;
  3476. }
  3477. }
  3478. return c;
  3479. }
  3480. char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
  3481. {
  3482. if (commit->object.flags & BOUNDARY)
  3483. return "-";
  3484. else if (commit->object.flags & UNINTERESTING)
  3485. return "^";
  3486. else if (commit->object.flags & PATCHSAME)
  3487. return "=";
  3488. else if (!revs || revs->left_right) {
  3489. if (commit->object.flags & SYMMETRIC_LEFT)
  3490. return "<";
  3491. else
  3492. return ">";
  3493. } else if (revs->graph)
  3494. return "*";
  3495. else if (revs->cherry_mark)
  3496. return "+";
  3497. return "";
  3498. }
  3499. void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
  3500. {
  3501. char *mark = get_revision_mark(revs, commit);
  3502. if (!strlen(mark))
  3503. return;
  3504. fputs(mark, stdout);
  3505. putchar(' ');
  3506. }