pack-objects.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773
  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "repository.h"
  4. #include "config.h"
  5. #include "attr.h"
  6. #include "object.h"
  7. #include "blob.h"
  8. #include "commit.h"
  9. #include "tag.h"
  10. #include "tree.h"
  11. #include "delta.h"
  12. #include "pack.h"
  13. #include "pack-revindex.h"
  14. #include "csum-file.h"
  15. #include "tree-walk.h"
  16. #include "diff.h"
  17. #include "revision.h"
  18. #include "list-objects.h"
  19. #include "list-objects-filter.h"
  20. #include "list-objects-filter-options.h"
  21. #include "pack-objects.h"
  22. #include "progress.h"
  23. #include "refs.h"
  24. #include "streaming.h"
  25. #include "thread-utils.h"
  26. #include "pack-bitmap.h"
  27. #include "delta-islands.h"
  28. #include "reachable.h"
  29. #include "oid-array.h"
  30. #include "strvec.h"
  31. #include "list.h"
  32. #include "packfile.h"
  33. #include "object-store.h"
  34. #include "dir.h"
  35. #include "midx.h"
  36. #include "trace2.h"
  37. #include "shallow.h"
  38. #include "promisor-remote.h"
  39. #define IN_PACK(obj) oe_in_pack(&to_pack, obj)
  40. #define SIZE(obj) oe_size(&to_pack, obj)
  41. #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
  42. #define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
  43. #define DELTA(obj) oe_delta(&to_pack, obj)
  44. #define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
  45. #define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
  46. #define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
  47. #define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
  48. #define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
  49. #define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
  50. #define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
  51. static const char *pack_usage[] = {
  52. N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"),
  53. N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"),
  54. NULL
  55. };
  56. /*
  57. * Objects we are going to pack are collected in the `to_pack` structure.
  58. * It contains an array (dynamically expanded) of the object data, and a map
  59. * that can resolve SHA1s to their position in the array.
  60. */
  61. static struct packing_data to_pack;
  62. static struct pack_idx_entry **written_list;
  63. static uint32_t nr_result, nr_written, nr_seen;
  64. static struct bitmap_index *bitmap_git;
  65. static uint32_t write_layer;
  66. static int non_empty;
  67. static int reuse_delta = 1, reuse_object = 1;
  68. static int keep_unreachable, unpack_unreachable, include_tag;
  69. static timestamp_t unpack_unreachable_expiration;
  70. static int pack_loose_unreachable;
  71. static int local;
  72. static int have_non_local_packs;
  73. static int incremental;
  74. static int ignore_packed_keep_on_disk;
  75. static int ignore_packed_keep_in_core;
  76. static int allow_ofs_delta;
  77. static struct pack_idx_option pack_idx_opts;
  78. static const char *base_name;
  79. static int progress = 1;
  80. static int window = 10;
  81. static unsigned long pack_size_limit;
  82. static int depth = 50;
  83. static int delta_search_threads;
  84. static int pack_to_stdout;
  85. static int sparse;
  86. static int thin;
  87. static int num_preferred_base;
  88. static struct progress *progress_state;
  89. static struct packed_git *reuse_packfile;
  90. static uint32_t reuse_packfile_objects;
  91. static struct bitmap *reuse_packfile_bitmap;
  92. static int use_bitmap_index_default = 1;
  93. static int use_bitmap_index = -1;
  94. static int allow_pack_reuse = 1;
  95. static enum {
  96. WRITE_BITMAP_FALSE = 0,
  97. WRITE_BITMAP_QUIET,
  98. WRITE_BITMAP_TRUE,
  99. } write_bitmap_index;
  100. static uint16_t write_bitmap_options = BITMAP_OPT_HASH_CACHE;
  101. static int exclude_promisor_objects;
  102. static int use_delta_islands;
  103. static unsigned long delta_cache_size = 0;
  104. static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
  105. static unsigned long cache_max_small_delta_size = 1000;
  106. static unsigned long window_memory_limit = 0;
  107. static struct list_objects_filter_options filter_options;
  108. static struct string_list uri_protocols = STRING_LIST_INIT_NODUP;
  109. enum missing_action {
  110. MA_ERROR = 0, /* fail if any missing objects are encountered */
  111. MA_ALLOW_ANY, /* silently allow ALL missing objects */
  112. MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
  113. };
  114. static enum missing_action arg_missing_action;
  115. static show_object_fn fn_show_object;
  116. struct configured_exclusion {
  117. struct oidmap_entry e;
  118. char *pack_hash_hex;
  119. char *uri;
  120. };
  121. static struct oidmap configured_exclusions;
  122. static struct oidset excluded_by_config;
  123. /*
  124. * stats
  125. */
  126. static uint32_t written, written_delta;
  127. static uint32_t reused, reused_delta;
  128. /*
  129. * Indexed commits
  130. */
  131. static struct commit **indexed_commits;
  132. static unsigned int indexed_commits_nr;
  133. static unsigned int indexed_commits_alloc;
  134. static void index_commit_for_bitmap(struct commit *commit)
  135. {
  136. if (indexed_commits_nr >= indexed_commits_alloc) {
  137. indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
  138. REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
  139. }
  140. indexed_commits[indexed_commits_nr++] = commit;
  141. }
  142. static void *get_delta(struct object_entry *entry)
  143. {
  144. unsigned long size, base_size, delta_size;
  145. void *buf, *base_buf, *delta_buf;
  146. enum object_type type;
  147. buf = read_object_file(&entry->idx.oid, &type, &size);
  148. if (!buf)
  149. die(_("unable to read %s"), oid_to_hex(&entry->idx.oid));
  150. base_buf = read_object_file(&DELTA(entry)->idx.oid, &type,
  151. &base_size);
  152. if (!base_buf)
  153. die("unable to read %s",
  154. oid_to_hex(&DELTA(entry)->idx.oid));
  155. delta_buf = diff_delta(base_buf, base_size,
  156. buf, size, &delta_size, 0);
  157. /*
  158. * We successfully computed this delta once but dropped it for
  159. * memory reasons. Something is very wrong if this time we
  160. * recompute and create a different delta.
  161. */
  162. if (!delta_buf || delta_size != DELTA_SIZE(entry))
  163. BUG("delta size changed");
  164. free(buf);
  165. free(base_buf);
  166. return delta_buf;
  167. }
  168. static unsigned long do_compress(void **pptr, unsigned long size)
  169. {
  170. git_zstream stream;
  171. void *in, *out;
  172. unsigned long maxsize;
  173. git_deflate_init(&stream, pack_compression_level);
  174. maxsize = git_deflate_bound(&stream, size);
  175. in = *pptr;
  176. out = xmalloc(maxsize);
  177. *pptr = out;
  178. stream.next_in = in;
  179. stream.avail_in = size;
  180. stream.next_out = out;
  181. stream.avail_out = maxsize;
  182. while (git_deflate(&stream, Z_FINISH) == Z_OK)
  183. ; /* nothing */
  184. git_deflate_end(&stream);
  185. free(in);
  186. return stream.total_out;
  187. }
  188. static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f,
  189. const struct object_id *oid)
  190. {
  191. git_zstream stream;
  192. unsigned char ibuf[1024 * 16];
  193. unsigned char obuf[1024 * 16];
  194. unsigned long olen = 0;
  195. git_deflate_init(&stream, pack_compression_level);
  196. for (;;) {
  197. ssize_t readlen;
  198. int zret = Z_OK;
  199. readlen = read_istream(st, ibuf, sizeof(ibuf));
  200. if (readlen == -1)
  201. die(_("unable to read %s"), oid_to_hex(oid));
  202. stream.next_in = ibuf;
  203. stream.avail_in = readlen;
  204. while ((stream.avail_in || readlen == 0) &&
  205. (zret == Z_OK || zret == Z_BUF_ERROR)) {
  206. stream.next_out = obuf;
  207. stream.avail_out = sizeof(obuf);
  208. zret = git_deflate(&stream, readlen ? 0 : Z_FINISH);
  209. hashwrite(f, obuf, stream.next_out - obuf);
  210. olen += stream.next_out - obuf;
  211. }
  212. if (stream.avail_in)
  213. die(_("deflate error (%d)"), zret);
  214. if (readlen == 0) {
  215. if (zret != Z_STREAM_END)
  216. die(_("deflate error (%d)"), zret);
  217. break;
  218. }
  219. }
  220. git_deflate_end(&stream);
  221. return olen;
  222. }
  223. /*
  224. * we are going to reuse the existing object data as is. make
  225. * sure it is not corrupt.
  226. */
  227. static int check_pack_inflate(struct packed_git *p,
  228. struct pack_window **w_curs,
  229. off_t offset,
  230. off_t len,
  231. unsigned long expect)
  232. {
  233. git_zstream stream;
  234. unsigned char fakebuf[4096], *in;
  235. int st;
  236. memset(&stream, 0, sizeof(stream));
  237. git_inflate_init(&stream);
  238. do {
  239. in = use_pack(p, w_curs, offset, &stream.avail_in);
  240. stream.next_in = in;
  241. stream.next_out = fakebuf;
  242. stream.avail_out = sizeof(fakebuf);
  243. st = git_inflate(&stream, Z_FINISH);
  244. offset += stream.next_in - in;
  245. } while (st == Z_OK || st == Z_BUF_ERROR);
  246. git_inflate_end(&stream);
  247. return (st == Z_STREAM_END &&
  248. stream.total_out == expect &&
  249. stream.total_in == len) ? 0 : -1;
  250. }
  251. static void copy_pack_data(struct hashfile *f,
  252. struct packed_git *p,
  253. struct pack_window **w_curs,
  254. off_t offset,
  255. off_t len)
  256. {
  257. unsigned char *in;
  258. unsigned long avail;
  259. while (len) {
  260. in = use_pack(p, w_curs, offset, &avail);
  261. if (avail > len)
  262. avail = (unsigned long)len;
  263. hashwrite(f, in, avail);
  264. offset += avail;
  265. len -= avail;
  266. }
  267. }
  268. /* Return 0 if we will bust the pack-size limit */
  269. static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry,
  270. unsigned long limit, int usable_delta)
  271. {
  272. unsigned long size, datalen;
  273. unsigned char header[MAX_PACK_OBJECT_HEADER],
  274. dheader[MAX_PACK_OBJECT_HEADER];
  275. unsigned hdrlen;
  276. enum object_type type;
  277. void *buf;
  278. struct git_istream *st = NULL;
  279. const unsigned hashsz = the_hash_algo->rawsz;
  280. if (!usable_delta) {
  281. if (oe_type(entry) == OBJ_BLOB &&
  282. oe_size_greater_than(&to_pack, entry, big_file_threshold) &&
  283. (st = open_istream(the_repository, &entry->idx.oid, &type,
  284. &size, NULL)) != NULL)
  285. buf = NULL;
  286. else {
  287. buf = read_object_file(&entry->idx.oid, &type, &size);
  288. if (!buf)
  289. die(_("unable to read %s"),
  290. oid_to_hex(&entry->idx.oid));
  291. }
  292. /*
  293. * make sure no cached delta data remains from a
  294. * previous attempt before a pack split occurred.
  295. */
  296. FREE_AND_NULL(entry->delta_data);
  297. entry->z_delta_size = 0;
  298. } else if (entry->delta_data) {
  299. size = DELTA_SIZE(entry);
  300. buf = entry->delta_data;
  301. entry->delta_data = NULL;
  302. type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
  303. OBJ_OFS_DELTA : OBJ_REF_DELTA;
  304. } else {
  305. buf = get_delta(entry);
  306. size = DELTA_SIZE(entry);
  307. type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
  308. OBJ_OFS_DELTA : OBJ_REF_DELTA;
  309. }
  310. if (st) /* large blob case, just assume we don't compress well */
  311. datalen = size;
  312. else if (entry->z_delta_size)
  313. datalen = entry->z_delta_size;
  314. else
  315. datalen = do_compress(&buf, size);
  316. /*
  317. * The object header is a byte of 'type' followed by zero or
  318. * more bytes of length.
  319. */
  320. hdrlen = encode_in_pack_object_header(header, sizeof(header),
  321. type, size);
  322. if (type == OBJ_OFS_DELTA) {
  323. /*
  324. * Deltas with relative base contain an additional
  325. * encoding of the relative offset for the delta
  326. * base from this object's position in the pack.
  327. */
  328. off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
  329. unsigned pos = sizeof(dheader) - 1;
  330. dheader[pos] = ofs & 127;
  331. while (ofs >>= 7)
  332. dheader[--pos] = 128 | (--ofs & 127);
  333. if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
  334. if (st)
  335. close_istream(st);
  336. free(buf);
  337. return 0;
  338. }
  339. hashwrite(f, header, hdrlen);
  340. hashwrite(f, dheader + pos, sizeof(dheader) - pos);
  341. hdrlen += sizeof(dheader) - pos;
  342. } else if (type == OBJ_REF_DELTA) {
  343. /*
  344. * Deltas with a base reference contain
  345. * additional bytes for the base object ID.
  346. */
  347. if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
  348. if (st)
  349. close_istream(st);
  350. free(buf);
  351. return 0;
  352. }
  353. hashwrite(f, header, hdrlen);
  354. hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
  355. hdrlen += hashsz;
  356. } else {
  357. if (limit && hdrlen + datalen + hashsz >= limit) {
  358. if (st)
  359. close_istream(st);
  360. free(buf);
  361. return 0;
  362. }
  363. hashwrite(f, header, hdrlen);
  364. }
  365. if (st) {
  366. datalen = write_large_blob_data(st, f, &entry->idx.oid);
  367. close_istream(st);
  368. } else {
  369. hashwrite(f, buf, datalen);
  370. free(buf);
  371. }
  372. return hdrlen + datalen;
  373. }
  374. /* Return 0 if we will bust the pack-size limit */
  375. static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
  376. unsigned long limit, int usable_delta)
  377. {
  378. struct packed_git *p = IN_PACK(entry);
  379. struct pack_window *w_curs = NULL;
  380. struct revindex_entry *revidx;
  381. off_t offset;
  382. enum object_type type = oe_type(entry);
  383. off_t datalen;
  384. unsigned char header[MAX_PACK_OBJECT_HEADER],
  385. dheader[MAX_PACK_OBJECT_HEADER];
  386. unsigned hdrlen;
  387. const unsigned hashsz = the_hash_algo->rawsz;
  388. unsigned long entry_size = SIZE(entry);
  389. if (DELTA(entry))
  390. type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
  391. OBJ_OFS_DELTA : OBJ_REF_DELTA;
  392. hdrlen = encode_in_pack_object_header(header, sizeof(header),
  393. type, entry_size);
  394. offset = entry->in_pack_offset;
  395. revidx = find_pack_revindex(p, offset);
  396. datalen = revidx[1].offset - offset;
  397. if (!pack_to_stdout && p->index_version > 1 &&
  398. check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) {
  399. error(_("bad packed object CRC for %s"),
  400. oid_to_hex(&entry->idx.oid));
  401. unuse_pack(&w_curs);
  402. return write_no_reuse_object(f, entry, limit, usable_delta);
  403. }
  404. offset += entry->in_pack_header_size;
  405. datalen -= entry->in_pack_header_size;
  406. if (!pack_to_stdout && p->index_version == 1 &&
  407. check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) {
  408. error(_("corrupt packed object for %s"),
  409. oid_to_hex(&entry->idx.oid));
  410. unuse_pack(&w_curs);
  411. return write_no_reuse_object(f, entry, limit, usable_delta);
  412. }
  413. if (type == OBJ_OFS_DELTA) {
  414. off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
  415. unsigned pos = sizeof(dheader) - 1;
  416. dheader[pos] = ofs & 127;
  417. while (ofs >>= 7)
  418. dheader[--pos] = 128 | (--ofs & 127);
  419. if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
  420. unuse_pack(&w_curs);
  421. return 0;
  422. }
  423. hashwrite(f, header, hdrlen);
  424. hashwrite(f, dheader + pos, sizeof(dheader) - pos);
  425. hdrlen += sizeof(dheader) - pos;
  426. reused_delta++;
  427. } else if (type == OBJ_REF_DELTA) {
  428. if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
  429. unuse_pack(&w_curs);
  430. return 0;
  431. }
  432. hashwrite(f, header, hdrlen);
  433. hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
  434. hdrlen += hashsz;
  435. reused_delta++;
  436. } else {
  437. if (limit && hdrlen + datalen + hashsz >= limit) {
  438. unuse_pack(&w_curs);
  439. return 0;
  440. }
  441. hashwrite(f, header, hdrlen);
  442. }
  443. copy_pack_data(f, p, &w_curs, offset, datalen);
  444. unuse_pack(&w_curs);
  445. reused++;
  446. return hdrlen + datalen;
  447. }
  448. /* Return 0 if we will bust the pack-size limit */
  449. static off_t write_object(struct hashfile *f,
  450. struct object_entry *entry,
  451. off_t write_offset)
  452. {
  453. unsigned long limit;
  454. off_t len;
  455. int usable_delta, to_reuse;
  456. if (!pack_to_stdout)
  457. crc32_begin(f);
  458. /* apply size limit if limited packsize and not first object */
  459. if (!pack_size_limit || !nr_written)
  460. limit = 0;
  461. else if (pack_size_limit <= write_offset)
  462. /*
  463. * the earlier object did not fit the limit; avoid
  464. * mistaking this with unlimited (i.e. limit = 0).
  465. */
  466. limit = 1;
  467. else
  468. limit = pack_size_limit - write_offset;
  469. if (!DELTA(entry))
  470. usable_delta = 0; /* no delta */
  471. else if (!pack_size_limit)
  472. usable_delta = 1; /* unlimited packfile */
  473. else if (DELTA(entry)->idx.offset == (off_t)-1)
  474. usable_delta = 0; /* base was written to another pack */
  475. else if (DELTA(entry)->idx.offset)
  476. usable_delta = 1; /* base already exists in this pack */
  477. else
  478. usable_delta = 0; /* base could end up in another pack */
  479. if (!reuse_object)
  480. to_reuse = 0; /* explicit */
  481. else if (!IN_PACK(entry))
  482. to_reuse = 0; /* can't reuse what we don't have */
  483. else if (oe_type(entry) == OBJ_REF_DELTA ||
  484. oe_type(entry) == OBJ_OFS_DELTA)
  485. /* check_object() decided it for us ... */
  486. to_reuse = usable_delta;
  487. /* ... but pack split may override that */
  488. else if (oe_type(entry) != entry->in_pack_type)
  489. to_reuse = 0; /* pack has delta which is unusable */
  490. else if (DELTA(entry))
  491. to_reuse = 0; /* we want to pack afresh */
  492. else
  493. to_reuse = 1; /* we have it in-pack undeltified,
  494. * and we do not need to deltify it.
  495. */
  496. if (!to_reuse)
  497. len = write_no_reuse_object(f, entry, limit, usable_delta);
  498. else
  499. len = write_reuse_object(f, entry, limit, usable_delta);
  500. if (!len)
  501. return 0;
  502. if (usable_delta)
  503. written_delta++;
  504. written++;
  505. if (!pack_to_stdout)
  506. entry->idx.crc32 = crc32_end(f);
  507. return len;
  508. }
  509. enum write_one_status {
  510. WRITE_ONE_SKIP = -1, /* already written */
  511. WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
  512. WRITE_ONE_WRITTEN = 1, /* normal */
  513. WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
  514. };
  515. static enum write_one_status write_one(struct hashfile *f,
  516. struct object_entry *e,
  517. off_t *offset)
  518. {
  519. off_t size;
  520. int recursing;
  521. /*
  522. * we set offset to 1 (which is an impossible value) to mark
  523. * the fact that this object is involved in "write its base
  524. * first before writing a deltified object" recursion.
  525. */
  526. recursing = (e->idx.offset == 1);
  527. if (recursing) {
  528. warning(_("recursive delta detected for object %s"),
  529. oid_to_hex(&e->idx.oid));
  530. return WRITE_ONE_RECURSIVE;
  531. } else if (e->idx.offset || e->preferred_base) {
  532. /* offset is non zero if object is written already. */
  533. return WRITE_ONE_SKIP;
  534. }
  535. /* if we are deltified, write out base object first. */
  536. if (DELTA(e)) {
  537. e->idx.offset = 1; /* now recurse */
  538. switch (write_one(f, DELTA(e), offset)) {
  539. case WRITE_ONE_RECURSIVE:
  540. /* we cannot depend on this one */
  541. SET_DELTA(e, NULL);
  542. break;
  543. default:
  544. break;
  545. case WRITE_ONE_BREAK:
  546. e->idx.offset = recursing;
  547. return WRITE_ONE_BREAK;
  548. }
  549. }
  550. e->idx.offset = *offset;
  551. size = write_object(f, e, *offset);
  552. if (!size) {
  553. e->idx.offset = recursing;
  554. return WRITE_ONE_BREAK;
  555. }
  556. written_list[nr_written++] = &e->idx;
  557. /* make sure off_t is sufficiently large not to wrap */
  558. if (signed_add_overflows(*offset, size))
  559. die(_("pack too large for current definition of off_t"));
  560. *offset += size;
  561. return WRITE_ONE_WRITTEN;
  562. }
  563. static int mark_tagged(const char *path, const struct object_id *oid, int flag,
  564. void *cb_data)
  565. {
  566. struct object_id peeled;
  567. struct object_entry *entry = packlist_find(&to_pack, oid);
  568. if (entry)
  569. entry->tagged = 1;
  570. if (!peel_ref(path, &peeled)) {
  571. entry = packlist_find(&to_pack, &peeled);
  572. if (entry)
  573. entry->tagged = 1;
  574. }
  575. return 0;
  576. }
  577. static inline void add_to_write_order(struct object_entry **wo,
  578. unsigned int *endp,
  579. struct object_entry *e)
  580. {
  581. if (e->filled || oe_layer(&to_pack, e) != write_layer)
  582. return;
  583. wo[(*endp)++] = e;
  584. e->filled = 1;
  585. }
  586. static void add_descendants_to_write_order(struct object_entry **wo,
  587. unsigned int *endp,
  588. struct object_entry *e)
  589. {
  590. int add_to_order = 1;
  591. while (e) {
  592. if (add_to_order) {
  593. struct object_entry *s;
  594. /* add this node... */
  595. add_to_write_order(wo, endp, e);
  596. /* all its siblings... */
  597. for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) {
  598. add_to_write_order(wo, endp, s);
  599. }
  600. }
  601. /* drop down a level to add left subtree nodes if possible */
  602. if (DELTA_CHILD(e)) {
  603. add_to_order = 1;
  604. e = DELTA_CHILD(e);
  605. } else {
  606. add_to_order = 0;
  607. /* our sibling might have some children, it is next */
  608. if (DELTA_SIBLING(e)) {
  609. e = DELTA_SIBLING(e);
  610. continue;
  611. }
  612. /* go back to our parent node */
  613. e = DELTA(e);
  614. while (e && !DELTA_SIBLING(e)) {
  615. /* we're on the right side of a subtree, keep
  616. * going up until we can go right again */
  617. e = DELTA(e);
  618. }
  619. if (!e) {
  620. /* done- we hit our original root node */
  621. return;
  622. }
  623. /* pass it off to sibling at this level */
  624. e = DELTA_SIBLING(e);
  625. }
  626. };
  627. }
  628. static void add_family_to_write_order(struct object_entry **wo,
  629. unsigned int *endp,
  630. struct object_entry *e)
  631. {
  632. struct object_entry *root;
  633. for (root = e; DELTA(root); root = DELTA(root))
  634. ; /* nothing */
  635. add_descendants_to_write_order(wo, endp, root);
  636. }
  637. static void compute_layer_order(struct object_entry **wo, unsigned int *wo_end)
  638. {
  639. unsigned int i, last_untagged;
  640. struct object_entry *objects = to_pack.objects;
  641. for (i = 0; i < to_pack.nr_objects; i++) {
  642. if (objects[i].tagged)
  643. break;
  644. add_to_write_order(wo, wo_end, &objects[i]);
  645. }
  646. last_untagged = i;
  647. /*
  648. * Then fill all the tagged tips.
  649. */
  650. for (; i < to_pack.nr_objects; i++) {
  651. if (objects[i].tagged)
  652. add_to_write_order(wo, wo_end, &objects[i]);
  653. }
  654. /*
  655. * And then all remaining commits and tags.
  656. */
  657. for (i = last_untagged; i < to_pack.nr_objects; i++) {
  658. if (oe_type(&objects[i]) != OBJ_COMMIT &&
  659. oe_type(&objects[i]) != OBJ_TAG)
  660. continue;
  661. add_to_write_order(wo, wo_end, &objects[i]);
  662. }
  663. /*
  664. * And then all the trees.
  665. */
  666. for (i = last_untagged; i < to_pack.nr_objects; i++) {
  667. if (oe_type(&objects[i]) != OBJ_TREE)
  668. continue;
  669. add_to_write_order(wo, wo_end, &objects[i]);
  670. }
  671. /*
  672. * Finally all the rest in really tight order
  673. */
  674. for (i = last_untagged; i < to_pack.nr_objects; i++) {
  675. if (!objects[i].filled && oe_layer(&to_pack, &objects[i]) == write_layer)
  676. add_family_to_write_order(wo, wo_end, &objects[i]);
  677. }
  678. }
  679. static struct object_entry **compute_write_order(void)
  680. {
  681. uint32_t max_layers = 1;
  682. unsigned int i, wo_end;
  683. struct object_entry **wo;
  684. struct object_entry *objects = to_pack.objects;
  685. for (i = 0; i < to_pack.nr_objects; i++) {
  686. objects[i].tagged = 0;
  687. objects[i].filled = 0;
  688. SET_DELTA_CHILD(&objects[i], NULL);
  689. SET_DELTA_SIBLING(&objects[i], NULL);
  690. }
  691. /*
  692. * Fully connect delta_child/delta_sibling network.
  693. * Make sure delta_sibling is sorted in the original
  694. * recency order.
  695. */
  696. for (i = to_pack.nr_objects; i > 0;) {
  697. struct object_entry *e = &objects[--i];
  698. if (!DELTA(e))
  699. continue;
  700. /* Mark me as the first child */
  701. e->delta_sibling_idx = DELTA(e)->delta_child_idx;
  702. SET_DELTA_CHILD(DELTA(e), e);
  703. }
  704. /*
  705. * Mark objects that are at the tip of tags.
  706. */
  707. for_each_tag_ref(mark_tagged, NULL);
  708. if (use_delta_islands)
  709. max_layers = compute_pack_layers(&to_pack);
  710. ALLOC_ARRAY(wo, to_pack.nr_objects);
  711. wo_end = 0;
  712. for (; write_layer < max_layers; ++write_layer)
  713. compute_layer_order(wo, &wo_end);
  714. if (wo_end != to_pack.nr_objects)
  715. die(_("ordered %u objects, expected %"PRIu32),
  716. wo_end, to_pack.nr_objects);
  717. return wo;
  718. }
  719. /*
  720. * A reused set of objects. All objects in a chunk have the same
  721. * relative position in the original packfile and the generated
  722. * packfile.
  723. */
  724. static struct reused_chunk {
  725. /* The offset of the first object of this chunk in the original
  726. * packfile. */
  727. off_t original;
  728. /* The offset of the first object of this chunk in the generated
  729. * packfile minus "original". */
  730. off_t difference;
  731. } *reused_chunks;
  732. static int reused_chunks_nr;
  733. static int reused_chunks_alloc;
  734. static void record_reused_object(off_t where, off_t offset)
  735. {
  736. if (reused_chunks_nr && reused_chunks[reused_chunks_nr-1].difference == offset)
  737. return;
  738. ALLOC_GROW(reused_chunks, reused_chunks_nr + 1,
  739. reused_chunks_alloc);
  740. reused_chunks[reused_chunks_nr].original = where;
  741. reused_chunks[reused_chunks_nr].difference = offset;
  742. reused_chunks_nr++;
  743. }
  744. /*
  745. * Binary search to find the chunk that "where" is in. Note
  746. * that we're not looking for an exact match, just the first
  747. * chunk that contains it (which implicitly ends at the start
  748. * of the next chunk.
  749. */
  750. static off_t find_reused_offset(off_t where)
  751. {
  752. int lo = 0, hi = reused_chunks_nr;
  753. while (lo < hi) {
  754. int mi = lo + ((hi - lo) / 2);
  755. if (where == reused_chunks[mi].original)
  756. return reused_chunks[mi].difference;
  757. if (where < reused_chunks[mi].original)
  758. hi = mi;
  759. else
  760. lo = mi + 1;
  761. }
  762. /*
  763. * The first chunk starts at zero, so we can't have gone below
  764. * there.
  765. */
  766. assert(lo);
  767. return reused_chunks[lo-1].difference;
  768. }
  769. static void write_reused_pack_one(size_t pos, struct hashfile *out,
  770. struct pack_window **w_curs)
  771. {
  772. off_t offset, next, cur;
  773. enum object_type type;
  774. unsigned long size;
  775. offset = reuse_packfile->revindex[pos].offset;
  776. next = reuse_packfile->revindex[pos + 1].offset;
  777. record_reused_object(offset, offset - hashfile_total(out));
  778. cur = offset;
  779. type = unpack_object_header(reuse_packfile, w_curs, &cur, &size);
  780. assert(type >= 0);
  781. if (type == OBJ_OFS_DELTA) {
  782. off_t base_offset;
  783. off_t fixup;
  784. unsigned char header[MAX_PACK_OBJECT_HEADER];
  785. unsigned len;
  786. base_offset = get_delta_base(reuse_packfile, w_curs, &cur, type, offset);
  787. assert(base_offset != 0);
  788. /* Convert to REF_DELTA if we must... */
  789. if (!allow_ofs_delta) {
  790. int base_pos = find_revindex_position(reuse_packfile, base_offset);
  791. struct object_id base_oid;
  792. nth_packed_object_id(&base_oid, reuse_packfile,
  793. reuse_packfile->revindex[base_pos].nr);
  794. len = encode_in_pack_object_header(header, sizeof(header),
  795. OBJ_REF_DELTA, size);
  796. hashwrite(out, header, len);
  797. hashwrite(out, base_oid.hash, the_hash_algo->rawsz);
  798. copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
  799. return;
  800. }
  801. /* Otherwise see if we need to rewrite the offset... */
  802. fixup = find_reused_offset(offset) -
  803. find_reused_offset(base_offset);
  804. if (fixup) {
  805. unsigned char ofs_header[10];
  806. unsigned i, ofs_len;
  807. off_t ofs = offset - base_offset - fixup;
  808. len = encode_in_pack_object_header(header, sizeof(header),
  809. OBJ_OFS_DELTA, size);
  810. i = sizeof(ofs_header) - 1;
  811. ofs_header[i] = ofs & 127;
  812. while (ofs >>= 7)
  813. ofs_header[--i] = 128 | (--ofs & 127);
  814. ofs_len = sizeof(ofs_header) - i;
  815. hashwrite(out, header, len);
  816. hashwrite(out, ofs_header + sizeof(ofs_header) - ofs_len, ofs_len);
  817. copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
  818. return;
  819. }
  820. /* ...otherwise we have no fixup, and can write it verbatim */
  821. }
  822. copy_pack_data(out, reuse_packfile, w_curs, offset, next - offset);
  823. }
  824. static size_t write_reused_pack_verbatim(struct hashfile *out,
  825. struct pack_window **w_curs)
  826. {
  827. size_t pos = 0;
  828. while (pos < reuse_packfile_bitmap->word_alloc &&
  829. reuse_packfile_bitmap->words[pos] == (eword_t)~0)
  830. pos++;
  831. if (pos) {
  832. off_t to_write;
  833. written = (pos * BITS_IN_EWORD);
  834. to_write = reuse_packfile->revindex[written].offset
  835. - sizeof(struct pack_header);
  836. /* We're recording one chunk, not one object. */
  837. record_reused_object(sizeof(struct pack_header), 0);
  838. hashflush(out);
  839. copy_pack_data(out, reuse_packfile, w_curs,
  840. sizeof(struct pack_header), to_write);
  841. display_progress(progress_state, written);
  842. }
  843. return pos;
  844. }
  845. static void write_reused_pack(struct hashfile *f)
  846. {
  847. size_t i = 0;
  848. uint32_t offset;
  849. struct pack_window *w_curs = NULL;
  850. if (allow_ofs_delta)
  851. i = write_reused_pack_verbatim(f, &w_curs);
  852. for (; i < reuse_packfile_bitmap->word_alloc; ++i) {
  853. eword_t word = reuse_packfile_bitmap->words[i];
  854. size_t pos = (i * BITS_IN_EWORD);
  855. for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
  856. if ((word >> offset) == 0)
  857. break;
  858. offset += ewah_bit_ctz64(word >> offset);
  859. write_reused_pack_one(pos + offset, f, &w_curs);
  860. display_progress(progress_state, ++written);
  861. }
  862. }
  863. unuse_pack(&w_curs);
  864. }
  865. static void write_excluded_by_configs(void)
  866. {
  867. struct oidset_iter iter;
  868. const struct object_id *oid;
  869. oidset_iter_init(&excluded_by_config, &iter);
  870. while ((oid = oidset_iter_next(&iter))) {
  871. struct configured_exclusion *ex =
  872. oidmap_get(&configured_exclusions, oid);
  873. if (!ex)
  874. BUG("configured exclusion wasn't configured");
  875. write_in_full(1, ex->pack_hash_hex, strlen(ex->pack_hash_hex));
  876. write_in_full(1, " ", 1);
  877. write_in_full(1, ex->uri, strlen(ex->uri));
  878. write_in_full(1, "\n", 1);
  879. }
  880. }
  881. static const char no_split_warning[] = N_(
  882. "disabling bitmap writing, packs are split due to pack.packSizeLimit"
  883. );
  884. static void write_pack_file(void)
  885. {
  886. uint32_t i = 0, j;
  887. struct hashfile *f;
  888. off_t offset;
  889. uint32_t nr_remaining = nr_result;
  890. time_t last_mtime = 0;
  891. struct object_entry **write_order;
  892. if (progress > pack_to_stdout)
  893. progress_state = start_progress(_("Writing objects"), nr_result);
  894. ALLOC_ARRAY(written_list, to_pack.nr_objects);
  895. write_order = compute_write_order();
  896. do {
  897. struct object_id oid;
  898. char *pack_tmp_name = NULL;
  899. if (pack_to_stdout)
  900. f = hashfd_throughput(1, "<stdout>", progress_state);
  901. else
  902. f = create_tmp_packfile(&pack_tmp_name);
  903. offset = write_pack_header(f, nr_remaining);
  904. if (reuse_packfile) {
  905. assert(pack_to_stdout);
  906. write_reused_pack(f);
  907. offset = hashfile_total(f);
  908. }
  909. nr_written = 0;
  910. for (; i < to_pack.nr_objects; i++) {
  911. struct object_entry *e = write_order[i];
  912. if (write_one(f, e, &offset) == WRITE_ONE_BREAK)
  913. break;
  914. display_progress(progress_state, written);
  915. }
  916. /*
  917. * Did we write the wrong # entries in the header?
  918. * If so, rewrite it like in fast-import
  919. */
  920. if (pack_to_stdout) {
  921. finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE);
  922. } else if (nr_written == nr_remaining) {
  923. finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
  924. } else {
  925. int fd = finalize_hashfile(f, oid.hash, 0);
  926. fixup_pack_header_footer(fd, oid.hash, pack_tmp_name,
  927. nr_written, oid.hash, offset);
  928. close(fd);
  929. if (write_bitmap_index) {
  930. if (write_bitmap_index != WRITE_BITMAP_QUIET)
  931. warning(_(no_split_warning));
  932. write_bitmap_index = 0;
  933. }
  934. }
  935. if (!pack_to_stdout) {
  936. struct stat st;
  937. struct strbuf tmpname = STRBUF_INIT;
  938. /*
  939. * Packs are runtime accessed in their mtime
  940. * order since newer packs are more likely to contain
  941. * younger objects. So if we are creating multiple
  942. * packs then we should modify the mtime of later ones
  943. * to preserve this property.
  944. */
  945. if (stat(pack_tmp_name, &st) < 0) {
  946. warning_errno(_("failed to stat %s"), pack_tmp_name);
  947. } else if (!last_mtime) {
  948. last_mtime = st.st_mtime;
  949. } else {
  950. struct utimbuf utb;
  951. utb.actime = st.st_atime;
  952. utb.modtime = --last_mtime;
  953. if (utime(pack_tmp_name, &utb) < 0)
  954. warning_errno(_("failed utime() on %s"), pack_tmp_name);
  955. }
  956. strbuf_addf(&tmpname, "%s-", base_name);
  957. if (write_bitmap_index) {
  958. bitmap_writer_set_checksum(oid.hash);
  959. bitmap_writer_build_type_index(
  960. &to_pack, written_list, nr_written);
  961. }
  962. finish_tmp_packfile(&tmpname, pack_tmp_name,
  963. written_list, nr_written,
  964. &pack_idx_opts, oid.hash);
  965. if (write_bitmap_index) {
  966. strbuf_addf(&tmpname, "%s.bitmap", oid_to_hex(&oid));
  967. stop_progress(&progress_state);
  968. bitmap_writer_show_progress(progress);
  969. bitmap_writer_reuse_bitmaps(&to_pack);
  970. bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);
  971. bitmap_writer_build(&to_pack);
  972. bitmap_writer_finish(written_list, nr_written,
  973. tmpname.buf, write_bitmap_options);
  974. write_bitmap_index = 0;
  975. }
  976. strbuf_release(&tmpname);
  977. free(pack_tmp_name);
  978. puts(oid_to_hex(&oid));
  979. }
  980. /* mark written objects as written to previous pack */
  981. for (j = 0; j < nr_written; j++) {
  982. written_list[j]->offset = (off_t)-1;
  983. }
  984. nr_remaining -= nr_written;
  985. } while (nr_remaining && i < to_pack.nr_objects);
  986. free(written_list);
  987. free(write_order);
  988. stop_progress(&progress_state);
  989. if (written != nr_result)
  990. die(_("wrote %"PRIu32" objects while expecting %"PRIu32),
  991. written, nr_result);
  992. trace2_data_intmax("pack-objects", the_repository,
  993. "write_pack_file/wrote", nr_result);
  994. }
  995. static int no_try_delta(const char *path)
  996. {
  997. static struct attr_check *check;
  998. if (!check)
  999. check = attr_check_initl("delta", NULL);
  1000. git_check_attr(the_repository->index, path, check);
  1001. if (ATTR_FALSE(check->items[0].value))
  1002. return 1;
  1003. return 0;
  1004. }
  1005. /*
  1006. * When adding an object, check whether we have already added it
  1007. * to our packing list. If so, we can skip. However, if we are
  1008. * being asked to excludei t, but the previous mention was to include
  1009. * it, make sure to adjust its flags and tweak our numbers accordingly.
  1010. *
  1011. * As an optimization, we pass out the index position where we would have
  1012. * found the item, since that saves us from having to look it up again a
  1013. * few lines later when we want to add the new entry.
  1014. */
  1015. static int have_duplicate_entry(const struct object_id *oid,
  1016. int exclude)
  1017. {
  1018. struct object_entry *entry;
  1019. if (reuse_packfile_bitmap &&
  1020. bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid))
  1021. return 1;
  1022. entry = packlist_find(&to_pack, oid);
  1023. if (!entry)
  1024. return 0;
  1025. if (exclude) {
  1026. if (!entry->preferred_base)
  1027. nr_result--;
  1028. entry->preferred_base = 1;
  1029. }
  1030. return 1;
  1031. }
  1032. static int want_found_object(int exclude, struct packed_git *p)
  1033. {
  1034. if (exclude)
  1035. return 1;
  1036. if (incremental)
  1037. return 0;
  1038. /*
  1039. * When asked to do --local (do not include an object that appears in a
  1040. * pack we borrow from elsewhere) or --honor-pack-keep (do not include
  1041. * an object that appears in a pack marked with .keep), finding a pack
  1042. * that matches the criteria is sufficient for us to decide to omit it.
  1043. * However, even if this pack does not satisfy the criteria, we need to
  1044. * make sure no copy of this object appears in _any_ pack that makes us
  1045. * to omit the object, so we need to check all the packs.
  1046. *
  1047. * We can however first check whether these options can possible matter;
  1048. * if they do not matter we know we want the object in generated pack.
  1049. * Otherwise, we signal "-1" at the end to tell the caller that we do
  1050. * not know either way, and it needs to check more packs.
  1051. */
  1052. if (!ignore_packed_keep_on_disk &&
  1053. !ignore_packed_keep_in_core &&
  1054. (!local || !have_non_local_packs))
  1055. return 1;
  1056. if (local && !p->pack_local)
  1057. return 0;
  1058. if (p->pack_local &&
  1059. ((ignore_packed_keep_on_disk && p->pack_keep) ||
  1060. (ignore_packed_keep_in_core && p->pack_keep_in_core)))
  1061. return 0;
  1062. /* we don't know yet; keep looking for more packs */
  1063. return -1;
  1064. }
  1065. /*
  1066. * Check whether we want the object in the pack (e.g., we do not want
  1067. * objects found in non-local stores if the "--local" option was used).
  1068. *
  1069. * If the caller already knows an existing pack it wants to take the object
  1070. * from, that is passed in *found_pack and *found_offset; otherwise this
  1071. * function finds if there is any pack that has the object and returns the pack
  1072. * and its offset in these variables.
  1073. */
  1074. static int want_object_in_pack(const struct object_id *oid,
  1075. int exclude,
  1076. struct packed_git **found_pack,
  1077. off_t *found_offset)
  1078. {
  1079. int want;
  1080. struct list_head *pos;
  1081. struct multi_pack_index *m;
  1082. if (!exclude && local && has_loose_object_nonlocal(oid))
  1083. return 0;
  1084. /*
  1085. * If we already know the pack object lives in, start checks from that
  1086. * pack - in the usual case when neither --local was given nor .keep files
  1087. * are present we will determine the answer right now.
  1088. */
  1089. if (*found_pack) {
  1090. want = want_found_object(exclude, *found_pack);
  1091. if (want != -1)
  1092. return want;
  1093. }
  1094. for (m = get_multi_pack_index(the_repository); m; m = m->next) {
  1095. struct pack_entry e;
  1096. if (fill_midx_entry(the_repository, oid, &e, m)) {
  1097. struct packed_git *p = e.p;
  1098. off_t offset;
  1099. if (p == *found_pack)
  1100. offset = *found_offset;
  1101. else
  1102. offset = find_pack_entry_one(oid->hash, p);
  1103. if (offset) {
  1104. if (!*found_pack) {
  1105. if (!is_pack_valid(p))
  1106. continue;
  1107. *found_offset = offset;
  1108. *found_pack = p;
  1109. }
  1110. want = want_found_object(exclude, p);
  1111. if (want != -1)
  1112. return want;
  1113. }
  1114. }
  1115. }
  1116. list_for_each(pos, get_packed_git_mru(the_repository)) {
  1117. struct packed_git *p = list_entry(pos, struct packed_git, mru);
  1118. off_t offset;
  1119. if (p == *found_pack)
  1120. offset = *found_offset;
  1121. else
  1122. offset = find_pack_entry_one(oid->hash, p);
  1123. if (offset) {
  1124. if (!*found_pack) {
  1125. if (!is_pack_valid(p))
  1126. continue;
  1127. *found_offset = offset;
  1128. *found_pack = p;
  1129. }
  1130. want = want_found_object(exclude, p);
  1131. if (!exclude && want > 0)
  1132. list_move(&p->mru,
  1133. get_packed_git_mru(the_repository));
  1134. if (want != -1)
  1135. return want;
  1136. }
  1137. }
  1138. if (uri_protocols.nr) {
  1139. struct configured_exclusion *ex =
  1140. oidmap_get(&configured_exclusions, oid);
  1141. int i;
  1142. const char *p;
  1143. if (ex) {
  1144. for (i = 0; i < uri_protocols.nr; i++) {
  1145. if (skip_prefix(ex->uri,
  1146. uri_protocols.items[i].string,
  1147. &p) &&
  1148. *p == ':') {
  1149. oidset_insert(&excluded_by_config, oid);
  1150. return 0;
  1151. }
  1152. }
  1153. }
  1154. }
  1155. return 1;
  1156. }
  1157. static void create_object_entry(const struct object_id *oid,
  1158. enum object_type type,
  1159. uint32_t hash,
  1160. int exclude,
  1161. int no_try_delta,
  1162. struct packed_git *found_pack,
  1163. off_t found_offset)
  1164. {
  1165. struct object_entry *entry;
  1166. entry = packlist_alloc(&to_pack, oid);
  1167. entry->hash = hash;
  1168. oe_set_type(entry, type);
  1169. if (exclude)
  1170. entry->preferred_base = 1;
  1171. else
  1172. nr_result++;
  1173. if (found_pack) {
  1174. oe_set_in_pack(&to_pack, entry, found_pack);
  1175. entry->in_pack_offset = found_offset;
  1176. }
  1177. entry->no_try_delta = no_try_delta;
  1178. }
  1179. static const char no_closure_warning[] = N_(
  1180. "disabling bitmap writing, as some objects are not being packed"
  1181. );
  1182. static int add_object_entry(const struct object_id *oid, enum object_type type,
  1183. const char *name, int exclude)
  1184. {
  1185. struct packed_git *found_pack = NULL;
  1186. off_t found_offset = 0;
  1187. display_progress(progress_state, ++nr_seen);
  1188. if (have_duplicate_entry(oid, exclude))
  1189. return 0;
  1190. if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {
  1191. /* The pack is missing an object, so it will not have closure */
  1192. if (write_bitmap_index) {
  1193. if (write_bitmap_index != WRITE_BITMAP_QUIET)
  1194. warning(_(no_closure_warning));
  1195. write_bitmap_index = 0;
  1196. }
  1197. return 0;
  1198. }
  1199. create_object_entry(oid, type, pack_name_hash(name),
  1200. exclude, name && no_try_delta(name),
  1201. found_pack, found_offset);
  1202. return 1;
  1203. }
  1204. static int add_object_entry_from_bitmap(const struct object_id *oid,
  1205. enum object_type type,
  1206. int flags, uint32_t name_hash,
  1207. struct packed_git *pack, off_t offset)
  1208. {
  1209. display_progress(progress_state, ++nr_seen);
  1210. if (have_duplicate_entry(oid, 0))
  1211. return 0;
  1212. if (!want_object_in_pack(oid, 0, &pack, &offset))
  1213. return 0;
  1214. create_object_entry(oid, type, name_hash, 0, 0, pack, offset);
  1215. return 1;
  1216. }
  1217. struct pbase_tree_cache {
  1218. struct object_id oid;
  1219. int ref;
  1220. int temporary;
  1221. void *tree_data;
  1222. unsigned long tree_size;
  1223. };
  1224. static struct pbase_tree_cache *(pbase_tree_cache[256]);
  1225. static int pbase_tree_cache_ix(const struct object_id *oid)
  1226. {
  1227. return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);
  1228. }
  1229. static int pbase_tree_cache_ix_incr(int ix)
  1230. {
  1231. return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
  1232. }
  1233. static struct pbase_tree {
  1234. struct pbase_tree *next;
  1235. /* This is a phony "cache" entry; we are not
  1236. * going to evict it or find it through _get()
  1237. * mechanism -- this is for the toplevel node that
  1238. * would almost always change with any commit.
  1239. */
  1240. struct pbase_tree_cache pcache;
  1241. } *pbase_tree;
  1242. static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)
  1243. {
  1244. struct pbase_tree_cache *ent, *nent;
  1245. void *data;
  1246. unsigned long size;
  1247. enum object_type type;
  1248. int neigh;
  1249. int my_ix = pbase_tree_cache_ix(oid);
  1250. int available_ix = -1;
  1251. /* pbase-tree-cache acts as a limited hashtable.
  1252. * your object will be found at your index or within a few
  1253. * slots after that slot if it is cached.
  1254. */
  1255. for (neigh = 0; neigh < 8; neigh++) {
  1256. ent = pbase_tree_cache[my_ix];
  1257. if (ent && oideq(&ent->oid, oid)) {
  1258. ent->ref++;
  1259. return ent;
  1260. }
  1261. else if (((available_ix < 0) && (!ent || !ent->ref)) ||
  1262. ((0 <= available_ix) &&
  1263. (!ent && pbase_tree_cache[available_ix])))
  1264. available_ix = my_ix;
  1265. if (!ent)
  1266. break;
  1267. my_ix = pbase_tree_cache_ix_incr(my_ix);
  1268. }
  1269. /* Did not find one. Either we got a bogus request or
  1270. * we need to read and perhaps cache.
  1271. */
  1272. data = read_object_file(oid, &type, &size);
  1273. if (!data)
  1274. return NULL;
  1275. if (type != OBJ_TREE) {
  1276. free(data);
  1277. return NULL;
  1278. }
  1279. /* We need to either cache or return a throwaway copy */
  1280. if (available_ix < 0)
  1281. ent = NULL;
  1282. else {
  1283. ent = pbase_tree_cache[available_ix];
  1284. my_ix = available_ix;
  1285. }
  1286. if (!ent) {
  1287. nent = xmalloc(sizeof(*nent));
  1288. nent->temporary = (available_ix < 0);
  1289. }
  1290. else {
  1291. /* evict and reuse */
  1292. free(ent->tree_data);
  1293. nent = ent;
  1294. }
  1295. oidcpy(&nent->oid, oid);
  1296. nent->tree_data = data;
  1297. nent->tree_size = size;
  1298. nent->ref = 1;
  1299. if (!nent->temporary)
  1300. pbase_tree_cache[my_ix] = nent;
  1301. return nent;
  1302. }
  1303. static void pbase_tree_put(struct pbase_tree_cache *cache)
  1304. {
  1305. if (!cache->temporary) {
  1306. cache->ref--;
  1307. return;
  1308. }
  1309. free(cache->tree_data);
  1310. free(cache);
  1311. }
  1312. static int name_cmp_len(const char *name)
  1313. {
  1314. int i;
  1315. for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
  1316. ;
  1317. return i;
  1318. }
  1319. static void add_pbase_object(struct tree_desc *tree,
  1320. const char *name,
  1321. int cmplen,
  1322. const char *fullname)
  1323. {
  1324. struct name_entry entry;
  1325. int cmp;
  1326. while (tree_entry(tree,&entry)) {
  1327. if (S_ISGITLINK(entry.mode))
  1328. continue;
  1329. cmp = tree_entry_len(&entry) != cmplen ? 1 :
  1330. memcmp(name, entry.path, cmplen);
  1331. if (cmp > 0)
  1332. continue;
  1333. if (cmp < 0)
  1334. return;
  1335. if (name[cmplen] != '/') {
  1336. add_object_entry(&entry.oid,
  1337. object_type(entry.mode),
  1338. fullname, 1);
  1339. return;
  1340. }
  1341. if (S_ISDIR(entry.mode)) {
  1342. struct tree_desc sub;
  1343. struct pbase_tree_cache *tree;
  1344. const char *down = name+cmplen+1;
  1345. int downlen = name_cmp_len(down);
  1346. tree = pbase_tree_get(&entry.oid);
  1347. if (!tree)
  1348. return;
  1349. init_tree_desc(&sub, tree->tree_data, tree->tree_size);
  1350. add_pbase_object(&sub, down, downlen, fullname);
  1351. pbase_tree_put(tree);
  1352. }
  1353. }
  1354. }
  1355. static unsigned *done_pbase_paths;
  1356. static int done_pbase_paths_num;
  1357. static int done_pbase_paths_alloc;
  1358. static int done_pbase_path_pos(unsigned hash)
  1359. {
  1360. int lo = 0;
  1361. int hi = done_pbase_paths_num;
  1362. while (lo < hi) {
  1363. int mi = lo + (hi - lo) / 2;
  1364. if (done_pbase_paths[mi] == hash)
  1365. return mi;
  1366. if (done_pbase_paths[mi] < hash)
  1367. hi = mi;
  1368. else
  1369. lo = mi + 1;
  1370. }
  1371. return -lo-1;
  1372. }
  1373. static int check_pbase_path(unsigned hash)
  1374. {
  1375. int pos = done_pbase_path_pos(hash);
  1376. if (0 <= pos)
  1377. return 1;
  1378. pos = -pos - 1;
  1379. ALLOC_GROW(done_pbase_paths,
  1380. done_pbase_paths_num + 1,
  1381. done_pbase_paths_alloc);
  1382. done_pbase_paths_num++;
  1383. if (pos < done_pbase_paths_num)
  1384. MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
  1385. done_pbase_paths_num - pos - 1);
  1386. done_pbase_paths[pos] = hash;
  1387. return 0;
  1388. }
  1389. static void add_preferred_base_object(const char *name)
  1390. {
  1391. struct pbase_tree *it;
  1392. int cmplen;
  1393. unsigned hash = pack_name_hash(name);
  1394. if (!num_preferred_base || check_pbase_path(hash))
  1395. return;
  1396. cmplen = name_cmp_len(name);
  1397. for (it = pbase_tree; it; it = it->next) {
  1398. if (cmplen == 0) {
  1399. add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);
  1400. }
  1401. else {
  1402. struct tree_desc tree;
  1403. init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
  1404. add_pbase_object(&tree, name, cmplen, name);
  1405. }
  1406. }
  1407. }
  1408. static void add_preferred_base(struct object_id *oid)
  1409. {
  1410. struct pbase_tree *it;
  1411. void *data;
  1412. unsigned long size;
  1413. struct object_id tree_oid;
  1414. if (window <= num_preferred_base++)
  1415. return;
  1416. data = read_object_with_reference(the_repository, oid,
  1417. tree_type, &size, &tree_oid);
  1418. if (!data)
  1419. return;
  1420. for (it = pbase_tree; it; it = it->next) {
  1421. if (oideq(&it->pcache.oid, &tree_oid)) {
  1422. free(data);
  1423. return;
  1424. }
  1425. }
  1426. it = xcalloc(1, sizeof(*it));
  1427. it->next = pbase_tree;
  1428. pbase_tree = it;
  1429. oidcpy(&it->pcache.oid, &tree_oid);
  1430. it->pcache.tree_data = data;
  1431. it->pcache.tree_size = size;
  1432. }
  1433. static void cleanup_preferred_base(void)
  1434. {
  1435. struct pbase_tree *it;
  1436. unsigned i;
  1437. it = pbase_tree;
  1438. pbase_tree = NULL;
  1439. while (it) {
  1440. struct pbase_tree *tmp = it;
  1441. it = tmp->next;
  1442. free(tmp->pcache.tree_data);
  1443. free(tmp);
  1444. }
  1445. for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
  1446. if (!pbase_tree_cache[i])
  1447. continue;
  1448. free(pbase_tree_cache[i]->tree_data);
  1449. FREE_AND_NULL(pbase_tree_cache[i]);
  1450. }
  1451. FREE_AND_NULL(done_pbase_paths);
  1452. done_pbase_paths_num = done_pbase_paths_alloc = 0;
  1453. }
  1454. /*
  1455. * Return 1 iff the object specified by "delta" can be sent
  1456. * literally as a delta against the base in "base_sha1". If
  1457. * so, then *base_out will point to the entry in our packing
  1458. * list, or NULL if we must use the external-base list.
  1459. *
  1460. * Depth value does not matter - find_deltas() will
  1461. * never consider reused delta as the base object to
  1462. * deltify other objects against, in order to avoid
  1463. * circular deltas.
  1464. */
  1465. static int can_reuse_delta(const struct object_id *base_oid,
  1466. struct object_entry *delta,
  1467. struct object_entry **base_out)
  1468. {
  1469. struct object_entry *base;
  1470. /*
  1471. * First see if we're already sending the base (or it's explicitly in
  1472. * our "excluded" list).
  1473. */
  1474. base = packlist_find(&to_pack, base_oid);
  1475. if (base) {
  1476. if (!in_same_island(&delta->idx.oid, &base->idx.oid))
  1477. return 0;
  1478. *base_out = base;
  1479. return 1;
  1480. }
  1481. /*
  1482. * Otherwise, reachability bitmaps may tell us if the receiver has it,
  1483. * even if it was buried too deep in history to make it into the
  1484. * packing list.
  1485. */
  1486. if (thin && bitmap_has_oid_in_uninteresting(bitmap_git, base_oid)) {
  1487. if (use_delta_islands) {
  1488. if (!in_same_island(&delta->idx.oid, base_oid))
  1489. return 0;
  1490. }
  1491. *base_out = NULL;
  1492. return 1;
  1493. }
  1494. return 0;
  1495. }
  1496. static void prefetch_to_pack(uint32_t object_index_start) {
  1497. struct oid_array to_fetch = OID_ARRAY_INIT;
  1498. uint32_t i;
  1499. for (i = object_index_start; i < to_pack.nr_objects; i++) {
  1500. struct object_entry *entry = to_pack.objects + i;
  1501. if (!oid_object_info_extended(the_repository,
  1502. &entry->idx.oid,
  1503. NULL,
  1504. OBJECT_INFO_FOR_PREFETCH))
  1505. continue;
  1506. oid_array_append(&to_fetch, &entry->idx.oid);
  1507. }
  1508. promisor_remote_get_direct(the_repository,
  1509. to_fetch.oid, to_fetch.nr);
  1510. oid_array_clear(&to_fetch);
  1511. }
  1512. static void check_object(struct object_entry *entry, uint32_t object_index)
  1513. {
  1514. unsigned long canonical_size;
  1515. enum object_type type;
  1516. struct object_info oi = {.typep = &type, .sizep = &canonical_size};
  1517. if (IN_PACK(entry)) {
  1518. struct packed_git *p = IN_PACK(entry);
  1519. struct pack_window *w_curs = NULL;
  1520. int have_base = 0;
  1521. struct object_id base_ref;
  1522. struct object_entry *base_entry;
  1523. unsigned long used, used_0;
  1524. unsigned long avail;
  1525. off_t ofs;
  1526. unsigned char *buf, c;
  1527. enum object_type type;
  1528. unsigned long in_pack_size;
  1529. buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
  1530. /*
  1531. * We want in_pack_type even if we do not reuse delta
  1532. * since non-delta representations could still be reused.
  1533. */
  1534. used = unpack_object_header_buffer(buf, avail,
  1535. &type,
  1536. &in_pack_size);
  1537. if (used == 0)
  1538. goto give_up;
  1539. if (type < 0)
  1540. BUG("invalid type %d", type);
  1541. entry->in_pack_type = type;
  1542. /*
  1543. * Determine if this is a delta and if so whether we can
  1544. * reuse it or not. Otherwise let's find out as cheaply as
  1545. * possible what the actual type and size for this object is.
  1546. */
  1547. switch (entry->in_pack_type) {
  1548. default:
  1549. /* Not a delta hence we've already got all we need. */
  1550. oe_set_type(entry, entry->in_pack_type);
  1551. SET_SIZE(entry, in_pack_size);
  1552. entry->in_pack_header_size = used;
  1553. if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB)
  1554. goto give_up;
  1555. unuse_pack(&w_curs);
  1556. return;
  1557. case OBJ_REF_DELTA:
  1558. if (reuse_delta && !entry->preferred_base) {
  1559. oidread(&base_ref,
  1560. use_pack(p, &w_curs,
  1561. entry->in_pack_offset + used,
  1562. NULL));
  1563. have_base = 1;
  1564. }
  1565. entry->in_pack_header_size = used + the_hash_algo->rawsz;
  1566. break;
  1567. case OBJ_OFS_DELTA:
  1568. buf = use_pack(p, &w_curs,
  1569. entry->in_pack_offset + used, NULL);
  1570. used_0 = 0;
  1571. c = buf[used_0++];
  1572. ofs = c & 127;
  1573. while (c & 128) {
  1574. ofs += 1;
  1575. if (!ofs || MSB(ofs, 7)) {
  1576. error(_("delta base offset overflow in pack for %s"),
  1577. oid_to_hex(&entry->idx.oid));
  1578. goto give_up;
  1579. }
  1580. c = buf[used_0++];
  1581. ofs = (ofs << 7) + (c & 127);
  1582. }
  1583. ofs = entry->in_pack_offset - ofs;
  1584. if (ofs <= 0 || ofs >= entry->in_pack_offset) {
  1585. error(_("delta base offset out of bound for %s"),
  1586. oid_to_hex(&entry->idx.oid));
  1587. goto give_up;
  1588. }
  1589. if (reuse_delta && !entry->preferred_base) {
  1590. struct revindex_entry *revidx;
  1591. revidx = find_pack_revindex(p, ofs);
  1592. if (!revidx)
  1593. goto give_up;
  1594. if (!nth_packed_object_id(&base_ref, p, revidx->nr))
  1595. have_base = 1;
  1596. }
  1597. entry->in_pack_header_size = used + used_0;
  1598. break;
  1599. }
  1600. if (have_base &&
  1601. can_reuse_delta(&base_ref, entry, &base_entry)) {
  1602. oe_set_type(entry, entry->in_pack_type);
  1603. SET_SIZE(entry, in_pack_size); /* delta size */
  1604. SET_DELTA_SIZE(entry, in_pack_size);
  1605. if (base_entry) {
  1606. SET_DELTA(entry, base_entry);
  1607. entry->delta_sibling_idx = base_entry->delta_child_idx;
  1608. SET_DELTA_CHILD(base_entry, entry);
  1609. } else {
  1610. SET_DELTA_EXT(entry, &base_ref);
  1611. }
  1612. unuse_pack(&w_curs);
  1613. return;
  1614. }
  1615. if (oe_type(entry)) {
  1616. off_t delta_pos;
  1617. /*
  1618. * This must be a delta and we already know what the
  1619. * final object type is. Let's extract the actual
  1620. * object size from the delta header.
  1621. */
  1622. delta_pos = entry->in_pack_offset + entry->in_pack_header_size;
  1623. canonical_size = get_size_from_delta(p, &w_curs, delta_pos);
  1624. if (canonical_size == 0)
  1625. goto give_up;
  1626. SET_SIZE(entry, canonical_size);
  1627. unuse_pack(&w_curs);
  1628. return;
  1629. }
  1630. /*
  1631. * No choice but to fall back to the recursive delta walk
  1632. * with oid_object_info() to find about the object type
  1633. * at this point...
  1634. */
  1635. give_up:
  1636. unuse_pack(&w_curs);
  1637. }
  1638. if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
  1639. OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0) {
  1640. if (has_promisor_remote()) {
  1641. prefetch_to_pack(object_index);
  1642. if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
  1643. OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0)
  1644. type = -1;
  1645. } else {
  1646. type = -1;
  1647. }
  1648. }
  1649. oe_set_type(entry, type);
  1650. if (entry->type_valid) {
  1651. SET_SIZE(entry, canonical_size);
  1652. } else {
  1653. /*
  1654. * Bad object type is checked in prepare_pack(). This is
  1655. * to permit a missing preferred base object to be ignored
  1656. * as a preferred base. Doing so can result in a larger
  1657. * pack file, but the transfer will still take place.
  1658. */
  1659. }
  1660. }
  1661. static int pack_offset_sort(const void *_a, const void *_b)
  1662. {
  1663. const struct object_entry *a = *(struct object_entry **)_a;
  1664. const struct object_entry *b = *(struct object_entry **)_b;
  1665. const struct packed_git *a_in_pack = IN_PACK(a);
  1666. const struct packed_git *b_in_pack = IN_PACK(b);
  1667. /* avoid filesystem trashing with loose objects */
  1668. if (!a_in_pack && !b_in_pack)
  1669. return oidcmp(&a->idx.oid, &b->idx.oid);
  1670. if (a_in_pack < b_in_pack)
  1671. return -1;
  1672. if (a_in_pack > b_in_pack)
  1673. return 1;
  1674. return a->in_pack_offset < b->in_pack_offset ? -1 :
  1675. (a->in_pack_offset > b->in_pack_offset);
  1676. }
  1677. /*
  1678. * Drop an on-disk delta we were planning to reuse. Naively, this would
  1679. * just involve blanking out the "delta" field, but we have to deal
  1680. * with some extra book-keeping:
  1681. *
  1682. * 1. Removing ourselves from the delta_sibling linked list.
  1683. *
  1684. * 2. Updating our size/type to the non-delta representation. These were
  1685. * either not recorded initially (size) or overwritten with the delta type
  1686. * (type) when check_object() decided to reuse the delta.
  1687. *
  1688. * 3. Resetting our delta depth, as we are now a base object.
  1689. */
  1690. static void drop_reused_delta(struct object_entry *entry)
  1691. {
  1692. unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx;
  1693. struct object_info oi = OBJECT_INFO_INIT;
  1694. enum object_type type;
  1695. unsigned long size;
  1696. while (*idx) {
  1697. struct object_entry *oe = &to_pack.objects[*idx - 1];
  1698. if (oe == entry)
  1699. *idx = oe->delta_sibling_idx;
  1700. else
  1701. idx = &oe->delta_sibling_idx;
  1702. }
  1703. SET_DELTA(entry, NULL);
  1704. entry->depth = 0;
  1705. oi.sizep = &size;
  1706. oi.typep = &type;
  1707. if (packed_object_info(the_repository, IN_PACK(entry), entry->in_pack_offset, &oi) < 0) {
  1708. /*
  1709. * We failed to get the info from this pack for some reason;
  1710. * fall back to oid_object_info, which may find another copy.
  1711. * And if that fails, the error will be recorded in oe_type(entry)
  1712. * and dealt with in prepare_pack().
  1713. */
  1714. oe_set_type(entry,
  1715. oid_object_info(the_repository, &entry->idx.oid, &size));
  1716. } else {
  1717. oe_set_type(entry, type);
  1718. }
  1719. SET_SIZE(entry, size);
  1720. }
  1721. /*
  1722. * Follow the chain of deltas from this entry onward, throwing away any links
  1723. * that cause us to hit a cycle (as determined by the DFS state flags in
  1724. * the entries).
  1725. *
  1726. * We also detect too-long reused chains that would violate our --depth
  1727. * limit.
  1728. */
  1729. static void break_delta_chains(struct object_entry *entry)
  1730. {
  1731. /*
  1732. * The actual depth of each object we will write is stored as an int,
  1733. * as it cannot exceed our int "depth" limit. But before we break
  1734. * changes based no that limit, we may potentially go as deep as the
  1735. * number of objects, which is elsewhere bounded to a uint32_t.
  1736. */
  1737. uint32_t total_depth;
  1738. struct object_entry *cur, *next;
  1739. for (cur = entry, total_depth = 0;
  1740. cur;
  1741. cur = DELTA(cur), total_depth++) {
  1742. if (cur->dfs_state == DFS_DONE) {
  1743. /*
  1744. * We've already seen this object and know it isn't
  1745. * part of a cycle. We do need to append its depth
  1746. * to our count.
  1747. */
  1748. total_depth += cur->depth;
  1749. break;
  1750. }
  1751. /*
  1752. * We break cycles before looping, so an ACTIVE state (or any
  1753. * other cruft which made its way into the state variable)
  1754. * is a bug.
  1755. */
  1756. if (cur->dfs_state != DFS_NONE)
  1757. BUG("confusing delta dfs state in first pass: %d",
  1758. cur->dfs_state);
  1759. /*
  1760. * Now we know this is the first time we've seen the object. If
  1761. * it's not a delta, we're done traversing, but we'll mark it
  1762. * done to save time on future traversals.
  1763. */
  1764. if (!DELTA(cur)) {
  1765. cur->dfs_state = DFS_DONE;
  1766. break;
  1767. }
  1768. /*
  1769. * Mark ourselves as active and see if the next step causes
  1770. * us to cycle to another active object. It's important to do
  1771. * this _before_ we loop, because it impacts where we make the
  1772. * cut, and thus how our total_depth counter works.
  1773. * E.g., We may see a partial loop like:
  1774. *
  1775. * A -> B -> C -> D -> B
  1776. *
  1777. * Cutting B->C breaks the cycle. But now the depth of A is
  1778. * only 1, and our total_depth counter is at 3. The size of the
  1779. * error is always one less than the size of the cycle we
  1780. * broke. Commits C and D were "lost" from A's chain.
  1781. *
  1782. * If we instead cut D->B, then the depth of A is correct at 3.
  1783. * We keep all commits in the chain that we examined.
  1784. */
  1785. cur->dfs_state = DFS_ACTIVE;
  1786. if (DELTA(cur)->dfs_state == DFS_ACTIVE) {
  1787. drop_reused_delta(cur);
  1788. cur->dfs_state = DFS_DONE;
  1789. break;
  1790. }
  1791. }
  1792. /*
  1793. * And now that we've gone all the way to the bottom of the chain, we
  1794. * need to clear the active flags and set the depth fields as
  1795. * appropriate. Unlike the loop above, which can quit when it drops a
  1796. * delta, we need to keep going to look for more depth cuts. So we need
  1797. * an extra "next" pointer to keep going after we reset cur->delta.
  1798. */
  1799. for (cur = entry; cur; cur = next) {
  1800. next = DELTA(cur);
  1801. /*
  1802. * We should have a chain of zero or more ACTIVE states down to
  1803. * a final DONE. We can quit after the DONE, because either it
  1804. * has no bases, or we've already handled them in a previous
  1805. * call.
  1806. */
  1807. if (cur->dfs_state == DFS_DONE)
  1808. break;
  1809. else if (cur->dfs_state != DFS_ACTIVE)
  1810. BUG("confusing delta dfs state in second pass: %d",
  1811. cur->dfs_state);
  1812. /*
  1813. * If the total_depth is more than depth, then we need to snip
  1814. * the chain into two or more smaller chains that don't exceed
  1815. * the maximum depth. Most of the resulting chains will contain
  1816. * (depth + 1) entries (i.e., depth deltas plus one base), and
  1817. * the last chain (i.e., the one containing entry) will contain
  1818. * whatever entries are left over, namely
  1819. * (total_depth % (depth + 1)) of them.
  1820. *
  1821. * Since we are iterating towards decreasing depth, we need to
  1822. * decrement total_depth as we go, and we need to write to the
  1823. * entry what its final depth will be after all of the
  1824. * snipping. Since we're snipping into chains of length (depth
  1825. * + 1) entries, the final depth of an entry will be its
  1826. * original depth modulo (depth + 1). Any time we encounter an
  1827. * entry whose final depth is supposed to be zero, we snip it
  1828. * from its delta base, thereby making it so.
  1829. */
  1830. cur->depth = (total_depth--) % (depth + 1);
  1831. if (!cur->depth)
  1832. drop_reused_delta(cur);
  1833. cur->dfs_state = DFS_DONE;
  1834. }
  1835. }
  1836. static void get_object_details(void)
  1837. {
  1838. uint32_t i;
  1839. struct object_entry **sorted_by_offset;
  1840. if (progress)
  1841. progress_state = start_progress(_("Counting objects"),
  1842. to_pack.nr_objects);
  1843. sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));
  1844. for (i = 0; i < to_pack.nr_objects; i++)
  1845. sorted_by_offset[i] = to_pack.objects + i;
  1846. QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
  1847. for (i = 0; i < to_pack.nr_objects; i++) {
  1848. struct object_entry *entry = sorted_by_offset[i];
  1849. check_object(entry, i);
  1850. if (entry->type_valid &&
  1851. oe_size_greater_than(&to_pack, entry, big_file_threshold))
  1852. entry->no_try_delta = 1;
  1853. display_progress(progress_state, i + 1);
  1854. }
  1855. stop_progress(&progress_state);
  1856. /*
  1857. * This must happen in a second pass, since we rely on the delta
  1858. * information for the whole list being completed.
  1859. */
  1860. for (i = 0; i < to_pack.nr_objects; i++)
  1861. break_delta_chains(&to_pack.objects[i]);
  1862. free(sorted_by_offset);
  1863. }
  1864. /*
  1865. * We search for deltas in a list sorted by type, by filename hash, and then
  1866. * by size, so that we see progressively smaller and smaller files.
  1867. * That's because we prefer deltas to be from the bigger file
  1868. * to the smaller -- deletes are potentially cheaper, but perhaps
  1869. * more importantly, the bigger file is likely the more recent
  1870. * one. The deepest deltas are therefore the oldest objects which are
  1871. * less susceptible to be accessed often.
  1872. */
  1873. static int type_size_sort(const void *_a, const void *_b)
  1874. {
  1875. const struct object_entry *a = *(struct object_entry **)_a;
  1876. const struct object_entry *b = *(struct object_entry **)_b;
  1877. const enum object_type a_type = oe_type(a);
  1878. const enum object_type b_type = oe_type(b);
  1879. const unsigned long a_size = SIZE(a);
  1880. const unsigned long b_size = SIZE(b);
  1881. if (a_type > b_type)
  1882. return -1;
  1883. if (a_type < b_type)
  1884. return 1;
  1885. if (a->hash > b->hash)
  1886. return -1;
  1887. if (a->hash < b->hash)
  1888. return 1;
  1889. if (a->preferred_base > b->preferred_base)
  1890. return -1;
  1891. if (a->preferred_base < b->preferred_base)
  1892. return 1;
  1893. if (use_delta_islands) {
  1894. const int island_cmp = island_delta_cmp(&a->idx.oid, &b->idx.oid);
  1895. if (island_cmp)
  1896. return island_cmp;
  1897. }
  1898. if (a_size > b_size)
  1899. return -1;
  1900. if (a_size < b_size)
  1901. return 1;
  1902. return a < b ? -1 : (a > b); /* newest first */
  1903. }
  1904. struct unpacked {
  1905. struct object_entry *entry;
  1906. void *data;
  1907. struct delta_index *index;
  1908. unsigned depth;
  1909. };
  1910. static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
  1911. unsigned long delta_size)
  1912. {
  1913. if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
  1914. return 0;
  1915. if (delta_size < cache_max_small_delta_size)
  1916. return 1;
  1917. /* cache delta, if objects are large enough compared to delta size */
  1918. if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
  1919. return 1;
  1920. return 0;
  1921. }
  1922. /* Protect delta_cache_size */
  1923. static pthread_mutex_t cache_mutex;
  1924. #define cache_lock() pthread_mutex_lock(&cache_mutex)
  1925. #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
  1926. /*
  1927. * Protect object list partitioning (e.g. struct thread_param) and
  1928. * progress_state
  1929. */
  1930. static pthread_mutex_t progress_mutex;
  1931. #define progress_lock() pthread_mutex_lock(&progress_mutex)
  1932. #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
  1933. /*
  1934. * Access to struct object_entry is unprotected since each thread owns
  1935. * a portion of the main object list. Just don't access object entries
  1936. * ahead in the list because they can be stolen and would need
  1937. * progress_mutex for protection.
  1938. */
  1939. /*
  1940. * Return the size of the object without doing any delta
  1941. * reconstruction (so non-deltas are true object sizes, but deltas
  1942. * return the size of the delta data).
  1943. */
  1944. unsigned long oe_get_size_slow(struct packing_data *pack,
  1945. const struct object_entry *e)
  1946. {
  1947. struct packed_git *p;
  1948. struct pack_window *w_curs;
  1949. unsigned char *buf;
  1950. enum object_type type;
  1951. unsigned long used, avail, size;
  1952. if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
  1953. packing_data_lock(&to_pack);
  1954. if (oid_object_info(the_repository, &e->idx.oid, &size) < 0)
  1955. die(_("unable to get size of %s"),
  1956. oid_to_hex(&e->idx.oid));
  1957. packing_data_unlock(&to_pack);
  1958. return size;
  1959. }
  1960. p = oe_in_pack(pack, e);
  1961. if (!p)
  1962. BUG("when e->type is a delta, it must belong to a pack");
  1963. packing_data_lock(&to_pack);
  1964. w_curs = NULL;
  1965. buf = use_pack(p, &w_curs, e->in_pack_offset, &avail);
  1966. used = unpack_object_header_buffer(buf, avail, &type, &size);
  1967. if (used == 0)
  1968. die(_("unable to parse object header of %s"),
  1969. oid_to_hex(&e->idx.oid));
  1970. unuse_pack(&w_curs);
  1971. packing_data_unlock(&to_pack);
  1972. return size;
  1973. }
  1974. static int try_delta(struct unpacked *trg, struct unpacked *src,
  1975. unsigned max_depth, unsigned long *mem_usage)
  1976. {
  1977. struct object_entry *trg_entry = trg->entry;
  1978. struct object_entry *src_entry = src->entry;
  1979. unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
  1980. unsigned ref_depth;
  1981. enum object_type type;
  1982. void *delta_buf;
  1983. /* Don't bother doing diffs between different types */
  1984. if (oe_type(trg_entry) != oe_type(src_entry))
  1985. return -1;
  1986. /*
  1987. * We do not bother to try a delta that we discarded on an
  1988. * earlier try, but only when reusing delta data. Note that
  1989. * src_entry that is marked as the preferred_base should always
  1990. * be considered, as even if we produce a suboptimal delta against
  1991. * it, we will still save the transfer cost, as we already know
  1992. * the other side has it and we won't send src_entry at all.
  1993. */
  1994. if (reuse_delta && IN_PACK(trg_entry) &&
  1995. IN_PACK(trg_entry) == IN_PACK(src_entry) &&
  1996. !src_entry->preferred_base &&
  1997. trg_entry->in_pack_type != OBJ_REF_DELTA &&
  1998. trg_entry->in_pack_type != OBJ_OFS_DELTA)
  1999. return 0;
  2000. /* Let's not bust the allowed depth. */
  2001. if (src->depth >= max_depth)
  2002. return 0;
  2003. /* Now some size filtering heuristics. */
  2004. trg_size = SIZE(trg_entry);
  2005. if (!DELTA(trg_entry)) {
  2006. max_size = trg_size/2 - the_hash_algo->rawsz;
  2007. ref_depth = 1;
  2008. } else {
  2009. max_size = DELTA_SIZE(trg_entry);
  2010. ref_depth = trg->depth;
  2011. }
  2012. max_size = (uint64_t)max_size * (max_depth - src->depth) /
  2013. (max_depth - ref_depth + 1);
  2014. if (max_size == 0)
  2015. return 0;
  2016. src_size = SIZE(src_entry);
  2017. sizediff = src_size < trg_size ? trg_size - src_size : 0;
  2018. if (sizediff >= max_size)
  2019. return 0;
  2020. if (trg_size < src_size / 32)
  2021. return 0;
  2022. if (!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid))
  2023. return 0;
  2024. /* Load data if not already done */
  2025. if (!trg->data) {
  2026. packing_data_lock(&to_pack);
  2027. trg->data = read_object_file(&trg_entry->idx.oid, &type, &sz);
  2028. packing_data_unlock(&to_pack);
  2029. if (!trg->data)
  2030. die(_("object %s cannot be read"),
  2031. oid_to_hex(&trg_entry->idx.oid));
  2032. if (sz != trg_size)
  2033. die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
  2034. oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz,
  2035. (uintmax_t)trg_size);
  2036. *mem_usage += sz;
  2037. }
  2038. if (!src->data) {
  2039. packing_data_lock(&to_pack);
  2040. src->data = read_object_file(&src_entry->idx.oid, &type, &sz);
  2041. packing_data_unlock(&to_pack);
  2042. if (!src->data) {
  2043. if (src_entry->preferred_base) {
  2044. static int warned = 0;
  2045. if (!warned++)
  2046. warning(_("object %s cannot be read"),
  2047. oid_to_hex(&src_entry->idx.oid));
  2048. /*
  2049. * Those objects are not included in the
  2050. * resulting pack. Be resilient and ignore
  2051. * them if they can't be read, in case the
  2052. * pack could be created nevertheless.
  2053. */
  2054. return 0;
  2055. }
  2056. die(_("object %s cannot be read"),
  2057. oid_to_hex(&src_entry->idx.oid));
  2058. }
  2059. if (sz != src_size)
  2060. die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
  2061. oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz,
  2062. (uintmax_t)src_size);
  2063. *mem_usage += sz;
  2064. }
  2065. if (!src->index) {
  2066. src->index = create_delta_index(src->data, src_size);
  2067. if (!src->index) {
  2068. static int warned = 0;
  2069. if (!warned++)
  2070. warning(_("suboptimal pack - out of memory"));
  2071. return 0;
  2072. }
  2073. *mem_usage += sizeof_delta_index(src->index);
  2074. }
  2075. delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
  2076. if (!delta_buf)
  2077. return 0;
  2078. if (DELTA(trg_entry)) {
  2079. /* Prefer only shallower same-sized deltas. */
  2080. if (delta_size == DELTA_SIZE(trg_entry) &&
  2081. src->depth + 1 >= trg->depth) {
  2082. free(delta_buf);
  2083. return 0;
  2084. }
  2085. }
  2086. /*
  2087. * Handle memory allocation outside of the cache
  2088. * accounting lock. Compiler will optimize the strangeness
  2089. * away when NO_PTHREADS is defined.
  2090. */
  2091. free(trg_entry->delta_data);
  2092. cache_lock();
  2093. if (trg_entry->delta_data) {
  2094. delta_cache_size -= DELTA_SIZE(trg_entry);
  2095. trg_entry->delta_data = NULL;
  2096. }
  2097. if (delta_cacheable(src_size, trg_size, delta_size)) {
  2098. delta_cache_size += delta_size;
  2099. cache_unlock();
  2100. trg_entry->delta_data = xrealloc(delta_buf, delta_size);
  2101. } else {
  2102. cache_unlock();
  2103. free(delta_buf);
  2104. }
  2105. SET_DELTA(trg_entry, src_entry);
  2106. SET_DELTA_SIZE(trg_entry, delta_size);
  2107. trg->depth = src->depth + 1;
  2108. return 1;
  2109. }
  2110. static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
  2111. {
  2112. struct object_entry *child = DELTA_CHILD(me);
  2113. unsigned int m = n;
  2114. while (child) {
  2115. const unsigned int c = check_delta_limit(child, n + 1);
  2116. if (m < c)
  2117. m = c;
  2118. child = DELTA_SIBLING(child);
  2119. }
  2120. return m;
  2121. }
  2122. static unsigned long free_unpacked(struct unpacked *n)
  2123. {
  2124. unsigned long freed_mem = sizeof_delta_index(n->index);
  2125. free_delta_index(n->index);
  2126. n->index = NULL;
  2127. if (n->data) {
  2128. freed_mem += SIZE(n->entry);
  2129. FREE_AND_NULL(n->data);
  2130. }
  2131. n->entry = NULL;
  2132. n->depth = 0;
  2133. return freed_mem;
  2134. }
  2135. static void find_deltas(struct object_entry **list, unsigned *list_size,
  2136. int window, int depth, unsigned *processed)
  2137. {
  2138. uint32_t i, idx = 0, count = 0;
  2139. struct unpacked *array;
  2140. unsigned long mem_usage = 0;
  2141. array = xcalloc(window, sizeof(struct unpacked));
  2142. for (;;) {
  2143. struct object_entry *entry;
  2144. struct unpacked *n = array + idx;
  2145. int j, max_depth, best_base = -1;
  2146. progress_lock();
  2147. if (!*list_size) {
  2148. progress_unlock();
  2149. break;
  2150. }
  2151. entry = *list++;
  2152. (*list_size)--;
  2153. if (!entry->preferred_base) {
  2154. (*processed)++;
  2155. display_progress(progress_state, *processed);
  2156. }
  2157. progress_unlock();
  2158. mem_usage -= free_unpacked(n);
  2159. n->entry = entry;
  2160. while (window_memory_limit &&
  2161. mem_usage > window_memory_limit &&
  2162. count > 1) {
  2163. const uint32_t tail = (idx + window - count) % window;
  2164. mem_usage -= free_unpacked(array + tail);
  2165. count--;
  2166. }
  2167. /* We do not compute delta to *create* objects we are not
  2168. * going to pack.
  2169. */
  2170. if (entry->preferred_base)
  2171. goto next;
  2172. /*
  2173. * If the current object is at pack edge, take the depth the
  2174. * objects that depend on the current object into account
  2175. * otherwise they would become too deep.
  2176. */
  2177. max_depth = depth;
  2178. if (DELTA_CHILD(entry)) {
  2179. max_depth -= check_delta_limit(entry, 0);
  2180. if (max_depth <= 0)
  2181. goto next;
  2182. }
  2183. j = window;
  2184. while (--j > 0) {
  2185. int ret;
  2186. uint32_t other_idx = idx + j;
  2187. struct unpacked *m;
  2188. if (other_idx >= window)
  2189. other_idx -= window;
  2190. m = array + other_idx;
  2191. if (!m->entry)
  2192. break;
  2193. ret = try_delta(n, m, max_depth, &mem_usage);
  2194. if (ret < 0)
  2195. break;
  2196. else if (ret > 0)
  2197. best_base = other_idx;
  2198. }
  2199. /*
  2200. * If we decided to cache the delta data, then it is best
  2201. * to compress it right away. First because we have to do
  2202. * it anyway, and doing it here while we're threaded will
  2203. * save a lot of time in the non threaded write phase,
  2204. * as well as allow for caching more deltas within
  2205. * the same cache size limit.
  2206. * ...
  2207. * But only if not writing to stdout, since in that case
  2208. * the network is most likely throttling writes anyway,
  2209. * and therefore it is best to go to the write phase ASAP
  2210. * instead, as we can afford spending more time compressing
  2211. * between writes at that moment.
  2212. */
  2213. if (entry->delta_data && !pack_to_stdout) {
  2214. unsigned long size;
  2215. size = do_compress(&entry->delta_data, DELTA_SIZE(entry));
  2216. if (size < (1U << OE_Z_DELTA_BITS)) {
  2217. entry->z_delta_size = size;
  2218. cache_lock();
  2219. delta_cache_size -= DELTA_SIZE(entry);
  2220. delta_cache_size += entry->z_delta_size;
  2221. cache_unlock();
  2222. } else {
  2223. FREE_AND_NULL(entry->delta_data);
  2224. entry->z_delta_size = 0;
  2225. }
  2226. }
  2227. /* if we made n a delta, and if n is already at max
  2228. * depth, leaving it in the window is pointless. we
  2229. * should evict it first.
  2230. */
  2231. if (DELTA(entry) && max_depth <= n->depth)
  2232. continue;
  2233. /*
  2234. * Move the best delta base up in the window, after the
  2235. * currently deltified object, to keep it longer. It will
  2236. * be the first base object to be attempted next.
  2237. */
  2238. if (DELTA(entry)) {
  2239. struct unpacked swap = array[best_base];
  2240. int dist = (window + idx - best_base) % window;
  2241. int dst = best_base;
  2242. while (dist--) {
  2243. int src = (dst + 1) % window;
  2244. array[dst] = array[src];
  2245. dst = src;
  2246. }
  2247. array[dst] = swap;
  2248. }
  2249. next:
  2250. idx++;
  2251. if (count + 1 < window)
  2252. count++;
  2253. if (idx >= window)
  2254. idx = 0;
  2255. }
  2256. for (i = 0; i < window; ++i) {
  2257. free_delta_index(array[i].index);
  2258. free(array[i].data);
  2259. }
  2260. free(array);
  2261. }
  2262. /*
  2263. * The main object list is split into smaller lists, each is handed to
  2264. * one worker.
  2265. *
  2266. * The main thread waits on the condition that (at least) one of the workers
  2267. * has stopped working (which is indicated in the .working member of
  2268. * struct thread_params).
  2269. *
  2270. * When a work thread has completed its work, it sets .working to 0 and
  2271. * signals the main thread and waits on the condition that .data_ready
  2272. * becomes 1.
  2273. *
  2274. * The main thread steals half of the work from the worker that has
  2275. * most work left to hand it to the idle worker.
  2276. */
  2277. struct thread_params {
  2278. pthread_t thread;
  2279. struct object_entry **list;
  2280. unsigned list_size;
  2281. unsigned remaining;
  2282. int window;
  2283. int depth;
  2284. int working;
  2285. int data_ready;
  2286. pthread_mutex_t mutex;
  2287. pthread_cond_t cond;
  2288. unsigned *processed;
  2289. };
  2290. static pthread_cond_t progress_cond;
  2291. /*
  2292. * Mutex and conditional variable can't be statically-initialized on Windows.
  2293. */
  2294. static void init_threaded_search(void)
  2295. {
  2296. pthread_mutex_init(&cache_mutex, NULL);
  2297. pthread_mutex_init(&progress_mutex, NULL);
  2298. pthread_cond_init(&progress_cond, NULL);
  2299. }
  2300. static void cleanup_threaded_search(void)
  2301. {
  2302. pthread_cond_destroy(&progress_cond);
  2303. pthread_mutex_destroy(&cache_mutex);
  2304. pthread_mutex_destroy(&progress_mutex);
  2305. }
  2306. static void *threaded_find_deltas(void *arg)
  2307. {
  2308. struct thread_params *me = arg;
  2309. progress_lock();
  2310. while (me->remaining) {
  2311. progress_unlock();
  2312. find_deltas(me->list, &me->remaining,
  2313. me->window, me->depth, me->processed);
  2314. progress_lock();
  2315. me->working = 0;
  2316. pthread_cond_signal(&progress_cond);
  2317. progress_unlock();
  2318. /*
  2319. * We must not set ->data_ready before we wait on the
  2320. * condition because the main thread may have set it to 1
  2321. * before we get here. In order to be sure that new
  2322. * work is available if we see 1 in ->data_ready, it
  2323. * was initialized to 0 before this thread was spawned
  2324. * and we reset it to 0 right away.
  2325. */
  2326. pthread_mutex_lock(&me->mutex);
  2327. while (!me->data_ready)
  2328. pthread_cond_wait(&me->cond, &me->mutex);
  2329. me->data_ready = 0;
  2330. pthread_mutex_unlock(&me->mutex);
  2331. progress_lock();
  2332. }
  2333. progress_unlock();
  2334. /* leave ->working 1 so that this doesn't get more work assigned */
  2335. return NULL;
  2336. }
  2337. static void ll_find_deltas(struct object_entry **list, unsigned list_size,
  2338. int window, int depth, unsigned *processed)
  2339. {
  2340. struct thread_params *p;
  2341. int i, ret, active_threads = 0;
  2342. init_threaded_search();
  2343. if (delta_search_threads <= 1) {
  2344. find_deltas(list, &list_size, window, depth, processed);
  2345. cleanup_threaded_search();
  2346. return;
  2347. }
  2348. if (progress > pack_to_stdout)
  2349. fprintf_ln(stderr, _("Delta compression using up to %d threads"),
  2350. delta_search_threads);
  2351. p = xcalloc(delta_search_threads, sizeof(*p));
  2352. /* Partition the work amongst work threads. */
  2353. for (i = 0; i < delta_search_threads; i++) {
  2354. unsigned sub_size = list_size / (delta_search_threads - i);
  2355. /* don't use too small segments or no deltas will be found */
  2356. if (sub_size < 2*window && i+1 < delta_search_threads)
  2357. sub_size = 0;
  2358. p[i].window = window;
  2359. p[i].depth = depth;
  2360. p[i].processed = processed;
  2361. p[i].working = 1;
  2362. p[i].data_ready = 0;
  2363. /* try to split chunks on "path" boundaries */
  2364. while (sub_size && sub_size < list_size &&
  2365. list[sub_size]->hash &&
  2366. list[sub_size]->hash == list[sub_size-1]->hash)
  2367. sub_size++;
  2368. p[i].list = list;
  2369. p[i].list_size = sub_size;
  2370. p[i].remaining = sub_size;
  2371. list += sub_size;
  2372. list_size -= sub_size;
  2373. }
  2374. /* Start work threads. */
  2375. for (i = 0; i < delta_search_threads; i++) {
  2376. if (!p[i].list_size)
  2377. continue;
  2378. pthread_mutex_init(&p[i].mutex, NULL);
  2379. pthread_cond_init(&p[i].cond, NULL);
  2380. ret = pthread_create(&p[i].thread, NULL,
  2381. threaded_find_deltas, &p[i]);
  2382. if (ret)
  2383. die(_("unable to create thread: %s"), strerror(ret));
  2384. active_threads++;
  2385. }
  2386. /*
  2387. * Now let's wait for work completion. Each time a thread is done
  2388. * with its work, we steal half of the remaining work from the
  2389. * thread with the largest number of unprocessed objects and give
  2390. * it to that newly idle thread. This ensure good load balancing
  2391. * until the remaining object list segments are simply too short
  2392. * to be worth splitting anymore.
  2393. */
  2394. while (active_threads) {
  2395. struct thread_params *target = NULL;
  2396. struct thread_params *victim = NULL;
  2397. unsigned sub_size = 0;
  2398. progress_lock();
  2399. for (;;) {
  2400. for (i = 0; !target && i < delta_search_threads; i++)
  2401. if (!p[i].working)
  2402. target = &p[i];
  2403. if (target)
  2404. break;
  2405. pthread_cond_wait(&progress_cond, &progress_mutex);
  2406. }
  2407. for (i = 0; i < delta_search_threads; i++)
  2408. if (p[i].remaining > 2*window &&
  2409. (!victim || victim->remaining < p[i].remaining))
  2410. victim = &p[i];
  2411. if (victim) {
  2412. sub_size = victim->remaining / 2;
  2413. list = victim->list + victim->list_size - sub_size;
  2414. while (sub_size && list[0]->hash &&
  2415. list[0]->hash == list[-1]->hash) {
  2416. list++;
  2417. sub_size--;
  2418. }
  2419. if (!sub_size) {
  2420. /*
  2421. * It is possible for some "paths" to have
  2422. * so many objects that no hash boundary
  2423. * might be found. Let's just steal the
  2424. * exact half in that case.
  2425. */
  2426. sub_size = victim->remaining / 2;
  2427. list -= sub_size;
  2428. }
  2429. target->list = list;
  2430. victim->list_size -= sub_size;
  2431. victim->remaining -= sub_size;
  2432. }
  2433. target->list_size = sub_size;
  2434. target->remaining = sub_size;
  2435. target->working = 1;
  2436. progress_unlock();
  2437. pthread_mutex_lock(&target->mutex);
  2438. target->data_ready = 1;
  2439. pthread_cond_signal(&target->cond);
  2440. pthread_mutex_unlock(&target->mutex);
  2441. if (!sub_size) {
  2442. pthread_join(target->thread, NULL);
  2443. pthread_cond_destroy(&target->cond);
  2444. pthread_mutex_destroy(&target->mutex);
  2445. active_threads--;
  2446. }
  2447. }
  2448. cleanup_threaded_search();
  2449. free(p);
  2450. }
  2451. static int obj_is_packed(const struct object_id *oid)
  2452. {
  2453. return packlist_find(&to_pack, oid) ||
  2454. (reuse_packfile_bitmap &&
  2455. bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid));
  2456. }
  2457. static void add_tag_chain(const struct object_id *oid)
  2458. {
  2459. struct tag *tag;
  2460. /*
  2461. * We catch duplicates already in add_object_entry(), but we'd
  2462. * prefer to do this extra check to avoid having to parse the
  2463. * tag at all if we already know that it's being packed (e.g., if
  2464. * it was included via bitmaps, we would not have parsed it
  2465. * previously).
  2466. */
  2467. if (obj_is_packed(oid))
  2468. return;
  2469. tag = lookup_tag(the_repository, oid);
  2470. while (1) {
  2471. if (!tag || parse_tag(tag) || !tag->tagged)
  2472. die(_("unable to pack objects reachable from tag %s"),
  2473. oid_to_hex(oid));
  2474. add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);
  2475. if (tag->tagged->type != OBJ_TAG)
  2476. return;
  2477. tag = (struct tag *)tag->tagged;
  2478. }
  2479. }
  2480. static int add_ref_tag(const char *path, const struct object_id *oid, int flag, void *cb_data)
  2481. {
  2482. struct object_id peeled;
  2483. if (starts_with(path, "refs/tags/") && /* is a tag? */
  2484. !peel_ref(path, &peeled) && /* peelable? */
  2485. obj_is_packed(&peeled)) /* object packed? */
  2486. add_tag_chain(oid);
  2487. return 0;
  2488. }
  2489. static void prepare_pack(int window, int depth)
  2490. {
  2491. struct object_entry **delta_list;
  2492. uint32_t i, nr_deltas;
  2493. unsigned n;
  2494. if (use_delta_islands)
  2495. resolve_tree_islands(the_repository, progress, &to_pack);
  2496. get_object_details();
  2497. /*
  2498. * If we're locally repacking then we need to be doubly careful
  2499. * from now on in order to make sure no stealth corruption gets
  2500. * propagated to the new pack. Clients receiving streamed packs
  2501. * should validate everything they get anyway so no need to incur
  2502. * the additional cost here in that case.
  2503. */
  2504. if (!pack_to_stdout)
  2505. do_check_packed_object_crc = 1;
  2506. if (!to_pack.nr_objects || !window || !depth)
  2507. return;
  2508. ALLOC_ARRAY(delta_list, to_pack.nr_objects);
  2509. nr_deltas = n = 0;
  2510. for (i = 0; i < to_pack.nr_objects; i++) {
  2511. struct object_entry *entry = to_pack.objects + i;
  2512. if (DELTA(entry))
  2513. /* This happens if we decided to reuse existing
  2514. * delta from a pack. "reuse_delta &&" is implied.
  2515. */
  2516. continue;
  2517. if (!entry->type_valid ||
  2518. oe_size_less_than(&to_pack, entry, 50))
  2519. continue;
  2520. if (entry->no_try_delta)
  2521. continue;
  2522. if (!entry->preferred_base) {
  2523. nr_deltas++;
  2524. if (oe_type(entry) < 0)
  2525. die(_("unable to get type of object %s"),
  2526. oid_to_hex(&entry->idx.oid));
  2527. } else {
  2528. if (oe_type(entry) < 0) {
  2529. /*
  2530. * This object is not found, but we
  2531. * don't have to include it anyway.
  2532. */
  2533. continue;
  2534. }
  2535. }
  2536. delta_list[n++] = entry;
  2537. }
  2538. if (nr_deltas && n > 1) {
  2539. unsigned nr_done = 0;
  2540. if (progress)
  2541. progress_state = start_progress(_("Compressing objects"),
  2542. nr_deltas);
  2543. QSORT(delta_list, n, type_size_sort);
  2544. ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
  2545. stop_progress(&progress_state);
  2546. if (nr_done != nr_deltas)
  2547. die(_("inconsistency with delta count"));
  2548. }
  2549. free(delta_list);
  2550. }
  2551. static int git_pack_config(const char *k, const char *v, void *cb)
  2552. {
  2553. if (!strcmp(k, "pack.window")) {
  2554. window = git_config_int(k, v);
  2555. return 0;
  2556. }
  2557. if (!strcmp(k, "pack.windowmemory")) {
  2558. window_memory_limit = git_config_ulong(k, v);
  2559. return 0;
  2560. }
  2561. if (!strcmp(k, "pack.depth")) {
  2562. depth = git_config_int(k, v);
  2563. return 0;
  2564. }
  2565. if (!strcmp(k, "pack.deltacachesize")) {
  2566. max_delta_cache_size = git_config_int(k, v);
  2567. return 0;
  2568. }
  2569. if (!strcmp(k, "pack.deltacachelimit")) {
  2570. cache_max_small_delta_size = git_config_int(k, v);
  2571. return 0;
  2572. }
  2573. if (!strcmp(k, "pack.writebitmaphashcache")) {
  2574. if (git_config_bool(k, v))
  2575. write_bitmap_options |= BITMAP_OPT_HASH_CACHE;
  2576. else
  2577. write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;
  2578. }
  2579. if (!strcmp(k, "pack.usebitmaps")) {
  2580. use_bitmap_index_default = git_config_bool(k, v);
  2581. return 0;
  2582. }
  2583. if (!strcmp(k, "pack.allowpackreuse")) {
  2584. allow_pack_reuse = git_config_bool(k, v);
  2585. return 0;
  2586. }
  2587. if (!strcmp(k, "pack.threads")) {
  2588. delta_search_threads = git_config_int(k, v);
  2589. if (delta_search_threads < 0)
  2590. die(_("invalid number of threads specified (%d)"),
  2591. delta_search_threads);
  2592. if (!HAVE_THREADS && delta_search_threads != 1) {
  2593. warning(_("no threads support, ignoring %s"), k);
  2594. delta_search_threads = 0;
  2595. }
  2596. return 0;
  2597. }
  2598. if (!strcmp(k, "pack.indexversion")) {
  2599. pack_idx_opts.version = git_config_int(k, v);
  2600. if (pack_idx_opts.version > 2)
  2601. die(_("bad pack.indexversion=%"PRIu32),
  2602. pack_idx_opts.version);
  2603. return 0;
  2604. }
  2605. if (!strcmp(k, "uploadpack.blobpackfileuri")) {
  2606. struct configured_exclusion *ex = xmalloc(sizeof(*ex));
  2607. const char *oid_end, *pack_end;
  2608. /*
  2609. * Stores the pack hash. This is not a true object ID, but is
  2610. * of the same form.
  2611. */
  2612. struct object_id pack_hash;
  2613. if (parse_oid_hex(v, &ex->e.oid, &oid_end) ||
  2614. *oid_end != ' ' ||
  2615. parse_oid_hex(oid_end + 1, &pack_hash, &pack_end) ||
  2616. *pack_end != ' ')
  2617. die(_("value of uploadpack.blobpackfileuri must be "
  2618. "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v);
  2619. if (oidmap_get(&configured_exclusions, &ex->e.oid))
  2620. die(_("object already configured in another "
  2621. "uploadpack.blobpackfileuri (got '%s')"), v);
  2622. ex->pack_hash_hex = xcalloc(1, pack_end - oid_end);
  2623. memcpy(ex->pack_hash_hex, oid_end + 1, pack_end - oid_end - 1);
  2624. ex->uri = xstrdup(pack_end + 1);
  2625. oidmap_put(&configured_exclusions, ex);
  2626. }
  2627. return git_default_config(k, v, cb);
  2628. }
  2629. static void read_object_list_from_stdin(void)
  2630. {
  2631. char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];
  2632. struct object_id oid;
  2633. const char *p;
  2634. for (;;) {
  2635. if (!fgets(line, sizeof(line), stdin)) {
  2636. if (feof(stdin))
  2637. break;
  2638. if (!ferror(stdin))
  2639. die("BUG: fgets returned NULL, not EOF, not error!");
  2640. if (errno != EINTR)
  2641. die_errno("fgets");
  2642. clearerr(stdin);
  2643. continue;
  2644. }
  2645. if (line[0] == '-') {
  2646. if (get_oid_hex(line+1, &oid))
  2647. die(_("expected edge object ID, got garbage:\n %s"),
  2648. line);
  2649. add_preferred_base(&oid);
  2650. continue;
  2651. }
  2652. if (parse_oid_hex(line, &oid, &p))
  2653. die(_("expected object ID, got garbage:\n %s"), line);
  2654. add_preferred_base_object(p + 1);
  2655. add_object_entry(&oid, OBJ_NONE, p + 1, 0);
  2656. }
  2657. }
  2658. /* Remember to update object flag allocation in object.h */
  2659. #define OBJECT_ADDED (1u<<20)
  2660. static void show_commit(struct commit *commit, void *data)
  2661. {
  2662. add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
  2663. commit->object.flags |= OBJECT_ADDED;
  2664. if (write_bitmap_index)
  2665. index_commit_for_bitmap(commit);
  2666. if (use_delta_islands)
  2667. propagate_island_marks(commit);
  2668. }
  2669. static void show_object(struct object *obj, const char *name, void *data)
  2670. {
  2671. add_preferred_base_object(name);
  2672. add_object_entry(&obj->oid, obj->type, name, 0);
  2673. obj->flags |= OBJECT_ADDED;
  2674. if (use_delta_islands) {
  2675. const char *p;
  2676. unsigned depth;
  2677. struct object_entry *ent;
  2678. /* the empty string is a root tree, which is depth 0 */
  2679. depth = *name ? 1 : 0;
  2680. for (p = strchr(name, '/'); p; p = strchr(p + 1, '/'))
  2681. depth++;
  2682. ent = packlist_find(&to_pack, &obj->oid);
  2683. if (ent && depth > oe_tree_depth(&to_pack, ent))
  2684. oe_set_tree_depth(&to_pack, ent, depth);
  2685. }
  2686. }
  2687. static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)
  2688. {
  2689. assert(arg_missing_action == MA_ALLOW_ANY);
  2690. /*
  2691. * Quietly ignore ALL missing objects. This avoids problems with
  2692. * staging them now and getting an odd error later.
  2693. */
  2694. if (!has_object(the_repository, &obj->oid, 0))
  2695. return;
  2696. show_object(obj, name, data);
  2697. }
  2698. static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)
  2699. {
  2700. assert(arg_missing_action == MA_ALLOW_PROMISOR);
  2701. /*
  2702. * Quietly ignore EXPECTED missing objects. This avoids problems with
  2703. * staging them now and getting an odd error later.
  2704. */
  2705. if (!has_object(the_repository, &obj->oid, 0) && is_promisor_object(&obj->oid))
  2706. return;
  2707. show_object(obj, name, data);
  2708. }
  2709. static int option_parse_missing_action(const struct option *opt,
  2710. const char *arg, int unset)
  2711. {
  2712. assert(arg);
  2713. assert(!unset);
  2714. if (!strcmp(arg, "error")) {
  2715. arg_missing_action = MA_ERROR;
  2716. fn_show_object = show_object;
  2717. return 0;
  2718. }
  2719. if (!strcmp(arg, "allow-any")) {
  2720. arg_missing_action = MA_ALLOW_ANY;
  2721. fetch_if_missing = 0;
  2722. fn_show_object = show_object__ma_allow_any;
  2723. return 0;
  2724. }
  2725. if (!strcmp(arg, "allow-promisor")) {
  2726. arg_missing_action = MA_ALLOW_PROMISOR;
  2727. fetch_if_missing = 0;
  2728. fn_show_object = show_object__ma_allow_promisor;
  2729. return 0;
  2730. }
  2731. die(_("invalid value for --missing"));
  2732. return 0;
  2733. }
  2734. static void show_edge(struct commit *commit)
  2735. {
  2736. add_preferred_base(&commit->object.oid);
  2737. }
  2738. struct in_pack_object {
  2739. off_t offset;
  2740. struct object *object;
  2741. };
  2742. struct in_pack {
  2743. unsigned int alloc;
  2744. unsigned int nr;
  2745. struct in_pack_object *array;
  2746. };
  2747. static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack)
  2748. {
  2749. in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->oid.hash, p);
  2750. in_pack->array[in_pack->nr].object = object;
  2751. in_pack->nr++;
  2752. }
  2753. /*
  2754. * Compare the objects in the offset order, in order to emulate the
  2755. * "git rev-list --objects" output that produced the pack originally.
  2756. */
  2757. static int ofscmp(const void *a_, const void *b_)
  2758. {
  2759. struct in_pack_object *a = (struct in_pack_object *)a_;
  2760. struct in_pack_object *b = (struct in_pack_object *)b_;
  2761. if (a->offset < b->offset)
  2762. return -1;
  2763. else if (a->offset > b->offset)
  2764. return 1;
  2765. else
  2766. return oidcmp(&a->object->oid, &b->object->oid);
  2767. }
  2768. static void add_objects_in_unpacked_packs(void)
  2769. {
  2770. struct packed_git *p;
  2771. struct in_pack in_pack;
  2772. uint32_t i;
  2773. memset(&in_pack, 0, sizeof(in_pack));
  2774. for (p = get_all_packs(the_repository); p; p = p->next) {
  2775. struct object_id oid;
  2776. struct object *o;
  2777. if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
  2778. continue;
  2779. if (open_pack_index(p))
  2780. die(_("cannot open pack index"));
  2781. ALLOC_GROW(in_pack.array,
  2782. in_pack.nr + p->num_objects,
  2783. in_pack.alloc);
  2784. for (i = 0; i < p->num_objects; i++) {
  2785. nth_packed_object_id(&oid, p, i);
  2786. o = lookup_unknown_object(&oid);
  2787. if (!(o->flags & OBJECT_ADDED))
  2788. mark_in_pack_object(o, p, &in_pack);
  2789. o->flags |= OBJECT_ADDED;
  2790. }
  2791. }
  2792. if (in_pack.nr) {
  2793. QSORT(in_pack.array, in_pack.nr, ofscmp);
  2794. for (i = 0; i < in_pack.nr; i++) {
  2795. struct object *o = in_pack.array[i].object;
  2796. add_object_entry(&o->oid, o->type, "", 0);
  2797. }
  2798. }
  2799. free(in_pack.array);
  2800. }
  2801. static int add_loose_object(const struct object_id *oid, const char *path,
  2802. void *data)
  2803. {
  2804. enum object_type type = oid_object_info(the_repository, oid, NULL);
  2805. if (type < 0) {
  2806. warning(_("loose object at %s could not be examined"), path);
  2807. return 0;
  2808. }
  2809. add_object_entry(oid, type, "", 0);
  2810. return 0;
  2811. }
  2812. /*
  2813. * We actually don't even have to worry about reachability here.
  2814. * add_object_entry will weed out duplicates, so we just add every
  2815. * loose object we find.
  2816. */
  2817. static void add_unreachable_loose_objects(void)
  2818. {
  2819. for_each_loose_file_in_objdir(get_object_directory(),
  2820. add_loose_object,
  2821. NULL, NULL, NULL);
  2822. }
  2823. static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
  2824. {
  2825. static struct packed_git *last_found = (void *)1;
  2826. struct packed_git *p;
  2827. p = (last_found != (void *)1) ? last_found :
  2828. get_all_packs(the_repository);
  2829. while (p) {
  2830. if ((!p->pack_local || p->pack_keep ||
  2831. p->pack_keep_in_core) &&
  2832. find_pack_entry_one(oid->hash, p)) {
  2833. last_found = p;
  2834. return 1;
  2835. }
  2836. if (p == last_found)
  2837. p = get_all_packs(the_repository);
  2838. else
  2839. p = p->next;
  2840. if (p == last_found)
  2841. p = p->next;
  2842. }
  2843. return 0;
  2844. }
  2845. /*
  2846. * Store a list of sha1s that are should not be discarded
  2847. * because they are either written too recently, or are
  2848. * reachable from another object that was.
  2849. *
  2850. * This is filled by get_object_list.
  2851. */
  2852. static struct oid_array recent_objects;
  2853. static int loosened_object_can_be_discarded(const struct object_id *oid,
  2854. timestamp_t mtime)
  2855. {
  2856. if (!unpack_unreachable_expiration)
  2857. return 0;
  2858. if (mtime > unpack_unreachable_expiration)
  2859. return 0;
  2860. if (oid_array_lookup(&recent_objects, oid) >= 0)
  2861. return 0;
  2862. return 1;
  2863. }
  2864. static void loosen_unused_packed_objects(void)
  2865. {
  2866. struct packed_git *p;
  2867. uint32_t i;
  2868. struct object_id oid;
  2869. for (p = get_all_packs(the_repository); p; p = p->next) {
  2870. if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
  2871. continue;
  2872. if (open_pack_index(p))
  2873. die(_("cannot open pack index"));
  2874. for (i = 0; i < p->num_objects; i++) {
  2875. nth_packed_object_id(&oid, p, i);
  2876. if (!packlist_find(&to_pack, &oid) &&
  2877. !has_sha1_pack_kept_or_nonlocal(&oid) &&
  2878. !loosened_object_can_be_discarded(&oid, p->mtime))
  2879. if (force_object_loose(&oid, p->mtime))
  2880. die(_("unable to force loose object"));
  2881. }
  2882. }
  2883. }
  2884. /*
  2885. * This tracks any options which pack-reuse code expects to be on, or which a
  2886. * reader of the pack might not understand, and which would therefore prevent
  2887. * blind reuse of what we have on disk.
  2888. */
  2889. static int pack_options_allow_reuse(void)
  2890. {
  2891. return allow_pack_reuse &&
  2892. pack_to_stdout &&
  2893. !ignore_packed_keep_on_disk &&
  2894. !ignore_packed_keep_in_core &&
  2895. (!local || !have_non_local_packs) &&
  2896. !incremental;
  2897. }
  2898. static int get_object_list_from_bitmap(struct rev_info *revs)
  2899. {
  2900. if (!(bitmap_git = prepare_bitmap_walk(revs, &filter_options)))
  2901. return -1;
  2902. if (pack_options_allow_reuse() &&
  2903. !reuse_partial_packfile_from_bitmap(
  2904. bitmap_git,
  2905. &reuse_packfile,
  2906. &reuse_packfile_objects,
  2907. &reuse_packfile_bitmap)) {
  2908. assert(reuse_packfile_objects);
  2909. nr_result += reuse_packfile_objects;
  2910. display_progress(progress_state, nr_result);
  2911. }
  2912. traverse_bitmap_commit_list(bitmap_git, revs,
  2913. &add_object_entry_from_bitmap);
  2914. return 0;
  2915. }
  2916. static void record_recent_object(struct object *obj,
  2917. const char *name,
  2918. void *data)
  2919. {
  2920. oid_array_append(&recent_objects, &obj->oid);
  2921. }
  2922. static void record_recent_commit(struct commit *commit, void *data)
  2923. {
  2924. oid_array_append(&recent_objects, &commit->object.oid);
  2925. }
  2926. static void get_object_list(int ac, const char **av)
  2927. {
  2928. struct rev_info revs;
  2929. struct setup_revision_opt s_r_opt = {
  2930. .allow_exclude_promisor_objects = 1,
  2931. };
  2932. char line[1000];
  2933. int flags = 0;
  2934. int save_warning;
  2935. repo_init_revisions(the_repository, &revs, NULL);
  2936. save_commit_buffer = 0;
  2937. setup_revisions(ac, av, &revs, &s_r_opt);
  2938. /* make sure shallows are read */
  2939. is_repository_shallow(the_repository);
  2940. save_warning = warn_on_object_refname_ambiguity;
  2941. warn_on_object_refname_ambiguity = 0;
  2942. while (fgets(line, sizeof(line), stdin) != NULL) {
  2943. int len = strlen(line);
  2944. if (len && line[len - 1] == '\n')
  2945. line[--len] = 0;
  2946. if (!len)
  2947. break;
  2948. if (*line == '-') {
  2949. if (!strcmp(line, "--not")) {
  2950. flags ^= UNINTERESTING;
  2951. write_bitmap_index = 0;
  2952. continue;
  2953. }
  2954. if (starts_with(line, "--shallow ")) {
  2955. struct object_id oid;
  2956. if (get_oid_hex(line + 10, &oid))
  2957. die("not an object name '%s'", line + 10);
  2958. register_shallow(the_repository, &oid);
  2959. use_bitmap_index = 0;
  2960. continue;
  2961. }
  2962. die(_("not a rev '%s'"), line);
  2963. }
  2964. if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))
  2965. die(_("bad revision '%s'"), line);
  2966. }
  2967. warn_on_object_refname_ambiguity = save_warning;
  2968. if (use_bitmap_index && !get_object_list_from_bitmap(&revs))
  2969. return;
  2970. if (use_delta_islands)
  2971. load_delta_islands(the_repository, progress);
  2972. if (prepare_revision_walk(&revs))
  2973. die(_("revision walk setup failed"));
  2974. mark_edges_uninteresting(&revs, show_edge, sparse);
  2975. if (!fn_show_object)
  2976. fn_show_object = show_object;
  2977. traverse_commit_list_filtered(&filter_options, &revs,
  2978. show_commit, fn_show_object, NULL,
  2979. NULL);
  2980. if (unpack_unreachable_expiration) {
  2981. revs.ignore_missing_links = 1;
  2982. if (add_unseen_recent_objects_to_traversal(&revs,
  2983. unpack_unreachable_expiration))
  2984. die(_("unable to add recent objects"));
  2985. if (prepare_revision_walk(&revs))
  2986. die(_("revision walk setup failed"));
  2987. traverse_commit_list(&revs, record_recent_commit,
  2988. record_recent_object, NULL);
  2989. }
  2990. if (keep_unreachable)
  2991. add_objects_in_unpacked_packs();
  2992. if (pack_loose_unreachable)
  2993. add_unreachable_loose_objects();
  2994. if (unpack_unreachable)
  2995. loosen_unused_packed_objects();
  2996. oid_array_clear(&recent_objects);
  2997. }
  2998. static void add_extra_kept_packs(const struct string_list *names)
  2999. {
  3000. struct packed_git *p;
  3001. if (!names->nr)
  3002. return;
  3003. for (p = get_all_packs(the_repository); p; p = p->next) {
  3004. const char *name = basename(p->pack_name);
  3005. int i;
  3006. if (!p->pack_local)
  3007. continue;
  3008. for (i = 0; i < names->nr; i++)
  3009. if (!fspathcmp(name, names->items[i].string))
  3010. break;
  3011. if (i < names->nr) {
  3012. p->pack_keep_in_core = 1;
  3013. ignore_packed_keep_in_core = 1;
  3014. continue;
  3015. }
  3016. }
  3017. }
  3018. static int option_parse_index_version(const struct option *opt,
  3019. const char *arg, int unset)
  3020. {
  3021. char *c;
  3022. const char *val = arg;
  3023. BUG_ON_OPT_NEG(unset);
  3024. pack_idx_opts.version = strtoul(val, &c, 10);
  3025. if (pack_idx_opts.version > 2)
  3026. die(_("unsupported index version %s"), val);
  3027. if (*c == ',' && c[1])
  3028. pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);
  3029. if (*c || pack_idx_opts.off32_limit & 0x80000000)
  3030. die(_("bad index version '%s'"), val);
  3031. return 0;
  3032. }
  3033. static int option_parse_unpack_unreachable(const struct option *opt,
  3034. const char *arg, int unset)
  3035. {
  3036. if (unset) {
  3037. unpack_unreachable = 0;
  3038. unpack_unreachable_expiration = 0;
  3039. }
  3040. else {
  3041. unpack_unreachable = 1;
  3042. if (arg)
  3043. unpack_unreachable_expiration = approxidate(arg);
  3044. }
  3045. return 0;
  3046. }
  3047. int cmd_pack_objects(int argc, const char **argv, const char *prefix)
  3048. {
  3049. int use_internal_rev_list = 0;
  3050. int shallow = 0;
  3051. int all_progress_implied = 0;
  3052. struct strvec rp = STRVEC_INIT;
  3053. int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
  3054. int rev_list_index = 0;
  3055. struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
  3056. struct option pack_objects_options[] = {
  3057. OPT_SET_INT('q', "quiet", &progress,
  3058. N_("do not show progress meter"), 0),
  3059. OPT_SET_INT(0, "progress", &progress,
  3060. N_("show progress meter"), 1),
  3061. OPT_SET_INT(0, "all-progress", &progress,
  3062. N_("show progress meter during object writing phase"), 2),
  3063. OPT_BOOL(0, "all-progress-implied",
  3064. &all_progress_implied,
  3065. N_("similar to --all-progress when progress meter is shown")),
  3066. OPT_CALLBACK_F(0, "index-version", NULL, N_("<version>[,<offset>]"),
  3067. N_("write the pack index file in the specified idx format version"),
  3068. PARSE_OPT_NONEG, option_parse_index_version),
  3069. OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
  3070. N_("maximum size of each output pack file")),
  3071. OPT_BOOL(0, "local", &local,
  3072. N_("ignore borrowed objects from alternate object store")),
  3073. OPT_BOOL(0, "incremental", &incremental,
  3074. N_("ignore packed objects")),
  3075. OPT_INTEGER(0, "window", &window,
  3076. N_("limit pack window by objects")),
  3077. OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,
  3078. N_("limit pack window by memory in addition to object limit")),
  3079. OPT_INTEGER(0, "depth", &depth,
  3080. N_("maximum length of delta chain allowed in the resulting pack")),
  3081. OPT_BOOL(0, "reuse-delta", &reuse_delta,
  3082. N_("reuse existing deltas")),
  3083. OPT_BOOL(0, "reuse-object", &reuse_object,
  3084. N_("reuse existing objects")),
  3085. OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
  3086. N_("use OFS_DELTA objects")),
  3087. OPT_INTEGER(0, "threads", &delta_search_threads,
  3088. N_("use threads when searching for best delta matches")),
  3089. OPT_BOOL(0, "non-empty", &non_empty,
  3090. N_("do not create an empty pack output")),
  3091. OPT_BOOL(0, "revs", &use_internal_rev_list,
  3092. N_("read revision arguments from standard input")),
  3093. OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked,
  3094. N_("limit the objects to those that are not yet packed"),
  3095. 1, PARSE_OPT_NONEG),
  3096. OPT_SET_INT_F(0, "all", &rev_list_all,
  3097. N_("include objects reachable from any reference"),
  3098. 1, PARSE_OPT_NONEG),
  3099. OPT_SET_INT_F(0, "reflog", &rev_list_reflog,
  3100. N_("include objects referred by reflog entries"),
  3101. 1, PARSE_OPT_NONEG),
  3102. OPT_SET_INT_F(0, "indexed-objects", &rev_list_index,
  3103. N_("include objects referred to by the index"),
  3104. 1, PARSE_OPT_NONEG),
  3105. OPT_BOOL(0, "stdout", &pack_to_stdout,
  3106. N_("output pack to stdout")),
  3107. OPT_BOOL(0, "include-tag", &include_tag,
  3108. N_("include tag objects that refer to objects to be packed")),
  3109. OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
  3110. N_("keep unreachable objects")),
  3111. OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,
  3112. N_("pack loose unreachable objects")),
  3113. OPT_CALLBACK_F(0, "unpack-unreachable", NULL, N_("time"),
  3114. N_("unpack unreachable objects newer than <time>"),
  3115. PARSE_OPT_OPTARG, option_parse_unpack_unreachable),
  3116. OPT_BOOL(0, "sparse", &sparse,
  3117. N_("use the sparse reachability algorithm")),
  3118. OPT_BOOL(0, "thin", &thin,
  3119. N_("create thin packs")),
  3120. OPT_BOOL(0, "shallow", &shallow,
  3121. N_("create packs suitable for shallow fetches")),
  3122. OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
  3123. N_("ignore packs that have companion .keep file")),
  3124. OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
  3125. N_("ignore this pack")),
  3126. OPT_INTEGER(0, "compression", &pack_compression_level,
  3127. N_("pack compression level")),
  3128. OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents,
  3129. N_("do not hide commits by grafts"), 0),
  3130. OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,
  3131. N_("use a bitmap index if available to speed up counting objects")),
  3132. OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index,
  3133. N_("write a bitmap index together with the pack index"),
  3134. WRITE_BITMAP_TRUE),
  3135. OPT_SET_INT_F(0, "write-bitmap-index-quiet",
  3136. &write_bitmap_index,
  3137. N_("write a bitmap index if possible"),
  3138. WRITE_BITMAP_QUIET, PARSE_OPT_HIDDEN),
  3139. OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
  3140. OPT_CALLBACK_F(0, "missing", NULL, N_("action"),
  3141. N_("handling for missing objects"), PARSE_OPT_NONEG,
  3142. option_parse_missing_action),
  3143. OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
  3144. N_("do not pack objects in promisor packfiles")),
  3145. OPT_BOOL(0, "delta-islands", &use_delta_islands,
  3146. N_("respect islands during delta compression")),
  3147. OPT_STRING_LIST(0, "uri-protocol", &uri_protocols,
  3148. N_("protocol"),
  3149. N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
  3150. OPT_END(),
  3151. };
  3152. if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))
  3153. BUG("too many dfs states, increase OE_DFS_STATE_BITS");
  3154. read_replace_refs = 0;
  3155. sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
  3156. prepare_repo_settings(the_repository);
  3157. if (sparse < 0)
  3158. sparse = the_repository->settings.pack_use_sparse;
  3159. reset_pack_idx_option(&pack_idx_opts);
  3160. git_config(git_pack_config, NULL);
  3161. progress = isatty(2);
  3162. argc = parse_options(argc, argv, prefix, pack_objects_options,
  3163. pack_usage, 0);
  3164. if (argc) {
  3165. base_name = argv[0];
  3166. argc--;
  3167. }
  3168. if (pack_to_stdout != !base_name || argc)
  3169. usage_with_options(pack_usage, pack_objects_options);
  3170. if (depth >= (1 << OE_DEPTH_BITS)) {
  3171. warning(_("delta chain depth %d is too deep, forcing %d"),
  3172. depth, (1 << OE_DEPTH_BITS) - 1);
  3173. depth = (1 << OE_DEPTH_BITS) - 1;
  3174. }
  3175. if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) {
  3176. warning(_("pack.deltaCacheLimit is too high, forcing %d"),
  3177. (1U << OE_Z_DELTA_BITS) - 1);
  3178. cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1;
  3179. }
  3180. strvec_push(&rp, "pack-objects");
  3181. if (thin) {
  3182. use_internal_rev_list = 1;
  3183. strvec_push(&rp, shallow
  3184. ? "--objects-edge-aggressive"
  3185. : "--objects-edge");
  3186. } else
  3187. strvec_push(&rp, "--objects");
  3188. if (rev_list_all) {
  3189. use_internal_rev_list = 1;
  3190. strvec_push(&rp, "--all");
  3191. }
  3192. if (rev_list_reflog) {
  3193. use_internal_rev_list = 1;
  3194. strvec_push(&rp, "--reflog");
  3195. }
  3196. if (rev_list_index) {
  3197. use_internal_rev_list = 1;
  3198. strvec_push(&rp, "--indexed-objects");
  3199. }
  3200. if (rev_list_unpacked) {
  3201. use_internal_rev_list = 1;
  3202. strvec_push(&rp, "--unpacked");
  3203. }
  3204. if (exclude_promisor_objects) {
  3205. use_internal_rev_list = 1;
  3206. fetch_if_missing = 0;
  3207. strvec_push(&rp, "--exclude-promisor-objects");
  3208. }
  3209. if (unpack_unreachable || keep_unreachable || pack_loose_unreachable)
  3210. use_internal_rev_list = 1;
  3211. if (!reuse_object)
  3212. reuse_delta = 0;
  3213. if (pack_compression_level == -1)
  3214. pack_compression_level = Z_DEFAULT_COMPRESSION;
  3215. else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)
  3216. die(_("bad pack compression level %d"), pack_compression_level);
  3217. if (!delta_search_threads) /* --threads=0 means autodetect */
  3218. delta_search_threads = online_cpus();
  3219. if (!HAVE_THREADS && delta_search_threads != 1)
  3220. warning(_("no threads support, ignoring --threads"));
  3221. if (!pack_to_stdout && !pack_size_limit)
  3222. pack_size_limit = pack_size_limit_cfg;
  3223. if (pack_to_stdout && pack_size_limit)
  3224. die(_("--max-pack-size cannot be used to build a pack for transfer"));
  3225. if (pack_size_limit && pack_size_limit < 1024*1024) {
  3226. warning(_("minimum pack size limit is 1 MiB"));
  3227. pack_size_limit = 1024*1024;
  3228. }
  3229. if (!pack_to_stdout && thin)
  3230. die(_("--thin cannot be used to build an indexable pack"));
  3231. if (keep_unreachable && unpack_unreachable)
  3232. die(_("--keep-unreachable and --unpack-unreachable are incompatible"));
  3233. if (!rev_list_all || !rev_list_reflog || !rev_list_index)
  3234. unpack_unreachable_expiration = 0;
  3235. if (filter_options.choice) {
  3236. if (!pack_to_stdout)
  3237. die(_("cannot use --filter without --stdout"));
  3238. }
  3239. /*
  3240. * "soft" reasons not to use bitmaps - for on-disk repack by default we want
  3241. *
  3242. * - to produce good pack (with bitmap index not-yet-packed objects are
  3243. * packed in suboptimal order).
  3244. *
  3245. * - to use more robust pack-generation codepath (avoiding possible
  3246. * bugs in bitmap code and possible bitmap index corruption).
  3247. */
  3248. if (!pack_to_stdout)
  3249. use_bitmap_index_default = 0;
  3250. if (use_bitmap_index < 0)
  3251. use_bitmap_index = use_bitmap_index_default;
  3252. /* "hard" reasons not to use bitmaps; these just won't work at all */
  3253. if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow(the_repository))
  3254. use_bitmap_index = 0;
  3255. if (pack_to_stdout || !rev_list_all)
  3256. write_bitmap_index = 0;
  3257. if (use_delta_islands)
  3258. strvec_push(&rp, "--topo-order");
  3259. if (progress && all_progress_implied)
  3260. progress = 2;
  3261. add_extra_kept_packs(&keep_pack_list);
  3262. if (ignore_packed_keep_on_disk) {
  3263. struct packed_git *p;
  3264. for (p = get_all_packs(the_repository); p; p = p->next)
  3265. if (p->pack_local && p->pack_keep)
  3266. break;
  3267. if (!p) /* no keep-able packs found */
  3268. ignore_packed_keep_on_disk = 0;
  3269. }
  3270. if (local) {
  3271. /*
  3272. * unlike ignore_packed_keep_on_disk above, we do not
  3273. * want to unset "local" based on looking at packs, as
  3274. * it also covers non-local objects
  3275. */
  3276. struct packed_git *p;
  3277. for (p = get_all_packs(the_repository); p; p = p->next) {
  3278. if (!p->pack_local) {
  3279. have_non_local_packs = 1;
  3280. break;
  3281. }
  3282. }
  3283. }
  3284. trace2_region_enter("pack-objects", "enumerate-objects",
  3285. the_repository);
  3286. prepare_packing_data(the_repository, &to_pack);
  3287. if (progress)
  3288. progress_state = start_progress(_("Enumerating objects"), 0);
  3289. if (!use_internal_rev_list)
  3290. read_object_list_from_stdin();
  3291. else {
  3292. get_object_list(rp.nr, rp.v);
  3293. strvec_clear(&rp);
  3294. }
  3295. cleanup_preferred_base();
  3296. if (include_tag && nr_result)
  3297. for_each_ref(add_ref_tag, NULL);
  3298. stop_progress(&progress_state);
  3299. trace2_region_leave("pack-objects", "enumerate-objects",
  3300. the_repository);
  3301. if (non_empty && !nr_result)
  3302. return 0;
  3303. if (nr_result) {
  3304. trace2_region_enter("pack-objects", "prepare-pack",
  3305. the_repository);
  3306. prepare_pack(window, depth);
  3307. trace2_region_leave("pack-objects", "prepare-pack",
  3308. the_repository);
  3309. }
  3310. trace2_region_enter("pack-objects", "write-pack-file", the_repository);
  3311. write_excluded_by_configs();
  3312. write_pack_file();
  3313. trace2_region_leave("pack-objects", "write-pack-file", the_repository);
  3314. if (progress)
  3315. fprintf_ln(stderr,
  3316. _("Total %"PRIu32" (delta %"PRIu32"),"
  3317. " reused %"PRIu32" (delta %"PRIu32"),"
  3318. " pack-reused %"PRIu32),
  3319. written, written_delta, reused, reused_delta,
  3320. reuse_packfile_objects);
  3321. return 0;
  3322. }