fast-import.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635
  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "repository.h"
  4. #include "config.h"
  5. #include "lockfile.h"
  6. #include "object.h"
  7. #include "blob.h"
  8. #include "tree.h"
  9. #include "commit.h"
  10. #include "delta.h"
  11. #include "pack.h"
  12. #include "refs.h"
  13. #include "csum-file.h"
  14. #include "quote.h"
  15. #include "dir.h"
  16. #include "run-command.h"
  17. #include "packfile.h"
  18. #include "object-store.h"
  19. #include "mem-pool.h"
  20. #include "commit-reach.h"
  21. #include "khash.h"
  22. #define PACK_ID_BITS 16
  23. #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
  24. #define DEPTH_BITS 13
  25. #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
  26. /*
  27. * We abuse the setuid bit on directories to mean "do not delta".
  28. */
  29. #define NO_DELTA S_ISUID
  30. /*
  31. * The amount of additional space required in order to write an object into the
  32. * current pack. This is the hash lengths at the end of the pack, plus the
  33. * length of one object ID.
  34. */
  35. #define PACK_SIZE_THRESHOLD (the_hash_algo->rawsz * 3)
  36. struct object_entry {
  37. struct pack_idx_entry idx;
  38. struct hashmap_entry ent;
  39. uint32_t type : TYPE_BITS,
  40. pack_id : PACK_ID_BITS,
  41. depth : DEPTH_BITS;
  42. };
  43. static int object_entry_hashcmp(const void *map_data,
  44. const struct hashmap_entry *eptr,
  45. const struct hashmap_entry *entry_or_key,
  46. const void *keydata)
  47. {
  48. const struct object_id *oid = keydata;
  49. const struct object_entry *e1, *e2;
  50. e1 = container_of(eptr, const struct object_entry, ent);
  51. if (oid)
  52. return oidcmp(&e1->idx.oid, oid);
  53. e2 = container_of(entry_or_key, const struct object_entry, ent);
  54. return oidcmp(&e1->idx.oid, &e2->idx.oid);
  55. }
  56. struct object_entry_pool {
  57. struct object_entry_pool *next_pool;
  58. struct object_entry *next_free;
  59. struct object_entry *end;
  60. struct object_entry entries[FLEX_ARRAY]; /* more */
  61. };
  62. struct mark_set {
  63. union {
  64. struct object_id *oids[1024];
  65. struct object_entry *marked[1024];
  66. struct mark_set *sets[1024];
  67. } data;
  68. unsigned int shift;
  69. };
  70. struct last_object {
  71. struct strbuf data;
  72. off_t offset;
  73. unsigned int depth;
  74. unsigned no_swap : 1;
  75. };
  76. struct atom_str {
  77. struct atom_str *next_atom;
  78. unsigned short str_len;
  79. char str_dat[FLEX_ARRAY]; /* more */
  80. };
  81. struct tree_content;
  82. struct tree_entry {
  83. struct tree_content *tree;
  84. struct atom_str *name;
  85. struct tree_entry_ms {
  86. uint16_t mode;
  87. struct object_id oid;
  88. } versions[2];
  89. };
  90. struct tree_content {
  91. unsigned int entry_capacity; /* must match avail_tree_content */
  92. unsigned int entry_count;
  93. unsigned int delta_depth;
  94. struct tree_entry *entries[FLEX_ARRAY]; /* more */
  95. };
  96. struct avail_tree_content {
  97. unsigned int entry_capacity; /* must match tree_content */
  98. struct avail_tree_content *next_avail;
  99. };
  100. struct branch {
  101. struct branch *table_next_branch;
  102. struct branch *active_next_branch;
  103. const char *name;
  104. struct tree_entry branch_tree;
  105. uintmax_t last_commit;
  106. uintmax_t num_notes;
  107. unsigned active : 1;
  108. unsigned delete : 1;
  109. unsigned pack_id : PACK_ID_BITS;
  110. struct object_id oid;
  111. };
  112. struct tag {
  113. struct tag *next_tag;
  114. const char *name;
  115. unsigned int pack_id;
  116. struct object_id oid;
  117. };
  118. struct hash_list {
  119. struct hash_list *next;
  120. struct object_id oid;
  121. };
  122. typedef enum {
  123. WHENSPEC_RAW = 1,
  124. WHENSPEC_RAW_PERMISSIVE,
  125. WHENSPEC_RFC2822,
  126. WHENSPEC_NOW
  127. } whenspec_type;
  128. struct recent_command {
  129. struct recent_command *prev;
  130. struct recent_command *next;
  131. char *buf;
  132. };
  133. typedef void (*mark_set_inserter_t)(struct mark_set *s, struct object_id *oid, uintmax_t mark);
  134. typedef void (*each_mark_fn_t)(uintmax_t mark, void *obj, void *cbp);
  135. /* Configured limits on output */
  136. static unsigned long max_depth = 50;
  137. static off_t max_packsize;
  138. static int unpack_limit = 100;
  139. static int force_update;
  140. /* Stats and misc. counters */
  141. static uintmax_t alloc_count;
  142. static uintmax_t marks_set_count;
  143. static uintmax_t object_count_by_type[1 << TYPE_BITS];
  144. static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
  145. static uintmax_t delta_count_by_type[1 << TYPE_BITS];
  146. static uintmax_t delta_count_attempts_by_type[1 << TYPE_BITS];
  147. static unsigned long object_count;
  148. static unsigned long branch_count;
  149. static unsigned long branch_load_count;
  150. static int failure;
  151. static FILE *pack_edges;
  152. static unsigned int show_stats = 1;
  153. static int global_argc;
  154. static const char **global_argv;
  155. /* Memory pools */
  156. static struct mem_pool fi_mem_pool = {NULL, 2*1024*1024 -
  157. sizeof(struct mp_block), 0 };
  158. /* Atom management */
  159. static unsigned int atom_table_sz = 4451;
  160. static unsigned int atom_cnt;
  161. static struct atom_str **atom_table;
  162. /* The .pack file being generated */
  163. static struct pack_idx_option pack_idx_opts;
  164. static unsigned int pack_id;
  165. static struct hashfile *pack_file;
  166. static struct packed_git *pack_data;
  167. static struct packed_git **all_packs;
  168. static off_t pack_size;
  169. /* Table of objects we've written. */
  170. static unsigned int object_entry_alloc = 5000;
  171. static struct object_entry_pool *blocks;
  172. static struct hashmap object_table;
  173. static struct mark_set *marks;
  174. static const char *export_marks_file;
  175. static const char *import_marks_file;
  176. static int import_marks_file_from_stream;
  177. static int import_marks_file_ignore_missing;
  178. static int import_marks_file_done;
  179. static int relative_marks_paths;
  180. /* Our last blob */
  181. static struct last_object last_blob = { STRBUF_INIT, 0, 0, 0 };
  182. /* Tree management */
  183. static unsigned int tree_entry_alloc = 1000;
  184. static void *avail_tree_entry;
  185. static unsigned int avail_tree_table_sz = 100;
  186. static struct avail_tree_content **avail_tree_table;
  187. static size_t tree_entry_allocd;
  188. static struct strbuf old_tree = STRBUF_INIT;
  189. static struct strbuf new_tree = STRBUF_INIT;
  190. /* Branch data */
  191. static unsigned long max_active_branches = 5;
  192. static unsigned long cur_active_branches;
  193. static unsigned long branch_table_sz = 1039;
  194. static struct branch **branch_table;
  195. static struct branch *active_branches;
  196. /* Tag data */
  197. static struct tag *first_tag;
  198. static struct tag *last_tag;
  199. /* Input stream parsing */
  200. static whenspec_type whenspec = WHENSPEC_RAW;
  201. static struct strbuf command_buf = STRBUF_INIT;
  202. static int unread_command_buf;
  203. static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
  204. static struct recent_command *cmd_tail = &cmd_hist;
  205. static struct recent_command *rc_free;
  206. static unsigned int cmd_save = 100;
  207. static uintmax_t next_mark;
  208. static struct strbuf new_data = STRBUF_INIT;
  209. static int seen_data_command;
  210. static int require_explicit_termination;
  211. static int allow_unsafe_features;
  212. /* Signal handling */
  213. static volatile sig_atomic_t checkpoint_requested;
  214. /* Submodule marks */
  215. static struct string_list sub_marks_from = STRING_LIST_INIT_DUP;
  216. static struct string_list sub_marks_to = STRING_LIST_INIT_DUP;
  217. static kh_oid_map_t *sub_oid_map;
  218. /* Where to write output of cat-blob commands */
  219. static int cat_blob_fd = STDOUT_FILENO;
  220. static void parse_argv(void);
  221. static void parse_get_mark(const char *p);
  222. static void parse_cat_blob(const char *p);
  223. static void parse_ls(const char *p, struct branch *b);
  224. static void for_each_mark(struct mark_set *m, uintmax_t base, each_mark_fn_t callback, void *p)
  225. {
  226. uintmax_t k;
  227. if (m->shift) {
  228. for (k = 0; k < 1024; k++) {
  229. if (m->data.sets[k])
  230. for_each_mark(m->data.sets[k], base + (k << m->shift), callback, p);
  231. }
  232. } else {
  233. for (k = 0; k < 1024; k++) {
  234. if (m->data.marked[k])
  235. callback(base + k, m->data.marked[k], p);
  236. }
  237. }
  238. }
  239. static void dump_marks_fn(uintmax_t mark, void *object, void *cbp) {
  240. struct object_entry *e = object;
  241. FILE *f = cbp;
  242. fprintf(f, ":%" PRIuMAX " %s\n", mark, oid_to_hex(&e->idx.oid));
  243. }
  244. static void write_branch_report(FILE *rpt, struct branch *b)
  245. {
  246. fprintf(rpt, "%s:\n", b->name);
  247. fprintf(rpt, " status :");
  248. if (b->active)
  249. fputs(" active", rpt);
  250. if (b->branch_tree.tree)
  251. fputs(" loaded", rpt);
  252. if (is_null_oid(&b->branch_tree.versions[1].oid))
  253. fputs(" dirty", rpt);
  254. fputc('\n', rpt);
  255. fprintf(rpt, " tip commit : %s\n", oid_to_hex(&b->oid));
  256. fprintf(rpt, " old tree : %s\n",
  257. oid_to_hex(&b->branch_tree.versions[0].oid));
  258. fprintf(rpt, " cur tree : %s\n",
  259. oid_to_hex(&b->branch_tree.versions[1].oid));
  260. fprintf(rpt, " commit clock: %" PRIuMAX "\n", b->last_commit);
  261. fputs(" last pack : ", rpt);
  262. if (b->pack_id < MAX_PACK_ID)
  263. fprintf(rpt, "%u", b->pack_id);
  264. fputc('\n', rpt);
  265. fputc('\n', rpt);
  266. }
  267. static void write_crash_report(const char *err)
  268. {
  269. char *loc = git_pathdup("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
  270. FILE *rpt = fopen(loc, "w");
  271. struct branch *b;
  272. unsigned long lu;
  273. struct recent_command *rc;
  274. if (!rpt) {
  275. error_errno("can't write crash report %s", loc);
  276. free(loc);
  277. return;
  278. }
  279. fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
  280. fprintf(rpt, "fast-import crash report:\n");
  281. fprintf(rpt, " fast-import process: %"PRIuMAX"\n", (uintmax_t) getpid());
  282. fprintf(rpt, " parent process : %"PRIuMAX"\n", (uintmax_t) getppid());
  283. fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_MODE(ISO8601)));
  284. fputc('\n', rpt);
  285. fputs("fatal: ", rpt);
  286. fputs(err, rpt);
  287. fputc('\n', rpt);
  288. fputc('\n', rpt);
  289. fputs("Most Recent Commands Before Crash\n", rpt);
  290. fputs("---------------------------------\n", rpt);
  291. for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
  292. if (rc->next == &cmd_hist)
  293. fputs("* ", rpt);
  294. else
  295. fputs(" ", rpt);
  296. fputs(rc->buf, rpt);
  297. fputc('\n', rpt);
  298. }
  299. fputc('\n', rpt);
  300. fputs("Active Branch LRU\n", rpt);
  301. fputs("-----------------\n", rpt);
  302. fprintf(rpt, " active_branches = %lu cur, %lu max\n",
  303. cur_active_branches,
  304. max_active_branches);
  305. fputc('\n', rpt);
  306. fputs(" pos clock name\n", rpt);
  307. fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
  308. for (b = active_branches, lu = 0; b; b = b->active_next_branch)
  309. fprintf(rpt, " %2lu) %6" PRIuMAX" %s\n",
  310. ++lu, b->last_commit, b->name);
  311. fputc('\n', rpt);
  312. fputs("Inactive Branches\n", rpt);
  313. fputs("-----------------\n", rpt);
  314. for (lu = 0; lu < branch_table_sz; lu++) {
  315. for (b = branch_table[lu]; b; b = b->table_next_branch)
  316. write_branch_report(rpt, b);
  317. }
  318. if (first_tag) {
  319. struct tag *tg;
  320. fputc('\n', rpt);
  321. fputs("Annotated Tags\n", rpt);
  322. fputs("--------------\n", rpt);
  323. for (tg = first_tag; tg; tg = tg->next_tag) {
  324. fputs(oid_to_hex(&tg->oid), rpt);
  325. fputc(' ', rpt);
  326. fputs(tg->name, rpt);
  327. fputc('\n', rpt);
  328. }
  329. }
  330. fputc('\n', rpt);
  331. fputs("Marks\n", rpt);
  332. fputs("-----\n", rpt);
  333. if (export_marks_file)
  334. fprintf(rpt, " exported to %s\n", export_marks_file);
  335. else
  336. for_each_mark(marks, 0, dump_marks_fn, rpt);
  337. fputc('\n', rpt);
  338. fputs("-------------------\n", rpt);
  339. fputs("END OF CRASH REPORT\n", rpt);
  340. fclose(rpt);
  341. free(loc);
  342. }
  343. static void end_packfile(void);
  344. static void unkeep_all_packs(void);
  345. static void dump_marks(void);
  346. static NORETURN void die_nicely(const char *err, va_list params)
  347. {
  348. static int zombie;
  349. char message[2 * PATH_MAX];
  350. vsnprintf(message, sizeof(message), err, params);
  351. fputs("fatal: ", stderr);
  352. fputs(message, stderr);
  353. fputc('\n', stderr);
  354. if (!zombie) {
  355. zombie = 1;
  356. write_crash_report(message);
  357. end_packfile();
  358. unkeep_all_packs();
  359. dump_marks();
  360. }
  361. exit(128);
  362. }
  363. #ifndef SIGUSR1 /* Windows, for example */
  364. static void set_checkpoint_signal(void)
  365. {
  366. }
  367. #else
  368. static void checkpoint_signal(int signo)
  369. {
  370. checkpoint_requested = 1;
  371. }
  372. static void set_checkpoint_signal(void)
  373. {
  374. struct sigaction sa;
  375. memset(&sa, 0, sizeof(sa));
  376. sa.sa_handler = checkpoint_signal;
  377. sigemptyset(&sa.sa_mask);
  378. sa.sa_flags = SA_RESTART;
  379. sigaction(SIGUSR1, &sa, NULL);
  380. }
  381. #endif
  382. static void alloc_objects(unsigned int cnt)
  383. {
  384. struct object_entry_pool *b;
  385. b = xmalloc(sizeof(struct object_entry_pool)
  386. + cnt * sizeof(struct object_entry));
  387. b->next_pool = blocks;
  388. b->next_free = b->entries;
  389. b->end = b->entries + cnt;
  390. blocks = b;
  391. alloc_count += cnt;
  392. }
  393. static struct object_entry *new_object(struct object_id *oid)
  394. {
  395. struct object_entry *e;
  396. if (blocks->next_free == blocks->end)
  397. alloc_objects(object_entry_alloc);
  398. e = blocks->next_free++;
  399. oidcpy(&e->idx.oid, oid);
  400. return e;
  401. }
  402. static struct object_entry *find_object(struct object_id *oid)
  403. {
  404. return hashmap_get_entry_from_hash(&object_table, oidhash(oid), oid,
  405. struct object_entry, ent);
  406. }
  407. static struct object_entry *insert_object(struct object_id *oid)
  408. {
  409. struct object_entry *e;
  410. unsigned int hash = oidhash(oid);
  411. e = hashmap_get_entry_from_hash(&object_table, hash, oid,
  412. struct object_entry, ent);
  413. if (!e) {
  414. e = new_object(oid);
  415. e->idx.offset = 0;
  416. hashmap_entry_init(&e->ent, hash);
  417. hashmap_add(&object_table, &e->ent);
  418. }
  419. return e;
  420. }
  421. static void invalidate_pack_id(unsigned int id)
  422. {
  423. unsigned long lu;
  424. struct tag *t;
  425. struct hashmap_iter iter;
  426. struct object_entry *e;
  427. hashmap_for_each_entry(&object_table, &iter, e, ent) {
  428. if (e->pack_id == id)
  429. e->pack_id = MAX_PACK_ID;
  430. }
  431. for (lu = 0; lu < branch_table_sz; lu++) {
  432. struct branch *b;
  433. for (b = branch_table[lu]; b; b = b->table_next_branch)
  434. if (b->pack_id == id)
  435. b->pack_id = MAX_PACK_ID;
  436. }
  437. for (t = first_tag; t; t = t->next_tag)
  438. if (t->pack_id == id)
  439. t->pack_id = MAX_PACK_ID;
  440. }
  441. static unsigned int hc_str(const char *s, size_t len)
  442. {
  443. unsigned int r = 0;
  444. while (len-- > 0)
  445. r = r * 31 + *s++;
  446. return r;
  447. }
  448. static void insert_mark(struct mark_set *s, uintmax_t idnum, struct object_entry *oe)
  449. {
  450. while ((idnum >> s->shift) >= 1024) {
  451. s = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
  452. s->shift = marks->shift + 10;
  453. s->data.sets[0] = marks;
  454. marks = s;
  455. }
  456. while (s->shift) {
  457. uintmax_t i = idnum >> s->shift;
  458. idnum -= i << s->shift;
  459. if (!s->data.sets[i]) {
  460. s->data.sets[i] = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
  461. s->data.sets[i]->shift = s->shift - 10;
  462. }
  463. s = s->data.sets[i];
  464. }
  465. if (!s->data.marked[idnum])
  466. marks_set_count++;
  467. s->data.marked[idnum] = oe;
  468. }
  469. static void *find_mark(struct mark_set *s, uintmax_t idnum)
  470. {
  471. uintmax_t orig_idnum = idnum;
  472. struct object_entry *oe = NULL;
  473. if ((idnum >> s->shift) < 1024) {
  474. while (s && s->shift) {
  475. uintmax_t i = idnum >> s->shift;
  476. idnum -= i << s->shift;
  477. s = s->data.sets[i];
  478. }
  479. if (s)
  480. oe = s->data.marked[idnum];
  481. }
  482. if (!oe)
  483. die("mark :%" PRIuMAX " not declared", orig_idnum);
  484. return oe;
  485. }
  486. static struct atom_str *to_atom(const char *s, unsigned short len)
  487. {
  488. unsigned int hc = hc_str(s, len) % atom_table_sz;
  489. struct atom_str *c;
  490. for (c = atom_table[hc]; c; c = c->next_atom)
  491. if (c->str_len == len && !strncmp(s, c->str_dat, len))
  492. return c;
  493. c = mem_pool_alloc(&fi_mem_pool, sizeof(struct atom_str) + len + 1);
  494. c->str_len = len;
  495. memcpy(c->str_dat, s, len);
  496. c->str_dat[len] = 0;
  497. c->next_atom = atom_table[hc];
  498. atom_table[hc] = c;
  499. atom_cnt++;
  500. return c;
  501. }
  502. static struct branch *lookup_branch(const char *name)
  503. {
  504. unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
  505. struct branch *b;
  506. for (b = branch_table[hc]; b; b = b->table_next_branch)
  507. if (!strcmp(name, b->name))
  508. return b;
  509. return NULL;
  510. }
  511. static struct branch *new_branch(const char *name)
  512. {
  513. unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
  514. struct branch *b = lookup_branch(name);
  515. if (b)
  516. die("Invalid attempt to create duplicate branch: %s", name);
  517. if (check_refname_format(name, REFNAME_ALLOW_ONELEVEL))
  518. die("Branch name doesn't conform to GIT standards: %s", name);
  519. b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch));
  520. b->name = mem_pool_strdup(&fi_mem_pool, name);
  521. b->table_next_branch = branch_table[hc];
  522. b->branch_tree.versions[0].mode = S_IFDIR;
  523. b->branch_tree.versions[1].mode = S_IFDIR;
  524. b->num_notes = 0;
  525. b->active = 0;
  526. b->pack_id = MAX_PACK_ID;
  527. branch_table[hc] = b;
  528. branch_count++;
  529. return b;
  530. }
  531. static unsigned int hc_entries(unsigned int cnt)
  532. {
  533. cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
  534. return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
  535. }
  536. static struct tree_content *new_tree_content(unsigned int cnt)
  537. {
  538. struct avail_tree_content *f, *l = NULL;
  539. struct tree_content *t;
  540. unsigned int hc = hc_entries(cnt);
  541. for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
  542. if (f->entry_capacity >= cnt)
  543. break;
  544. if (f) {
  545. if (l)
  546. l->next_avail = f->next_avail;
  547. else
  548. avail_tree_table[hc] = f->next_avail;
  549. } else {
  550. cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
  551. f = mem_pool_alloc(&fi_mem_pool, sizeof(*t) + sizeof(t->entries[0]) * cnt);
  552. f->entry_capacity = cnt;
  553. }
  554. t = (struct tree_content*)f;
  555. t->entry_count = 0;
  556. t->delta_depth = 0;
  557. return t;
  558. }
  559. static void release_tree_entry(struct tree_entry *e);
  560. static void release_tree_content(struct tree_content *t)
  561. {
  562. struct avail_tree_content *f = (struct avail_tree_content*)t;
  563. unsigned int hc = hc_entries(f->entry_capacity);
  564. f->next_avail = avail_tree_table[hc];
  565. avail_tree_table[hc] = f;
  566. }
  567. static void release_tree_content_recursive(struct tree_content *t)
  568. {
  569. unsigned int i;
  570. for (i = 0; i < t->entry_count; i++)
  571. release_tree_entry(t->entries[i]);
  572. release_tree_content(t);
  573. }
  574. static struct tree_content *grow_tree_content(
  575. struct tree_content *t,
  576. int amt)
  577. {
  578. struct tree_content *r = new_tree_content(t->entry_count + amt);
  579. r->entry_count = t->entry_count;
  580. r->delta_depth = t->delta_depth;
  581. COPY_ARRAY(r->entries, t->entries, t->entry_count);
  582. release_tree_content(t);
  583. return r;
  584. }
  585. static struct tree_entry *new_tree_entry(void)
  586. {
  587. struct tree_entry *e;
  588. if (!avail_tree_entry) {
  589. unsigned int n = tree_entry_alloc;
  590. tree_entry_allocd += n * sizeof(struct tree_entry);
  591. ALLOC_ARRAY(e, n);
  592. avail_tree_entry = e;
  593. while (n-- > 1) {
  594. *((void**)e) = e + 1;
  595. e++;
  596. }
  597. *((void**)e) = NULL;
  598. }
  599. e = avail_tree_entry;
  600. avail_tree_entry = *((void**)e);
  601. return e;
  602. }
  603. static void release_tree_entry(struct tree_entry *e)
  604. {
  605. if (e->tree)
  606. release_tree_content_recursive(e->tree);
  607. *((void**)e) = avail_tree_entry;
  608. avail_tree_entry = e;
  609. }
  610. static struct tree_content *dup_tree_content(struct tree_content *s)
  611. {
  612. struct tree_content *d;
  613. struct tree_entry *a, *b;
  614. unsigned int i;
  615. if (!s)
  616. return NULL;
  617. d = new_tree_content(s->entry_count);
  618. for (i = 0; i < s->entry_count; i++) {
  619. a = s->entries[i];
  620. b = new_tree_entry();
  621. memcpy(b, a, sizeof(*a));
  622. if (a->tree && is_null_oid(&b->versions[1].oid))
  623. b->tree = dup_tree_content(a->tree);
  624. else
  625. b->tree = NULL;
  626. d->entries[i] = b;
  627. }
  628. d->entry_count = s->entry_count;
  629. d->delta_depth = s->delta_depth;
  630. return d;
  631. }
  632. static void start_packfile(void)
  633. {
  634. struct strbuf tmp_file = STRBUF_INIT;
  635. struct packed_git *p;
  636. int pack_fd;
  637. pack_fd = odb_mkstemp(&tmp_file, "pack/tmp_pack_XXXXXX");
  638. FLEX_ALLOC_STR(p, pack_name, tmp_file.buf);
  639. strbuf_release(&tmp_file);
  640. p->pack_fd = pack_fd;
  641. p->do_not_close = 1;
  642. pack_file = hashfd(pack_fd, p->pack_name);
  643. pack_data = p;
  644. pack_size = write_pack_header(pack_file, 0);
  645. object_count = 0;
  646. REALLOC_ARRAY(all_packs, pack_id + 1);
  647. all_packs[pack_id] = p;
  648. }
  649. static const char *create_index(void)
  650. {
  651. const char *tmpfile;
  652. struct pack_idx_entry **idx, **c, **last;
  653. struct object_entry *e;
  654. struct object_entry_pool *o;
  655. /* Build the table of object IDs. */
  656. ALLOC_ARRAY(idx, object_count);
  657. c = idx;
  658. for (o = blocks; o; o = o->next_pool)
  659. for (e = o->next_free; e-- != o->entries;)
  660. if (pack_id == e->pack_id)
  661. *c++ = &e->idx;
  662. last = idx + object_count;
  663. if (c != last)
  664. die("internal consistency error creating the index");
  665. tmpfile = write_idx_file(NULL, idx, object_count, &pack_idx_opts,
  666. pack_data->hash);
  667. free(idx);
  668. return tmpfile;
  669. }
  670. static char *keep_pack(const char *curr_index_name)
  671. {
  672. static const char *keep_msg = "fast-import";
  673. struct strbuf name = STRBUF_INIT;
  674. int keep_fd;
  675. odb_pack_name(&name, pack_data->hash, "keep");
  676. keep_fd = odb_pack_keep(name.buf);
  677. if (keep_fd < 0)
  678. die_errno("cannot create keep file");
  679. write_or_die(keep_fd, keep_msg, strlen(keep_msg));
  680. if (close(keep_fd))
  681. die_errno("failed to write keep file");
  682. odb_pack_name(&name, pack_data->hash, "pack");
  683. if (finalize_object_file(pack_data->pack_name, name.buf))
  684. die("cannot store pack file");
  685. odb_pack_name(&name, pack_data->hash, "idx");
  686. if (finalize_object_file(curr_index_name, name.buf))
  687. die("cannot store index file");
  688. free((void *)curr_index_name);
  689. return strbuf_detach(&name, NULL);
  690. }
  691. static void unkeep_all_packs(void)
  692. {
  693. struct strbuf name = STRBUF_INIT;
  694. int k;
  695. for (k = 0; k < pack_id; k++) {
  696. struct packed_git *p = all_packs[k];
  697. odb_pack_name(&name, p->hash, "keep");
  698. unlink_or_warn(name.buf);
  699. }
  700. strbuf_release(&name);
  701. }
  702. static int loosen_small_pack(const struct packed_git *p)
  703. {
  704. struct child_process unpack = CHILD_PROCESS_INIT;
  705. if (lseek(p->pack_fd, 0, SEEK_SET) < 0)
  706. die_errno("Failed seeking to start of '%s'", p->pack_name);
  707. unpack.in = p->pack_fd;
  708. unpack.git_cmd = 1;
  709. unpack.stdout_to_stderr = 1;
  710. strvec_push(&unpack.args, "unpack-objects");
  711. if (!show_stats)
  712. strvec_push(&unpack.args, "-q");
  713. return run_command(&unpack);
  714. }
  715. static void end_packfile(void)
  716. {
  717. static int running;
  718. if (running || !pack_data)
  719. return;
  720. running = 1;
  721. clear_delta_base_cache();
  722. if (object_count) {
  723. struct packed_git *new_p;
  724. struct object_id cur_pack_oid;
  725. char *idx_name;
  726. int i;
  727. struct branch *b;
  728. struct tag *t;
  729. close_pack_windows(pack_data);
  730. finalize_hashfile(pack_file, cur_pack_oid.hash, 0);
  731. fixup_pack_header_footer(pack_data->pack_fd, pack_data->hash,
  732. pack_data->pack_name, object_count,
  733. cur_pack_oid.hash, pack_size);
  734. if (object_count <= unpack_limit) {
  735. if (!loosen_small_pack(pack_data)) {
  736. invalidate_pack_id(pack_id);
  737. goto discard_pack;
  738. }
  739. }
  740. close(pack_data->pack_fd);
  741. idx_name = keep_pack(create_index());
  742. /* Register the packfile with core git's machinery. */
  743. new_p = add_packed_git(idx_name, strlen(idx_name), 1);
  744. if (!new_p)
  745. die("core git rejected index %s", idx_name);
  746. all_packs[pack_id] = new_p;
  747. install_packed_git(the_repository, new_p);
  748. free(idx_name);
  749. /* Print the boundary */
  750. if (pack_edges) {
  751. fprintf(pack_edges, "%s:", new_p->pack_name);
  752. for (i = 0; i < branch_table_sz; i++) {
  753. for (b = branch_table[i]; b; b = b->table_next_branch) {
  754. if (b->pack_id == pack_id)
  755. fprintf(pack_edges, " %s",
  756. oid_to_hex(&b->oid));
  757. }
  758. }
  759. for (t = first_tag; t; t = t->next_tag) {
  760. if (t->pack_id == pack_id)
  761. fprintf(pack_edges, " %s",
  762. oid_to_hex(&t->oid));
  763. }
  764. fputc('\n', pack_edges);
  765. fflush(pack_edges);
  766. }
  767. pack_id++;
  768. }
  769. else {
  770. discard_pack:
  771. close(pack_data->pack_fd);
  772. unlink_or_warn(pack_data->pack_name);
  773. }
  774. FREE_AND_NULL(pack_data);
  775. running = 0;
  776. /* We can't carry a delta across packfiles. */
  777. strbuf_release(&last_blob.data);
  778. last_blob.offset = 0;
  779. last_blob.depth = 0;
  780. }
  781. static void cycle_packfile(void)
  782. {
  783. end_packfile();
  784. start_packfile();
  785. }
  786. static int store_object(
  787. enum object_type type,
  788. struct strbuf *dat,
  789. struct last_object *last,
  790. struct object_id *oidout,
  791. uintmax_t mark)
  792. {
  793. void *out, *delta;
  794. struct object_entry *e;
  795. unsigned char hdr[96];
  796. struct object_id oid;
  797. unsigned long hdrlen, deltalen;
  798. git_hash_ctx c;
  799. git_zstream s;
  800. hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu",
  801. type_name(type), (unsigned long)dat->len) + 1;
  802. the_hash_algo->init_fn(&c);
  803. the_hash_algo->update_fn(&c, hdr, hdrlen);
  804. the_hash_algo->update_fn(&c, dat->buf, dat->len);
  805. the_hash_algo->final_fn(oid.hash, &c);
  806. if (oidout)
  807. oidcpy(oidout, &oid);
  808. e = insert_object(&oid);
  809. if (mark)
  810. insert_mark(marks, mark, e);
  811. if (e->idx.offset) {
  812. duplicate_count_by_type[type]++;
  813. return 1;
  814. } else if (find_sha1_pack(oid.hash,
  815. get_all_packs(the_repository))) {
  816. e->type = type;
  817. e->pack_id = MAX_PACK_ID;
  818. e->idx.offset = 1; /* just not zero! */
  819. duplicate_count_by_type[type]++;
  820. return 1;
  821. }
  822. if (last && last->data.len && last->data.buf && last->depth < max_depth
  823. && dat->len > the_hash_algo->rawsz) {
  824. delta_count_attempts_by_type[type]++;
  825. delta = diff_delta(last->data.buf, last->data.len,
  826. dat->buf, dat->len,
  827. &deltalen, dat->len - the_hash_algo->rawsz);
  828. } else
  829. delta = NULL;
  830. git_deflate_init(&s, pack_compression_level);
  831. if (delta) {
  832. s.next_in = delta;
  833. s.avail_in = deltalen;
  834. } else {
  835. s.next_in = (void *)dat->buf;
  836. s.avail_in = dat->len;
  837. }
  838. s.avail_out = git_deflate_bound(&s, s.avail_in);
  839. s.next_out = out = xmalloc(s.avail_out);
  840. while (git_deflate(&s, Z_FINISH) == Z_OK)
  841. ; /* nothing */
  842. git_deflate_end(&s);
  843. /* Determine if we should auto-checkpoint. */
  844. if ((max_packsize
  845. && (pack_size + PACK_SIZE_THRESHOLD + s.total_out) > max_packsize)
  846. || (pack_size + PACK_SIZE_THRESHOLD + s.total_out) < pack_size) {
  847. /* This new object needs to *not* have the current pack_id. */
  848. e->pack_id = pack_id + 1;
  849. cycle_packfile();
  850. /* We cannot carry a delta into the new pack. */
  851. if (delta) {
  852. FREE_AND_NULL(delta);
  853. git_deflate_init(&s, pack_compression_level);
  854. s.next_in = (void *)dat->buf;
  855. s.avail_in = dat->len;
  856. s.avail_out = git_deflate_bound(&s, s.avail_in);
  857. s.next_out = out = xrealloc(out, s.avail_out);
  858. while (git_deflate(&s, Z_FINISH) == Z_OK)
  859. ; /* nothing */
  860. git_deflate_end(&s);
  861. }
  862. }
  863. e->type = type;
  864. e->pack_id = pack_id;
  865. e->idx.offset = pack_size;
  866. object_count++;
  867. object_count_by_type[type]++;
  868. crc32_begin(pack_file);
  869. if (delta) {
  870. off_t ofs = e->idx.offset - last->offset;
  871. unsigned pos = sizeof(hdr) - 1;
  872. delta_count_by_type[type]++;
  873. e->depth = last->depth + 1;
  874. hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr),
  875. OBJ_OFS_DELTA, deltalen);
  876. hashwrite(pack_file, hdr, hdrlen);
  877. pack_size += hdrlen;
  878. hdr[pos] = ofs & 127;
  879. while (ofs >>= 7)
  880. hdr[--pos] = 128 | (--ofs & 127);
  881. hashwrite(pack_file, hdr + pos, sizeof(hdr) - pos);
  882. pack_size += sizeof(hdr) - pos;
  883. } else {
  884. e->depth = 0;
  885. hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr),
  886. type, dat->len);
  887. hashwrite(pack_file, hdr, hdrlen);
  888. pack_size += hdrlen;
  889. }
  890. hashwrite(pack_file, out, s.total_out);
  891. pack_size += s.total_out;
  892. e->idx.crc32 = crc32_end(pack_file);
  893. free(out);
  894. free(delta);
  895. if (last) {
  896. if (last->no_swap) {
  897. last->data = *dat;
  898. } else {
  899. strbuf_swap(&last->data, dat);
  900. }
  901. last->offset = e->idx.offset;
  902. last->depth = e->depth;
  903. }
  904. return 0;
  905. }
  906. static void truncate_pack(struct hashfile_checkpoint *checkpoint)
  907. {
  908. if (hashfile_truncate(pack_file, checkpoint))
  909. die_errno("cannot truncate pack to skip duplicate");
  910. pack_size = checkpoint->offset;
  911. }
  912. static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
  913. {
  914. size_t in_sz = 64 * 1024, out_sz = 64 * 1024;
  915. unsigned char *in_buf = xmalloc(in_sz);
  916. unsigned char *out_buf = xmalloc(out_sz);
  917. struct object_entry *e;
  918. struct object_id oid;
  919. unsigned long hdrlen;
  920. off_t offset;
  921. git_hash_ctx c;
  922. git_zstream s;
  923. struct hashfile_checkpoint checkpoint;
  924. int status = Z_OK;
  925. /* Determine if we should auto-checkpoint. */
  926. if ((max_packsize
  927. && (pack_size + PACK_SIZE_THRESHOLD + len) > max_packsize)
  928. || (pack_size + PACK_SIZE_THRESHOLD + len) < pack_size)
  929. cycle_packfile();
  930. hashfile_checkpoint(pack_file, &checkpoint);
  931. offset = checkpoint.offset;
  932. hdrlen = xsnprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1;
  933. the_hash_algo->init_fn(&c);
  934. the_hash_algo->update_fn(&c, out_buf, hdrlen);
  935. crc32_begin(pack_file);
  936. git_deflate_init(&s, pack_compression_level);
  937. hdrlen = encode_in_pack_object_header(out_buf, out_sz, OBJ_BLOB, len);
  938. s.next_out = out_buf + hdrlen;
  939. s.avail_out = out_sz - hdrlen;
  940. while (status != Z_STREAM_END) {
  941. if (0 < len && !s.avail_in) {
  942. size_t cnt = in_sz < len ? in_sz : (size_t)len;
  943. size_t n = fread(in_buf, 1, cnt, stdin);
  944. if (!n && feof(stdin))
  945. die("EOF in data (%" PRIuMAX " bytes remaining)", len);
  946. the_hash_algo->update_fn(&c, in_buf, n);
  947. s.next_in = in_buf;
  948. s.avail_in = n;
  949. len -= n;
  950. }
  951. status = git_deflate(&s, len ? 0 : Z_FINISH);
  952. if (!s.avail_out || status == Z_STREAM_END) {
  953. size_t n = s.next_out - out_buf;
  954. hashwrite(pack_file, out_buf, n);
  955. pack_size += n;
  956. s.next_out = out_buf;
  957. s.avail_out = out_sz;
  958. }
  959. switch (status) {
  960. case Z_OK:
  961. case Z_BUF_ERROR:
  962. case Z_STREAM_END:
  963. continue;
  964. default:
  965. die("unexpected deflate failure: %d", status);
  966. }
  967. }
  968. git_deflate_end(&s);
  969. the_hash_algo->final_fn(oid.hash, &c);
  970. if (oidout)
  971. oidcpy(oidout, &oid);
  972. e = insert_object(&oid);
  973. if (mark)
  974. insert_mark(marks, mark, e);
  975. if (e->idx.offset) {
  976. duplicate_count_by_type[OBJ_BLOB]++;
  977. truncate_pack(&checkpoint);
  978. } else if (find_sha1_pack(oid.hash,
  979. get_all_packs(the_repository))) {
  980. e->type = OBJ_BLOB;
  981. e->pack_id = MAX_PACK_ID;
  982. e->idx.offset = 1; /* just not zero! */
  983. duplicate_count_by_type[OBJ_BLOB]++;
  984. truncate_pack(&checkpoint);
  985. } else {
  986. e->depth = 0;
  987. e->type = OBJ_BLOB;
  988. e->pack_id = pack_id;
  989. e->idx.offset = offset;
  990. e->idx.crc32 = crc32_end(pack_file);
  991. object_count++;
  992. object_count_by_type[OBJ_BLOB]++;
  993. }
  994. free(in_buf);
  995. free(out_buf);
  996. }
  997. /* All calls must be guarded by find_object() or find_mark() to
  998. * ensure the 'struct object_entry' passed was written by this
  999. * process instance. We unpack the entry by the offset, avoiding
  1000. * the need for the corresponding .idx file. This unpacking rule
  1001. * works because we only use OBJ_REF_DELTA within the packfiles
  1002. * created by fast-import.
  1003. *
  1004. * oe must not be NULL. Such an oe usually comes from giving
  1005. * an unknown SHA-1 to find_object() or an undefined mark to
  1006. * find_mark(). Callers must test for this condition and use
  1007. * the standard read_sha1_file() when it happens.
  1008. *
  1009. * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
  1010. * find_mark(), where the mark was reloaded from an existing marks
  1011. * file and is referencing an object that this fast-import process
  1012. * instance did not write out to a packfile. Callers must test for
  1013. * this condition and use read_sha1_file() instead.
  1014. */
  1015. static void *gfi_unpack_entry(
  1016. struct object_entry *oe,
  1017. unsigned long *sizep)
  1018. {
  1019. enum object_type type;
  1020. struct packed_git *p = all_packs[oe->pack_id];
  1021. if (p == pack_data && p->pack_size < (pack_size + the_hash_algo->rawsz)) {
  1022. /* The object is stored in the packfile we are writing to
  1023. * and we have modified it since the last time we scanned
  1024. * back to read a previously written object. If an old
  1025. * window covered [p->pack_size, p->pack_size + rawsz) its
  1026. * data is stale and is not valid. Closing all windows
  1027. * and updating the packfile length ensures we can read
  1028. * the newly written data.
  1029. */
  1030. close_pack_windows(p);
  1031. hashflush(pack_file);
  1032. /* We have to offer rawsz bytes additional on the end of
  1033. * the packfile as the core unpacker code assumes the
  1034. * footer is present at the file end and must promise
  1035. * at least rawsz bytes within any window it maps. But
  1036. * we don't actually create the footer here.
  1037. */
  1038. p->pack_size = pack_size + the_hash_algo->rawsz;
  1039. }
  1040. return unpack_entry(the_repository, p, oe->idx.offset, &type, sizep);
  1041. }
  1042. static const char *get_mode(const char *str, uint16_t *modep)
  1043. {
  1044. unsigned char c;
  1045. uint16_t mode = 0;
  1046. while ((c = *str++) != ' ') {
  1047. if (c < '0' || c > '7')
  1048. return NULL;
  1049. mode = (mode << 3) + (c - '0');
  1050. }
  1051. *modep = mode;
  1052. return str;
  1053. }
  1054. static void load_tree(struct tree_entry *root)
  1055. {
  1056. struct object_id *oid = &root->versions[1].oid;
  1057. struct object_entry *myoe;
  1058. struct tree_content *t;
  1059. unsigned long size;
  1060. char *buf;
  1061. const char *c;
  1062. root->tree = t = new_tree_content(8);
  1063. if (is_null_oid(oid))
  1064. return;
  1065. myoe = find_object(oid);
  1066. if (myoe && myoe->pack_id != MAX_PACK_ID) {
  1067. if (myoe->type != OBJ_TREE)
  1068. die("Not a tree: %s", oid_to_hex(oid));
  1069. t->delta_depth = myoe->depth;
  1070. buf = gfi_unpack_entry(myoe, &size);
  1071. if (!buf)
  1072. die("Can't load tree %s", oid_to_hex(oid));
  1073. } else {
  1074. enum object_type type;
  1075. buf = read_object_file(oid, &type, &size);
  1076. if (!buf || type != OBJ_TREE)
  1077. die("Can't load tree %s", oid_to_hex(oid));
  1078. }
  1079. c = buf;
  1080. while (c != (buf + size)) {
  1081. struct tree_entry *e = new_tree_entry();
  1082. if (t->entry_count == t->entry_capacity)
  1083. root->tree = t = grow_tree_content(t, t->entry_count);
  1084. t->entries[t->entry_count++] = e;
  1085. e->tree = NULL;
  1086. c = get_mode(c, &e->versions[1].mode);
  1087. if (!c)
  1088. die("Corrupt mode in %s", oid_to_hex(oid));
  1089. e->versions[0].mode = e->versions[1].mode;
  1090. e->name = to_atom(c, strlen(c));
  1091. c += e->name->str_len + 1;
  1092. hashcpy(e->versions[0].oid.hash, (unsigned char *)c);
  1093. hashcpy(e->versions[1].oid.hash, (unsigned char *)c);
  1094. c += the_hash_algo->rawsz;
  1095. }
  1096. free(buf);
  1097. }
  1098. static int tecmp0 (const void *_a, const void *_b)
  1099. {
  1100. struct tree_entry *a = *((struct tree_entry**)_a);
  1101. struct tree_entry *b = *((struct tree_entry**)_b);
  1102. return base_name_compare(
  1103. a->name->str_dat, a->name->str_len, a->versions[0].mode,
  1104. b->name->str_dat, b->name->str_len, b->versions[0].mode);
  1105. }
  1106. static int tecmp1 (const void *_a, const void *_b)
  1107. {
  1108. struct tree_entry *a = *((struct tree_entry**)_a);
  1109. struct tree_entry *b = *((struct tree_entry**)_b);
  1110. return base_name_compare(
  1111. a->name->str_dat, a->name->str_len, a->versions[1].mode,
  1112. b->name->str_dat, b->name->str_len, b->versions[1].mode);
  1113. }
  1114. static void mktree(struct tree_content *t, int v, struct strbuf *b)
  1115. {
  1116. size_t maxlen = 0;
  1117. unsigned int i;
  1118. if (!v)
  1119. QSORT(t->entries, t->entry_count, tecmp0);
  1120. else
  1121. QSORT(t->entries, t->entry_count, tecmp1);
  1122. for (i = 0; i < t->entry_count; i++) {
  1123. if (t->entries[i]->versions[v].mode)
  1124. maxlen += t->entries[i]->name->str_len + 34;
  1125. }
  1126. strbuf_reset(b);
  1127. strbuf_grow(b, maxlen);
  1128. for (i = 0; i < t->entry_count; i++) {
  1129. struct tree_entry *e = t->entries[i];
  1130. if (!e->versions[v].mode)
  1131. continue;
  1132. strbuf_addf(b, "%o %s%c",
  1133. (unsigned int)(e->versions[v].mode & ~NO_DELTA),
  1134. e->name->str_dat, '\0');
  1135. strbuf_add(b, e->versions[v].oid.hash, the_hash_algo->rawsz);
  1136. }
  1137. }
  1138. static void store_tree(struct tree_entry *root)
  1139. {
  1140. struct tree_content *t;
  1141. unsigned int i, j, del;
  1142. struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
  1143. struct object_entry *le = NULL;
  1144. if (!is_null_oid(&root->versions[1].oid))
  1145. return;
  1146. if (!root->tree)
  1147. load_tree(root);
  1148. t = root->tree;
  1149. for (i = 0; i < t->entry_count; i++) {
  1150. if (t->entries[i]->tree)
  1151. store_tree(t->entries[i]);
  1152. }
  1153. if (!(root->versions[0].mode & NO_DELTA))
  1154. le = find_object(&root->versions[0].oid);
  1155. if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) {
  1156. mktree(t, 0, &old_tree);
  1157. lo.data = old_tree;
  1158. lo.offset = le->idx.offset;
  1159. lo.depth = t->delta_depth;
  1160. }
  1161. mktree(t, 1, &new_tree);
  1162. store_object(OBJ_TREE, &new_tree, &lo, &root->versions[1].oid, 0);
  1163. t->delta_depth = lo.depth;
  1164. for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
  1165. struct tree_entry *e = t->entries[i];
  1166. if (e->versions[1].mode) {
  1167. e->versions[0].mode = e->versions[1].mode;
  1168. oidcpy(&e->versions[0].oid, &e->versions[1].oid);
  1169. t->entries[j++] = e;
  1170. } else {
  1171. release_tree_entry(e);
  1172. del++;
  1173. }
  1174. }
  1175. t->entry_count -= del;
  1176. }
  1177. static void tree_content_replace(
  1178. struct tree_entry *root,
  1179. const struct object_id *oid,
  1180. const uint16_t mode,
  1181. struct tree_content *newtree)
  1182. {
  1183. if (!S_ISDIR(mode))
  1184. die("Root cannot be a non-directory");
  1185. oidclr(&root->versions[0].oid);
  1186. oidcpy(&root->versions[1].oid, oid);
  1187. if (root->tree)
  1188. release_tree_content_recursive(root->tree);
  1189. root->tree = newtree;
  1190. }
  1191. static int tree_content_set(
  1192. struct tree_entry *root,
  1193. const char *p,
  1194. const struct object_id *oid,
  1195. const uint16_t mode,
  1196. struct tree_content *subtree)
  1197. {
  1198. struct tree_content *t;
  1199. const char *slash1;
  1200. unsigned int i, n;
  1201. struct tree_entry *e;
  1202. slash1 = strchrnul(p, '/');
  1203. n = slash1 - p;
  1204. if (!n)
  1205. die("Empty path component found in input");
  1206. if (!*slash1 && !S_ISDIR(mode) && subtree)
  1207. die("Non-directories cannot have subtrees");
  1208. if (!root->tree)
  1209. load_tree(root);
  1210. t = root->tree;
  1211. for (i = 0; i < t->entry_count; i++) {
  1212. e = t->entries[i];
  1213. if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
  1214. if (!*slash1) {
  1215. if (!S_ISDIR(mode)
  1216. && e->versions[1].mode == mode
  1217. && oideq(&e->versions[1].oid, oid))
  1218. return 0;
  1219. e->versions[1].mode = mode;
  1220. oidcpy(&e->versions[1].oid, oid);
  1221. if (e->tree)
  1222. release_tree_content_recursive(e->tree);
  1223. e->tree = subtree;
  1224. /*
  1225. * We need to leave e->versions[0].sha1 alone
  1226. * to avoid modifying the preimage tree used
  1227. * when writing out the parent directory.
  1228. * But after replacing the subdir with a
  1229. * completely different one, it's not a good
  1230. * delta base any more, and besides, we've
  1231. * thrown away the tree entries needed to
  1232. * make a delta against it.
  1233. *
  1234. * So let's just explicitly disable deltas
  1235. * for the subtree.
  1236. */
  1237. if (S_ISDIR(e->versions[0].mode))
  1238. e->versions[0].mode |= NO_DELTA;
  1239. oidclr(&root->versions[1].oid);
  1240. return 1;
  1241. }
  1242. if (!S_ISDIR(e->versions[1].mode)) {
  1243. e->tree = new_tree_content(8);
  1244. e->versions[1].mode = S_IFDIR;
  1245. }
  1246. if (!e->tree)
  1247. load_tree(e);
  1248. if (tree_content_set(e, slash1 + 1, oid, mode, subtree)) {
  1249. oidclr(&root->versions[1].oid);
  1250. return 1;
  1251. }
  1252. return 0;
  1253. }
  1254. }
  1255. if (t->entry_count == t->entry_capacity)
  1256. root->tree = t = grow_tree_content(t, t->entry_count);
  1257. e = new_tree_entry();
  1258. e->name = to_atom(p, n);
  1259. e->versions[0].mode = 0;
  1260. oidclr(&e->versions[0].oid);
  1261. t->entries[t->entry_count++] = e;
  1262. if (*slash1) {
  1263. e->tree = new_tree_content(8);
  1264. e->versions[1].mode = S_IFDIR;
  1265. tree_content_set(e, slash1 + 1, oid, mode, subtree);
  1266. } else {
  1267. e->tree = subtree;
  1268. e->versions[1].mode = mode;
  1269. oidcpy(&e->versions[1].oid, oid);
  1270. }
  1271. oidclr(&root->versions[1].oid);
  1272. return 1;
  1273. }
  1274. static int tree_content_remove(
  1275. struct tree_entry *root,
  1276. const char *p,
  1277. struct tree_entry *backup_leaf,
  1278. int allow_root)
  1279. {
  1280. struct tree_content *t;
  1281. const char *slash1;
  1282. unsigned int i, n;
  1283. struct tree_entry *e;
  1284. slash1 = strchrnul(p, '/');
  1285. n = slash1 - p;
  1286. if (!root->tree)
  1287. load_tree(root);
  1288. if (!*p && allow_root) {
  1289. e = root;
  1290. goto del_entry;
  1291. }
  1292. t = root->tree;
  1293. for (i = 0; i < t->entry_count; i++) {
  1294. e = t->entries[i];
  1295. if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
  1296. if (*slash1 && !S_ISDIR(e->versions[1].mode))
  1297. /*
  1298. * If p names a file in some subdirectory, and a
  1299. * file or symlink matching the name of the
  1300. * parent directory of p exists, then p cannot
  1301. * exist and need not be deleted.
  1302. */
  1303. return 1;
  1304. if (!*slash1 || !S_ISDIR(e->versions[1].mode))
  1305. goto del_entry;
  1306. if (!e->tree)
  1307. load_tree(e);
  1308. if (tree_content_remove(e, slash1 + 1, backup_leaf, 0)) {
  1309. for (n = 0; n < e->tree->entry_count; n++) {
  1310. if (e->tree->entries[n]->versions[1].mode) {
  1311. oidclr(&root->versions[1].oid);
  1312. return 1;
  1313. }
  1314. }
  1315. backup_leaf = NULL;
  1316. goto del_entry;
  1317. }
  1318. return 0;
  1319. }
  1320. }
  1321. return 0;
  1322. del_entry:
  1323. if (backup_leaf)
  1324. memcpy(backup_leaf, e, sizeof(*backup_leaf));
  1325. else if (e->tree)
  1326. release_tree_content_recursive(e->tree);
  1327. e->tree = NULL;
  1328. e->versions[1].mode = 0;
  1329. oidclr(&e->versions[1].oid);
  1330. oidclr(&root->versions[1].oid);
  1331. return 1;
  1332. }
  1333. static int tree_content_get(
  1334. struct tree_entry *root,
  1335. const char *p,
  1336. struct tree_entry *leaf,
  1337. int allow_root)
  1338. {
  1339. struct tree_content *t;
  1340. const char *slash1;
  1341. unsigned int i, n;
  1342. struct tree_entry *e;
  1343. slash1 = strchrnul(p, '/');
  1344. n = slash1 - p;
  1345. if (!n && !allow_root)
  1346. die("Empty path component found in input");
  1347. if (!root->tree)
  1348. load_tree(root);
  1349. if (!n) {
  1350. e = root;
  1351. goto found_entry;
  1352. }
  1353. t = root->tree;
  1354. for (i = 0; i < t->entry_count; i++) {
  1355. e = t->entries[i];
  1356. if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
  1357. if (!*slash1)
  1358. goto found_entry;
  1359. if (!S_ISDIR(e->versions[1].mode))
  1360. return 0;
  1361. if (!e->tree)
  1362. load_tree(e);
  1363. return tree_content_get(e, slash1 + 1, leaf, 0);
  1364. }
  1365. }
  1366. return 0;
  1367. found_entry:
  1368. memcpy(leaf, e, sizeof(*leaf));
  1369. if (e->tree && is_null_oid(&e->versions[1].oid))
  1370. leaf->tree = dup_tree_content(e->tree);
  1371. else
  1372. leaf->tree = NULL;
  1373. return 1;
  1374. }
  1375. static int update_branch(struct branch *b)
  1376. {
  1377. static const char *msg = "fast-import";
  1378. struct ref_transaction *transaction;
  1379. struct object_id old_oid;
  1380. struct strbuf err = STRBUF_INIT;
  1381. if (is_null_oid(&b->oid)) {
  1382. if (b->delete)
  1383. delete_ref(NULL, b->name, NULL, 0);
  1384. return 0;
  1385. }
  1386. if (read_ref(b->name, &old_oid))
  1387. oidclr(&old_oid);
  1388. if (!force_update && !is_null_oid(&old_oid)) {
  1389. struct commit *old_cmit, *new_cmit;
  1390. old_cmit = lookup_commit_reference_gently(the_repository,
  1391. &old_oid, 0);
  1392. new_cmit = lookup_commit_reference_gently(the_repository,
  1393. &b->oid, 0);
  1394. if (!old_cmit || !new_cmit)
  1395. return error("Branch %s is missing commits.", b->name);
  1396. if (!in_merge_bases(old_cmit, new_cmit)) {
  1397. warning("Not updating %s"
  1398. " (new tip %s does not contain %s)",
  1399. b->name, oid_to_hex(&b->oid),
  1400. oid_to_hex(&old_oid));
  1401. return -1;
  1402. }
  1403. }
  1404. transaction = ref_transaction_begin(&err);
  1405. if (!transaction ||
  1406. ref_transaction_update(transaction, b->name, &b->oid, &old_oid,
  1407. 0, msg, &err) ||
  1408. ref_transaction_commit(transaction, &err)) {
  1409. ref_transaction_free(transaction);
  1410. error("%s", err.buf);
  1411. strbuf_release(&err);
  1412. return -1;
  1413. }
  1414. ref_transaction_free(transaction);
  1415. strbuf_release(&err);
  1416. return 0;
  1417. }
  1418. static void dump_branches(void)
  1419. {
  1420. unsigned int i;
  1421. struct branch *b;
  1422. for (i = 0; i < branch_table_sz; i++) {
  1423. for (b = branch_table[i]; b; b = b->table_next_branch)
  1424. failure |= update_branch(b);
  1425. }
  1426. }
  1427. static void dump_tags(void)
  1428. {
  1429. static const char *msg = "fast-import";
  1430. struct tag *t;
  1431. struct strbuf ref_name = STRBUF_INIT;
  1432. struct strbuf err = STRBUF_INIT;
  1433. struct ref_transaction *transaction;
  1434. transaction = ref_transaction_begin(&err);
  1435. if (!transaction) {
  1436. failure |= error("%s", err.buf);
  1437. goto cleanup;
  1438. }
  1439. for (t = first_tag; t; t = t->next_tag) {
  1440. strbuf_reset(&ref_name);
  1441. strbuf_addf(&ref_name, "refs/tags/%s", t->name);
  1442. if (ref_transaction_update(transaction, ref_name.buf,
  1443. &t->oid, NULL, 0, msg, &err)) {
  1444. failure |= error("%s", err.buf);
  1445. goto cleanup;
  1446. }
  1447. }
  1448. if (ref_transaction_commit(transaction, &err))
  1449. failure |= error("%s", err.buf);
  1450. cleanup:
  1451. ref_transaction_free(transaction);
  1452. strbuf_release(&ref_name);
  1453. strbuf_release(&err);
  1454. }
  1455. static void dump_marks(void)
  1456. {
  1457. struct lock_file mark_lock = LOCK_INIT;
  1458. FILE *f;
  1459. if (!export_marks_file || (import_marks_file && !import_marks_file_done))
  1460. return;
  1461. if (safe_create_leading_directories_const(export_marks_file)) {
  1462. failure |= error_errno("unable to create leading directories of %s",
  1463. export_marks_file);
  1464. return;
  1465. }
  1466. if (hold_lock_file_for_update(&mark_lock, export_marks_file, 0) < 0) {
  1467. failure |= error_errno("Unable to write marks file %s",
  1468. export_marks_file);
  1469. return;
  1470. }
  1471. f = fdopen_lock_file(&mark_lock, "w");
  1472. if (!f) {
  1473. int saved_errno = errno;
  1474. rollback_lock_file(&mark_lock);
  1475. failure |= error("Unable to write marks file %s: %s",
  1476. export_marks_file, strerror(saved_errno));
  1477. return;
  1478. }
  1479. for_each_mark(marks, 0, dump_marks_fn, f);
  1480. if (commit_lock_file(&mark_lock)) {
  1481. failure |= error_errno("Unable to write file %s",
  1482. export_marks_file);
  1483. return;
  1484. }
  1485. }
  1486. static void insert_object_entry(struct mark_set *s, struct object_id *oid, uintmax_t mark)
  1487. {
  1488. struct object_entry *e;
  1489. e = find_object(oid);
  1490. if (!e) {
  1491. enum object_type type = oid_object_info(the_repository,
  1492. oid, NULL);
  1493. if (type < 0)
  1494. die("object not found: %s", oid_to_hex(oid));
  1495. e = insert_object(oid);
  1496. e->type = type;
  1497. e->pack_id = MAX_PACK_ID;
  1498. e->idx.offset = 1; /* just not zero! */
  1499. }
  1500. insert_mark(s, mark, e);
  1501. }
  1502. static void insert_oid_entry(struct mark_set *s, struct object_id *oid, uintmax_t mark)
  1503. {
  1504. insert_mark(s, mark, xmemdupz(oid, sizeof(*oid)));
  1505. }
  1506. static void read_mark_file(struct mark_set *s, FILE *f, mark_set_inserter_t inserter)
  1507. {
  1508. char line[512];
  1509. while (fgets(line, sizeof(line), f)) {
  1510. uintmax_t mark;
  1511. char *end;
  1512. struct object_id oid;
  1513. /* Ensure SHA-1 objects are padded with zeros. */
  1514. memset(oid.hash, 0, sizeof(oid.hash));
  1515. end = strchr(line, '\n');
  1516. if (line[0] != ':' || !end)
  1517. die("corrupt mark line: %s", line);
  1518. *end = 0;
  1519. mark = strtoumax(line + 1, &end, 10);
  1520. if (!mark || end == line + 1
  1521. || *end != ' '
  1522. || get_oid_hex_any(end + 1, &oid) == GIT_HASH_UNKNOWN)
  1523. die("corrupt mark line: %s", line);
  1524. inserter(s, &oid, mark);
  1525. }
  1526. }
  1527. static void read_marks(void)
  1528. {
  1529. FILE *f = fopen(import_marks_file, "r");
  1530. if (f)
  1531. ;
  1532. else if (import_marks_file_ignore_missing && errno == ENOENT)
  1533. goto done; /* Marks file does not exist */
  1534. else
  1535. die_errno("cannot read '%s'", import_marks_file);
  1536. read_mark_file(marks, f, insert_object_entry);
  1537. fclose(f);
  1538. done:
  1539. import_marks_file_done = 1;
  1540. }
  1541. static int read_next_command(void)
  1542. {
  1543. static int stdin_eof = 0;
  1544. if (stdin_eof) {
  1545. unread_command_buf = 0;
  1546. return EOF;
  1547. }
  1548. for (;;) {
  1549. if (unread_command_buf) {
  1550. unread_command_buf = 0;
  1551. } else {
  1552. struct recent_command *rc;
  1553. stdin_eof = strbuf_getline_lf(&command_buf, stdin);
  1554. if (stdin_eof)
  1555. return EOF;
  1556. if (!seen_data_command
  1557. && !starts_with(command_buf.buf, "feature ")
  1558. && !starts_with(command_buf.buf, "option ")) {
  1559. parse_argv();
  1560. }
  1561. rc = rc_free;
  1562. if (rc)
  1563. rc_free = rc->next;
  1564. else {
  1565. rc = cmd_hist.next;
  1566. cmd_hist.next = rc->next;
  1567. cmd_hist.next->prev = &cmd_hist;
  1568. free(rc->buf);
  1569. }
  1570. rc->buf = xstrdup(command_buf.buf);
  1571. rc->prev = cmd_tail;
  1572. rc->next = cmd_hist.prev;
  1573. rc->prev->next = rc;
  1574. cmd_tail = rc;
  1575. }
  1576. if (command_buf.buf[0] == '#')
  1577. continue;
  1578. return 0;
  1579. }
  1580. }
  1581. static void skip_optional_lf(void)
  1582. {
  1583. int term_char = fgetc(stdin);
  1584. if (term_char != '\n' && term_char != EOF)
  1585. ungetc(term_char, stdin);
  1586. }
  1587. static void parse_mark(void)
  1588. {
  1589. const char *v;
  1590. if (skip_prefix(command_buf.buf, "mark :", &v)) {
  1591. next_mark = strtoumax(v, NULL, 10);
  1592. read_next_command();
  1593. }
  1594. else
  1595. next_mark = 0;
  1596. }
  1597. static void parse_original_identifier(void)
  1598. {
  1599. const char *v;
  1600. if (skip_prefix(command_buf.buf, "original-oid ", &v))
  1601. read_next_command();
  1602. }
  1603. static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
  1604. {
  1605. const char *data;
  1606. strbuf_reset(sb);
  1607. if (!skip_prefix(command_buf.buf, "data ", &data))
  1608. die("Expected 'data n' command, found: %s", command_buf.buf);
  1609. if (skip_prefix(data, "<<", &data)) {
  1610. char *term = xstrdup(data);
  1611. size_t term_len = command_buf.len - (data - command_buf.buf);
  1612. for (;;) {
  1613. if (strbuf_getline_lf(&command_buf, stdin) == EOF)
  1614. die("EOF in data (terminator '%s' not found)", term);
  1615. if (term_len == command_buf.len
  1616. && !strcmp(term, command_buf.buf))
  1617. break;
  1618. strbuf_addbuf(sb, &command_buf);
  1619. strbuf_addch(sb, '\n');
  1620. }
  1621. free(term);
  1622. }
  1623. else {
  1624. uintmax_t len = strtoumax(data, NULL, 10);
  1625. size_t n = 0, length = (size_t)len;
  1626. if (limit && limit < len) {
  1627. *len_res = len;
  1628. return 0;
  1629. }
  1630. if (length < len)
  1631. die("data is too large to use in this context");
  1632. while (n < length) {
  1633. size_t s = strbuf_fread(sb, length - n, stdin);
  1634. if (!s && feof(stdin))
  1635. die("EOF in data (%lu bytes remaining)",
  1636. (unsigned long)(length - n));
  1637. n += s;
  1638. }
  1639. }
  1640. skip_optional_lf();
  1641. return 1;
  1642. }
  1643. static int validate_raw_date(const char *src, struct strbuf *result, int strict)
  1644. {
  1645. const char *orig_src = src;
  1646. char *endp;
  1647. unsigned long num;
  1648. errno = 0;
  1649. num = strtoul(src, &endp, 10);
  1650. /*
  1651. * NEEDSWORK: perhaps check for reasonable values? For example, we
  1652. * could error on values representing times more than a
  1653. * day in the future.
  1654. */
  1655. if (errno || endp == src || *endp != ' ')
  1656. return -1;
  1657. src = endp + 1;
  1658. if (*src != '-' && *src != '+')
  1659. return -1;
  1660. num = strtoul(src + 1, &endp, 10);
  1661. /*
  1662. * NEEDSWORK: check for brokenness other than num > 1400, such as
  1663. * (num % 100) >= 60, or ((num % 100) % 15) != 0 ?
  1664. */
  1665. if (errno || endp == src + 1 || *endp || /* did not parse */
  1666. (strict && (1400 < num)) /* parsed a broken timezone */
  1667. )
  1668. return -1;
  1669. strbuf_addstr(result, orig_src);
  1670. return 0;
  1671. }
  1672. static char *parse_ident(const char *buf)
  1673. {
  1674. const char *ltgt;
  1675. size_t name_len;
  1676. struct strbuf ident = STRBUF_INIT;
  1677. /* ensure there is a space delimiter even if there is no name */
  1678. if (*buf == '<')
  1679. --buf;
  1680. ltgt = buf + strcspn(buf, "<>");
  1681. if (*ltgt != '<')
  1682. die("Missing < in ident string: %s", buf);
  1683. if (ltgt != buf && ltgt[-1] != ' ')
  1684. die("Missing space before < in ident string: %s", buf);
  1685. ltgt = ltgt + 1 + strcspn(ltgt + 1, "<>");
  1686. if (*ltgt != '>')
  1687. die("Missing > in ident string: %s", buf);
  1688. ltgt++;
  1689. if (*ltgt != ' ')
  1690. die("Missing space after > in ident string: %s", buf);
  1691. ltgt++;
  1692. name_len = ltgt - buf;
  1693. strbuf_add(&ident, buf, name_len);
  1694. switch (whenspec) {
  1695. case WHENSPEC_RAW:
  1696. if (validate_raw_date(ltgt, &ident, 1) < 0)
  1697. die("Invalid raw date \"%s\" in ident: %s", ltgt, buf);
  1698. break;
  1699. case WHENSPEC_RAW_PERMISSIVE:
  1700. if (validate_raw_date(ltgt, &ident, 0) < 0)
  1701. die("Invalid raw date \"%s\" in ident: %s", ltgt, buf);
  1702. break;
  1703. case WHENSPEC_RFC2822:
  1704. if (parse_date(ltgt, &ident) < 0)
  1705. die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt, buf);
  1706. break;
  1707. case WHENSPEC_NOW:
  1708. if (strcmp("now", ltgt))
  1709. die("Date in ident must be 'now': %s", buf);
  1710. datestamp(&ident);
  1711. break;
  1712. }
  1713. return strbuf_detach(&ident, NULL);
  1714. }
  1715. static void parse_and_store_blob(
  1716. struct last_object *last,
  1717. struct object_id *oidout,
  1718. uintmax_t mark)
  1719. {
  1720. static struct strbuf buf = STRBUF_INIT;
  1721. uintmax_t len;
  1722. if (parse_data(&buf, big_file_threshold, &len))
  1723. store_object(OBJ_BLOB, &buf, last, oidout, mark);
  1724. else {
  1725. if (last) {
  1726. strbuf_release(&last->data);
  1727. last->offset = 0;
  1728. last->depth = 0;
  1729. }
  1730. stream_blob(len, oidout, mark);
  1731. skip_optional_lf();
  1732. }
  1733. }
  1734. static void parse_new_blob(void)
  1735. {
  1736. read_next_command();
  1737. parse_mark();
  1738. parse_original_identifier();
  1739. parse_and_store_blob(&last_blob, NULL, next_mark);
  1740. }
  1741. static void unload_one_branch(void)
  1742. {
  1743. while (cur_active_branches
  1744. && cur_active_branches >= max_active_branches) {
  1745. uintmax_t min_commit = ULONG_MAX;
  1746. struct branch *e, *l = NULL, *p = NULL;
  1747. for (e = active_branches; e; e = e->active_next_branch) {
  1748. if (e->last_commit < min_commit) {
  1749. p = l;
  1750. min_commit = e->last_commit;
  1751. }
  1752. l = e;
  1753. }
  1754. if (p) {
  1755. e = p->active_next_branch;
  1756. p->active_next_branch = e->active_next_branch;
  1757. } else {
  1758. e = active_branches;
  1759. active_branches = e->active_next_branch;
  1760. }
  1761. e->active = 0;
  1762. e->active_next_branch = NULL;
  1763. if (e->branch_tree.tree) {
  1764. release_tree_content_recursive(e->branch_tree.tree);
  1765. e->branch_tree.tree = NULL;
  1766. }
  1767. cur_active_branches--;
  1768. }
  1769. }
  1770. static void load_branch(struct branch *b)
  1771. {
  1772. load_tree(&b->branch_tree);
  1773. if (!b->active) {
  1774. b->active = 1;
  1775. b->active_next_branch = active_branches;
  1776. active_branches = b;
  1777. cur_active_branches++;
  1778. branch_load_count++;
  1779. }
  1780. }
  1781. static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes)
  1782. {
  1783. unsigned char fanout = 0;
  1784. while ((num_notes >>= 8))
  1785. fanout++;
  1786. return fanout;
  1787. }
  1788. static void construct_path_with_fanout(const char *hex_sha1,
  1789. unsigned char fanout, char *path)
  1790. {
  1791. unsigned int i = 0, j = 0;
  1792. if (fanout >= the_hash_algo->rawsz)
  1793. die("Too large fanout (%u)", fanout);
  1794. while (fanout) {
  1795. path[i++] = hex_sha1[j++];
  1796. path[i++] = hex_sha1[j++];
  1797. path[i++] = '/';
  1798. fanout--;
  1799. }
  1800. memcpy(path + i, hex_sha1 + j, the_hash_algo->hexsz - j);
  1801. path[i + the_hash_algo->hexsz - j] = '\0';
  1802. }
  1803. static uintmax_t do_change_note_fanout(
  1804. struct tree_entry *orig_root, struct tree_entry *root,
  1805. char *hex_oid, unsigned int hex_oid_len,
  1806. char *fullpath, unsigned int fullpath_len,
  1807. unsigned char fanout)
  1808. {
  1809. struct tree_content *t;
  1810. struct tree_entry *e, leaf;
  1811. unsigned int i, tmp_hex_oid_len, tmp_fullpath_len;
  1812. uintmax_t num_notes = 0;
  1813. struct object_id oid;
  1814. /* hex oid + '/' between each pair of hex digits + NUL */
  1815. char realpath[GIT_MAX_HEXSZ + ((GIT_MAX_HEXSZ / 2) - 1) + 1];
  1816. const unsigned hexsz = the_hash_algo->hexsz;
  1817. if (!root->tree)
  1818. load_tree(root);
  1819. t = root->tree;
  1820. for (i = 0; t && i < t->entry_count; i++) {
  1821. e = t->entries[i];
  1822. tmp_hex_oid_len = hex_oid_len + e->name->str_len;
  1823. tmp_fullpath_len = fullpath_len;
  1824. /*
  1825. * We're interested in EITHER existing note entries (entries
  1826. * with exactly 40 hex chars in path, not including directory
  1827. * separators), OR directory entries that may contain note
  1828. * entries (with < 40 hex chars in path).
  1829. * Also, each path component in a note entry must be a multiple
  1830. * of 2 chars.
  1831. */
  1832. if (!e->versions[1].mode ||
  1833. tmp_hex_oid_len > hexsz ||
  1834. e->name->str_len % 2)
  1835. continue;
  1836. /* This _may_ be a note entry, or a subdir containing notes */
  1837. memcpy(hex_oid + hex_oid_len, e->name->str_dat,
  1838. e->name->str_len);
  1839. if (tmp_fullpath_len)
  1840. fullpath[tmp_fullpath_len++] = '/';
  1841. memcpy(fullpath + tmp_fullpath_len, e->name->str_dat,
  1842. e->name->str_len);
  1843. tmp_fullpath_len += e->name->str_len;
  1844. fullpath[tmp_fullpath_len] = '\0';
  1845. if (tmp_hex_oid_len == hexsz && !get_oid_hex(hex_oid, &oid)) {
  1846. /* This is a note entry */
  1847. if (fanout == 0xff) {
  1848. /* Counting mode, no rename */
  1849. num_notes++;
  1850. continue;
  1851. }
  1852. construct_path_with_fanout(hex_oid, fanout, realpath);
  1853. if (!strcmp(fullpath, realpath)) {
  1854. /* Note entry is in correct location */
  1855. num_notes++;
  1856. continue;
  1857. }
  1858. /* Rename fullpath to realpath */
  1859. if (!tree_content_remove(orig_root, fullpath, &leaf, 0))
  1860. die("Failed to remove path %s", fullpath);
  1861. tree_content_set(orig_root, realpath,
  1862. &leaf.versions[1].oid,
  1863. leaf.versions[1].mode,
  1864. leaf.tree);
  1865. } else if (S_ISDIR(e->versions[1].mode)) {
  1866. /* This is a subdir that may contain note entries */
  1867. num_notes += do_change_note_fanout(orig_root, e,
  1868. hex_oid, tmp_hex_oid_len,
  1869. fullpath, tmp_fullpath_len, fanout);
  1870. }
  1871. /* The above may have reallocated the current tree_content */
  1872. t = root->tree;
  1873. }
  1874. return num_notes;
  1875. }
  1876. static uintmax_t change_note_fanout(struct tree_entry *root,
  1877. unsigned char fanout)
  1878. {
  1879. /*
  1880. * The size of path is due to one slash between every two hex digits,
  1881. * plus the terminating NUL. Note that there is no slash at the end, so
  1882. * the number of slashes is one less than half the number of hex
  1883. * characters.
  1884. */
  1885. char hex_oid[GIT_MAX_HEXSZ], path[GIT_MAX_HEXSZ + (GIT_MAX_HEXSZ / 2) - 1 + 1];
  1886. return do_change_note_fanout(root, root, hex_oid, 0, path, 0, fanout);
  1887. }
  1888. static int parse_mapped_oid_hex(const char *hex, struct object_id *oid, const char **end)
  1889. {
  1890. int algo;
  1891. khiter_t it;
  1892. /* Make SHA-1 object IDs have all-zero padding. */
  1893. memset(oid->hash, 0, sizeof(oid->hash));
  1894. algo = parse_oid_hex_any(hex, oid, end);
  1895. if (algo == GIT_HASH_UNKNOWN)
  1896. return -1;
  1897. it = kh_get_oid_map(sub_oid_map, *oid);
  1898. /* No such object? */
  1899. if (it == kh_end(sub_oid_map)) {
  1900. /* If we're using the same algorithm, pass it through. */
  1901. if (hash_algos[algo].format_id == the_hash_algo->format_id)
  1902. return 0;
  1903. return -1;
  1904. }
  1905. oidcpy(oid, kh_value(sub_oid_map, it));
  1906. return 0;
  1907. }
  1908. /*
  1909. * Given a pointer into a string, parse a mark reference:
  1910. *
  1911. * idnum ::= ':' bigint;
  1912. *
  1913. * Return the first character after the value in *endptr.
  1914. *
  1915. * Complain if the following character is not what is expected,
  1916. * either a space or end of the string.
  1917. */
  1918. static uintmax_t parse_mark_ref(const char *p, char **endptr)
  1919. {
  1920. uintmax_t mark;
  1921. assert(*p == ':');
  1922. p++;
  1923. mark = strtoumax(p, endptr, 10);
  1924. if (*endptr == p)
  1925. die("No value after ':' in mark: %s", command_buf.buf);
  1926. return mark;
  1927. }
  1928. /*
  1929. * Parse the mark reference, and complain if this is not the end of
  1930. * the string.
  1931. */
  1932. static uintmax_t parse_mark_ref_eol(const char *p)
  1933. {
  1934. char *end;
  1935. uintmax_t mark;
  1936. mark = parse_mark_ref(p, &end);
  1937. if (*end != '\0')
  1938. die("Garbage after mark: %s", command_buf.buf);
  1939. return mark;
  1940. }
  1941. /*
  1942. * Parse the mark reference, demanding a trailing space. Return a
  1943. * pointer to the space.
  1944. */
  1945. static uintmax_t parse_mark_ref_space(const char **p)
  1946. {
  1947. uintmax_t mark;
  1948. char *end;
  1949. mark = parse_mark_ref(*p, &end);
  1950. if (*end++ != ' ')
  1951. die("Missing space after mark: %s", command_buf.buf);
  1952. *p = end;
  1953. return mark;
  1954. }
  1955. static void file_change_m(const char *p, struct branch *b)
  1956. {
  1957. static struct strbuf uq = STRBUF_INIT;
  1958. const char *endp;
  1959. struct object_entry *oe;
  1960. struct object_id oid;
  1961. uint16_t mode, inline_data = 0;
  1962. p = get_mode(p, &mode);
  1963. if (!p)
  1964. die("Corrupt mode: %s", command_buf.buf);
  1965. switch (mode) {
  1966. case 0644:
  1967. case 0755:
  1968. mode |= S_IFREG;
  1969. case S_IFREG | 0644:
  1970. case S_IFREG | 0755:
  1971. case S_IFLNK:
  1972. case S_IFDIR:
  1973. case S_IFGITLINK:
  1974. /* ok */
  1975. break;
  1976. default:
  1977. die("Corrupt mode: %s", command_buf.buf);
  1978. }
  1979. if (*p == ':') {
  1980. oe = find_mark(marks, parse_mark_ref_space(&p));
  1981. oidcpy(&oid, &oe->idx.oid);
  1982. } else if (skip_prefix(p, "inline ", &p)) {
  1983. inline_data = 1;
  1984. oe = NULL; /* not used with inline_data, but makes gcc happy */
  1985. } else {
  1986. if (parse_mapped_oid_hex(p, &oid, &p))
  1987. die("Invalid dataref: %s", command_buf.buf);
  1988. oe = find_object(&oid);
  1989. if (*p++ != ' ')
  1990. die("Missing space after SHA1: %s", command_buf.buf);
  1991. }
  1992. strbuf_reset(&uq);
  1993. if (!unquote_c_style(&uq, p, &endp)) {
  1994. if (*endp)
  1995. die("Garbage after path in: %s", command_buf.buf);
  1996. p = uq.buf;
  1997. }
  1998. /* Git does not track empty, non-toplevel directories. */
  1999. if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *p) {
  2000. tree_content_remove(&b->branch_tree, p, NULL, 0);
  2001. return;
  2002. }
  2003. if (S_ISGITLINK(mode)) {
  2004. if (inline_data)
  2005. die("Git links cannot be specified 'inline': %s",
  2006. command_buf.buf);
  2007. else if (oe) {
  2008. if (oe->type != OBJ_COMMIT)
  2009. die("Not a commit (actually a %s): %s",
  2010. type_name(oe->type), command_buf.buf);
  2011. }
  2012. /*
  2013. * Accept the sha1 without checking; it expected to be in
  2014. * another repository.
  2015. */
  2016. } else if (inline_data) {
  2017. if (S_ISDIR(mode))
  2018. die("Directories cannot be specified 'inline': %s",
  2019. command_buf.buf);
  2020. if (p != uq.buf) {
  2021. strbuf_addstr(&uq, p);
  2022. p = uq.buf;
  2023. }
  2024. while (read_next_command() != EOF) {
  2025. const char *v;
  2026. if (skip_prefix(command_buf.buf, "cat-blob ", &v))
  2027. parse_cat_blob(v);
  2028. else {
  2029. parse_and_store_blob(&last_blob, &oid, 0);
  2030. break;
  2031. }
  2032. }
  2033. } else {
  2034. enum object_type expected = S_ISDIR(mode) ?
  2035. OBJ_TREE: OBJ_BLOB;
  2036. enum object_type type = oe ? oe->type :
  2037. oid_object_info(the_repository, &oid,
  2038. NULL);
  2039. if (type < 0)
  2040. die("%s not found: %s",
  2041. S_ISDIR(mode) ? "Tree" : "Blob",
  2042. command_buf.buf);
  2043. if (type != expected)
  2044. die("Not a %s (actually a %s): %s",
  2045. type_name(expected), type_name(type),
  2046. command_buf.buf);
  2047. }
  2048. if (!*p) {
  2049. tree_content_replace(&b->branch_tree, &oid, mode, NULL);
  2050. return;
  2051. }
  2052. tree_content_set(&b->branch_tree, p, &oid, mode, NULL);
  2053. }
  2054. static void file_change_d(const char *p, struct branch *b)
  2055. {
  2056. static struct strbuf uq = STRBUF_INIT;
  2057. const char *endp;
  2058. strbuf_reset(&uq);
  2059. if (!unquote_c_style(&uq, p, &endp)) {
  2060. if (*endp)
  2061. die("Garbage after path in: %s", command_buf.buf);
  2062. p = uq.buf;
  2063. }
  2064. tree_content_remove(&b->branch_tree, p, NULL, 1);
  2065. }
  2066. static void file_change_cr(const char *s, struct branch *b, int rename)
  2067. {
  2068. const char *d;
  2069. static struct strbuf s_uq = STRBUF_INIT;
  2070. static struct strbuf d_uq = STRBUF_INIT;
  2071. const char *endp;
  2072. struct tree_entry leaf;
  2073. strbuf_reset(&s_uq);
  2074. if (!unquote_c_style(&s_uq, s, &endp)) {
  2075. if (*endp != ' ')
  2076. die("Missing space after source: %s", command_buf.buf);
  2077. } else {
  2078. endp = strchr(s, ' ');
  2079. if (!endp)
  2080. die("Missing space after source: %s", command_buf.buf);
  2081. strbuf_add(&s_uq, s, endp - s);
  2082. }
  2083. s = s_uq.buf;
  2084. endp++;
  2085. if (!*endp)
  2086. die("Missing dest: %s", command_buf.buf);
  2087. d = endp;
  2088. strbuf_reset(&d_uq);
  2089. if (!unquote_c_style(&d_uq, d, &endp)) {
  2090. if (*endp)
  2091. die("Garbage after dest in: %s", command_buf.buf);
  2092. d = d_uq.buf;
  2093. }
  2094. memset(&leaf, 0, sizeof(leaf));
  2095. if (rename)
  2096. tree_content_remove(&b->branch_tree, s, &leaf, 1);
  2097. else
  2098. tree_content_get(&b->branch_tree, s, &leaf, 1);
  2099. if (!leaf.versions[1].mode)
  2100. die("Path %s not in branch", s);
  2101. if (!*d) { /* C "path/to/subdir" "" */
  2102. tree_content_replace(&b->branch_tree,
  2103. &leaf.versions[1].oid,
  2104. leaf.versions[1].mode,
  2105. leaf.tree);
  2106. return;
  2107. }
  2108. tree_content_set(&b->branch_tree, d,
  2109. &leaf.versions[1].oid,
  2110. leaf.versions[1].mode,
  2111. leaf.tree);
  2112. }
  2113. static void note_change_n(const char *p, struct branch *b, unsigned char *old_fanout)
  2114. {
  2115. static struct strbuf uq = STRBUF_INIT;
  2116. struct object_entry *oe;
  2117. struct branch *s;
  2118. struct object_id oid, commit_oid;
  2119. char path[GIT_MAX_RAWSZ * 3];
  2120. uint16_t inline_data = 0;
  2121. unsigned char new_fanout;
  2122. /*
  2123. * When loading a branch, we don't traverse its tree to count the real
  2124. * number of notes (too expensive to do this for all non-note refs).
  2125. * This means that recently loaded notes refs might incorrectly have
  2126. * b->num_notes == 0, and consequently, old_fanout might be wrong.
  2127. *
  2128. * Fix this by traversing the tree and counting the number of notes
  2129. * when b->num_notes == 0. If the notes tree is truly empty, the
  2130. * calculation should not take long.
  2131. */
  2132. if (b->num_notes == 0 && *old_fanout == 0) {
  2133. /* Invoke change_note_fanout() in "counting mode". */
  2134. b->num_notes = change_note_fanout(&b->branch_tree, 0xff);
  2135. *old_fanout = convert_num_notes_to_fanout(b->num_notes);
  2136. }
  2137. /* Now parse the notemodify command. */
  2138. /* <dataref> or 'inline' */
  2139. if (*p == ':') {
  2140. oe = find_mark(marks, parse_mark_ref_space(&p));
  2141. oidcpy(&oid, &oe->idx.oid);
  2142. } else if (skip_prefix(p, "inline ", &p)) {
  2143. inline_data = 1;
  2144. oe = NULL; /* not used with inline_data, but makes gcc happy */
  2145. } else {
  2146. if (parse_mapped_oid_hex(p, &oid, &p))
  2147. die("Invalid dataref: %s", command_buf.buf);
  2148. oe = find_object(&oid);
  2149. if (*p++ != ' ')
  2150. die("Missing space after SHA1: %s", command_buf.buf);
  2151. }
  2152. /* <commit-ish> */
  2153. s = lookup_branch(p);
  2154. if (s) {
  2155. if (is_null_oid(&s->oid))
  2156. die("Can't add a note on empty branch.");
  2157. oidcpy(&commit_oid, &s->oid);
  2158. } else if (*p == ':') {
  2159. uintmax_t commit_mark = parse_mark_ref_eol(p);
  2160. struct object_entry *commit_oe = find_mark(marks, commit_mark);
  2161. if (commit_oe->type != OBJ_COMMIT)
  2162. die("Mark :%" PRIuMAX " not a commit", commit_mark);
  2163. oidcpy(&commit_oid, &commit_oe->idx.oid);
  2164. } else if (!get_oid(p, &commit_oid)) {
  2165. unsigned long size;
  2166. char *buf = read_object_with_reference(the_repository,
  2167. &commit_oid,
  2168. commit_type, &size,
  2169. &commit_oid);
  2170. if (!buf || size < the_hash_algo->hexsz + 6)
  2171. die("Not a valid commit: %s", p);
  2172. free(buf);
  2173. } else
  2174. die("Invalid ref name or SHA1 expression: %s", p);
  2175. if (inline_data) {
  2176. if (p != uq.buf) {
  2177. strbuf_addstr(&uq, p);
  2178. p = uq.buf;
  2179. }
  2180. read_next_command();
  2181. parse_and_store_blob(&last_blob, &oid, 0);
  2182. } else if (oe) {
  2183. if (oe->type != OBJ_BLOB)
  2184. die("Not a blob (actually a %s): %s",
  2185. type_name(oe->type), command_buf.buf);
  2186. } else if (!is_null_oid(&oid)) {
  2187. enum object_type type = oid_object_info(the_repository, &oid,
  2188. NULL);
  2189. if (type < 0)
  2190. die("Blob not found: %s", command_buf.buf);
  2191. if (type != OBJ_BLOB)
  2192. die("Not a blob (actually a %s): %s",
  2193. type_name(type), command_buf.buf);
  2194. }
  2195. construct_path_with_fanout(oid_to_hex(&commit_oid), *old_fanout, path);
  2196. if (tree_content_remove(&b->branch_tree, path, NULL, 0))
  2197. b->num_notes--;
  2198. if (is_null_oid(&oid))
  2199. return; /* nothing to insert */
  2200. b->num_notes++;
  2201. new_fanout = convert_num_notes_to_fanout(b->num_notes);
  2202. construct_path_with_fanout(oid_to_hex(&commit_oid), new_fanout, path);
  2203. tree_content_set(&b->branch_tree, path, &oid, S_IFREG | 0644, NULL);
  2204. }
  2205. static void file_change_deleteall(struct branch *b)
  2206. {
  2207. release_tree_content_recursive(b->branch_tree.tree);
  2208. oidclr(&b->branch_tree.versions[0].oid);
  2209. oidclr(&b->branch_tree.versions[1].oid);
  2210. load_tree(&b->branch_tree);
  2211. b->num_notes = 0;
  2212. }
  2213. static void parse_from_commit(struct branch *b, char *buf, unsigned long size)
  2214. {
  2215. if (!buf || size < the_hash_algo->hexsz + 6)
  2216. die("Not a valid commit: %s", oid_to_hex(&b->oid));
  2217. if (memcmp("tree ", buf, 5)
  2218. || get_oid_hex(buf + 5, &b->branch_tree.versions[1].oid))
  2219. die("The commit %s is corrupt", oid_to_hex(&b->oid));
  2220. oidcpy(&b->branch_tree.versions[0].oid,
  2221. &b->branch_tree.versions[1].oid);
  2222. }
  2223. static void parse_from_existing(struct branch *b)
  2224. {
  2225. if (is_null_oid(&b->oid)) {
  2226. oidclr(&b->branch_tree.versions[0].oid);
  2227. oidclr(&b->branch_tree.versions[1].oid);
  2228. } else {
  2229. unsigned long size;
  2230. char *buf;
  2231. buf = read_object_with_reference(the_repository,
  2232. &b->oid, commit_type, &size,
  2233. &b->oid);
  2234. parse_from_commit(b, buf, size);
  2235. free(buf);
  2236. }
  2237. }
  2238. static int parse_objectish(struct branch *b, const char *objectish)
  2239. {
  2240. struct branch *s;
  2241. struct object_id oid;
  2242. oidcpy(&oid, &b->branch_tree.versions[1].oid);
  2243. s = lookup_branch(objectish);
  2244. if (b == s)
  2245. die("Can't create a branch from itself: %s", b->name);
  2246. else if (s) {
  2247. struct object_id *t = &s->branch_tree.versions[1].oid;
  2248. oidcpy(&b->oid, &s->oid);
  2249. oidcpy(&b->branch_tree.versions[0].oid, t);
  2250. oidcpy(&b->branch_tree.versions[1].oid, t);
  2251. } else if (*objectish == ':') {
  2252. uintmax_t idnum = parse_mark_ref_eol(objectish);
  2253. struct object_entry *oe = find_mark(marks, idnum);
  2254. if (oe->type != OBJ_COMMIT)
  2255. die("Mark :%" PRIuMAX " not a commit", idnum);
  2256. if (!oideq(&b->oid, &oe->idx.oid)) {
  2257. oidcpy(&b->oid, &oe->idx.oid);
  2258. if (oe->pack_id != MAX_PACK_ID) {
  2259. unsigned long size;
  2260. char *buf = gfi_unpack_entry(oe, &size);
  2261. parse_from_commit(b, buf, size);
  2262. free(buf);
  2263. } else
  2264. parse_from_existing(b);
  2265. }
  2266. } else if (!get_oid(objectish, &b->oid)) {
  2267. parse_from_existing(b);
  2268. if (is_null_oid(&b->oid))
  2269. b->delete = 1;
  2270. }
  2271. else
  2272. die("Invalid ref name or SHA1 expression: %s", objectish);
  2273. if (b->branch_tree.tree && !oideq(&oid, &b->branch_tree.versions[1].oid)) {
  2274. release_tree_content_recursive(b->branch_tree.tree);
  2275. b->branch_tree.tree = NULL;
  2276. }
  2277. read_next_command();
  2278. return 1;
  2279. }
  2280. static int parse_from(struct branch *b)
  2281. {
  2282. const char *from;
  2283. if (!skip_prefix(command_buf.buf, "from ", &from))
  2284. return 0;
  2285. return parse_objectish(b, from);
  2286. }
  2287. static int parse_objectish_with_prefix(struct branch *b, const char *prefix)
  2288. {
  2289. const char *base;
  2290. if (!skip_prefix(command_buf.buf, prefix, &base))
  2291. return 0;
  2292. return parse_objectish(b, base);
  2293. }
  2294. static struct hash_list *parse_merge(unsigned int *count)
  2295. {
  2296. struct hash_list *list = NULL, **tail = &list, *n;
  2297. const char *from;
  2298. struct branch *s;
  2299. *count = 0;
  2300. while (skip_prefix(command_buf.buf, "merge ", &from)) {
  2301. n = xmalloc(sizeof(*n));
  2302. s = lookup_branch(from);
  2303. if (s)
  2304. oidcpy(&n->oid, &s->oid);
  2305. else if (*from == ':') {
  2306. uintmax_t idnum = parse_mark_ref_eol(from);
  2307. struct object_entry *oe = find_mark(marks, idnum);
  2308. if (oe->type != OBJ_COMMIT)
  2309. die("Mark :%" PRIuMAX " not a commit", idnum);
  2310. oidcpy(&n->oid, &oe->idx.oid);
  2311. } else if (!get_oid(from, &n->oid)) {
  2312. unsigned long size;
  2313. char *buf = read_object_with_reference(the_repository,
  2314. &n->oid,
  2315. commit_type,
  2316. &size, &n->oid);
  2317. if (!buf || size < the_hash_algo->hexsz + 6)
  2318. die("Not a valid commit: %s", from);
  2319. free(buf);
  2320. } else
  2321. die("Invalid ref name or SHA1 expression: %s", from);
  2322. n->next = NULL;
  2323. *tail = n;
  2324. tail = &n->next;
  2325. (*count)++;
  2326. read_next_command();
  2327. }
  2328. return list;
  2329. }
  2330. static void parse_new_commit(const char *arg)
  2331. {
  2332. static struct strbuf msg = STRBUF_INIT;
  2333. struct branch *b;
  2334. char *author = NULL;
  2335. char *committer = NULL;
  2336. char *encoding = NULL;
  2337. struct hash_list *merge_list = NULL;
  2338. unsigned int merge_count;
  2339. unsigned char prev_fanout, new_fanout;
  2340. const char *v;
  2341. b = lookup_branch(arg);
  2342. if (!b)
  2343. b = new_branch(arg);
  2344. read_next_command();
  2345. parse_mark();
  2346. parse_original_identifier();
  2347. if (skip_prefix(command_buf.buf, "author ", &v)) {
  2348. author = parse_ident(v);
  2349. read_next_command();
  2350. }
  2351. if (skip_prefix(command_buf.buf, "committer ", &v)) {
  2352. committer = parse_ident(v);
  2353. read_next_command();
  2354. }
  2355. if (!committer)
  2356. die("Expected committer but didn't get one");
  2357. if (skip_prefix(command_buf.buf, "encoding ", &v)) {
  2358. encoding = xstrdup(v);
  2359. read_next_command();
  2360. }
  2361. parse_data(&msg, 0, NULL);
  2362. read_next_command();
  2363. parse_from(b);
  2364. merge_list = parse_merge(&merge_count);
  2365. /* ensure the branch is active/loaded */
  2366. if (!b->branch_tree.tree || !max_active_branches) {
  2367. unload_one_branch();
  2368. load_branch(b);
  2369. }
  2370. prev_fanout = convert_num_notes_to_fanout(b->num_notes);
  2371. /* file_change* */
  2372. while (command_buf.len > 0) {
  2373. if (skip_prefix(command_buf.buf, "M ", &v))
  2374. file_change_m(v, b);
  2375. else if (skip_prefix(command_buf.buf, "D ", &v))
  2376. file_change_d(v, b);
  2377. else if (skip_prefix(command_buf.buf, "R ", &v))
  2378. file_change_cr(v, b, 1);
  2379. else if (skip_prefix(command_buf.buf, "C ", &v))
  2380. file_change_cr(v, b, 0);
  2381. else if (skip_prefix(command_buf.buf, "N ", &v))
  2382. note_change_n(v, b, &prev_fanout);
  2383. else if (!strcmp("deleteall", command_buf.buf))
  2384. file_change_deleteall(b);
  2385. else if (skip_prefix(command_buf.buf, "ls ", &v))
  2386. parse_ls(v, b);
  2387. else if (skip_prefix(command_buf.buf, "cat-blob ", &v))
  2388. parse_cat_blob(v);
  2389. else {
  2390. unread_command_buf = 1;
  2391. break;
  2392. }
  2393. if (read_next_command() == EOF)
  2394. break;
  2395. }
  2396. new_fanout = convert_num_notes_to_fanout(b->num_notes);
  2397. if (new_fanout != prev_fanout)
  2398. b->num_notes = change_note_fanout(&b->branch_tree, new_fanout);
  2399. /* build the tree and the commit */
  2400. store_tree(&b->branch_tree);
  2401. oidcpy(&b->branch_tree.versions[0].oid,
  2402. &b->branch_tree.versions[1].oid);
  2403. strbuf_reset(&new_data);
  2404. strbuf_addf(&new_data, "tree %s\n",
  2405. oid_to_hex(&b->branch_tree.versions[1].oid));
  2406. if (!is_null_oid(&b->oid))
  2407. strbuf_addf(&new_data, "parent %s\n",
  2408. oid_to_hex(&b->oid));
  2409. while (merge_list) {
  2410. struct hash_list *next = merge_list->next;
  2411. strbuf_addf(&new_data, "parent %s\n",
  2412. oid_to_hex(&merge_list->oid));
  2413. free(merge_list);
  2414. merge_list = next;
  2415. }
  2416. strbuf_addf(&new_data,
  2417. "author %s\n"
  2418. "committer %s\n",
  2419. author ? author : committer, committer);
  2420. if (encoding)
  2421. strbuf_addf(&new_data,
  2422. "encoding %s\n",
  2423. encoding);
  2424. strbuf_addch(&new_data, '\n');
  2425. strbuf_addbuf(&new_data, &msg);
  2426. free(author);
  2427. free(committer);
  2428. free(encoding);
  2429. if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark))
  2430. b->pack_id = pack_id;
  2431. b->last_commit = object_count_by_type[OBJ_COMMIT];
  2432. }
  2433. static void parse_new_tag(const char *arg)
  2434. {
  2435. static struct strbuf msg = STRBUF_INIT;
  2436. const char *from;
  2437. char *tagger;
  2438. struct branch *s;
  2439. struct tag *t;
  2440. uintmax_t from_mark = 0;
  2441. struct object_id oid;
  2442. enum object_type type;
  2443. const char *v;
  2444. t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag));
  2445. memset(t, 0, sizeof(struct tag));
  2446. t->name = mem_pool_strdup(&fi_mem_pool, arg);
  2447. if (last_tag)
  2448. last_tag->next_tag = t;
  2449. else
  2450. first_tag = t;
  2451. last_tag = t;
  2452. read_next_command();
  2453. parse_mark();
  2454. /* from ... */
  2455. if (!skip_prefix(command_buf.buf, "from ", &from))
  2456. die("Expected from command, got %s", command_buf.buf);
  2457. s = lookup_branch(from);
  2458. if (s) {
  2459. if (is_null_oid(&s->oid))
  2460. die("Can't tag an empty branch.");
  2461. oidcpy(&oid, &s->oid);
  2462. type = OBJ_COMMIT;
  2463. } else if (*from == ':') {
  2464. struct object_entry *oe;
  2465. from_mark = parse_mark_ref_eol(from);
  2466. oe = find_mark(marks, from_mark);
  2467. type = oe->type;
  2468. oidcpy(&oid, &oe->idx.oid);
  2469. } else if (!get_oid(from, &oid)) {
  2470. struct object_entry *oe = find_object(&oid);
  2471. if (!oe) {
  2472. type = oid_object_info(the_repository, &oid, NULL);
  2473. if (type < 0)
  2474. die("Not a valid object: %s", from);
  2475. } else
  2476. type = oe->type;
  2477. } else
  2478. die("Invalid ref name or SHA1 expression: %s", from);
  2479. read_next_command();
  2480. /* original-oid ... */
  2481. parse_original_identifier();
  2482. /* tagger ... */
  2483. if (skip_prefix(command_buf.buf, "tagger ", &v)) {
  2484. tagger = parse_ident(v);
  2485. read_next_command();
  2486. } else
  2487. tagger = NULL;
  2488. /* tag payload/message */
  2489. parse_data(&msg, 0, NULL);
  2490. /* build the tag object */
  2491. strbuf_reset(&new_data);
  2492. strbuf_addf(&new_data,
  2493. "object %s\n"
  2494. "type %s\n"
  2495. "tag %s\n",
  2496. oid_to_hex(&oid), type_name(type), t->name);
  2497. if (tagger)
  2498. strbuf_addf(&new_data,
  2499. "tagger %s\n", tagger);
  2500. strbuf_addch(&new_data, '\n');
  2501. strbuf_addbuf(&new_data, &msg);
  2502. free(tagger);
  2503. if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, next_mark))
  2504. t->pack_id = MAX_PACK_ID;
  2505. else
  2506. t->pack_id = pack_id;
  2507. }
  2508. static void parse_reset_branch(const char *arg)
  2509. {
  2510. struct branch *b;
  2511. const char *tag_name;
  2512. b = lookup_branch(arg);
  2513. if (b) {
  2514. oidclr(&b->oid);
  2515. oidclr(&b->branch_tree.versions[0].oid);
  2516. oidclr(&b->branch_tree.versions[1].oid);
  2517. if (b->branch_tree.tree) {
  2518. release_tree_content_recursive(b->branch_tree.tree);
  2519. b->branch_tree.tree = NULL;
  2520. }
  2521. }
  2522. else
  2523. b = new_branch(arg);
  2524. read_next_command();
  2525. parse_from(b);
  2526. if (b->delete && skip_prefix(b->name, "refs/tags/", &tag_name)) {
  2527. /*
  2528. * Elsewhere, we call dump_branches() before dump_tags(),
  2529. * and dump_branches() will handle ref deletions first, so
  2530. * in order to make sure the deletion actually takes effect,
  2531. * we need to remove the tag from our list of tags to update.
  2532. *
  2533. * NEEDSWORK: replace list of tags with hashmap for faster
  2534. * deletion?
  2535. */
  2536. struct tag *t, *prev = NULL;
  2537. for (t = first_tag; t; t = t->next_tag) {
  2538. if (!strcmp(t->name, tag_name))
  2539. break;
  2540. prev = t;
  2541. }
  2542. if (t) {
  2543. if (prev)
  2544. prev->next_tag = t->next_tag;
  2545. else
  2546. first_tag = t->next_tag;
  2547. if (!t->next_tag)
  2548. last_tag = prev;
  2549. /* There is no mem_pool_free(t) function to call. */
  2550. }
  2551. }
  2552. if (command_buf.len > 0)
  2553. unread_command_buf = 1;
  2554. }
  2555. static void cat_blob_write(const char *buf, unsigned long size)
  2556. {
  2557. if (write_in_full(cat_blob_fd, buf, size) < 0)
  2558. die_errno("Write to frontend failed");
  2559. }
  2560. static void cat_blob(struct object_entry *oe, struct object_id *oid)
  2561. {
  2562. struct strbuf line = STRBUF_INIT;
  2563. unsigned long size;
  2564. enum object_type type = 0;
  2565. char *buf;
  2566. if (!oe || oe->pack_id == MAX_PACK_ID) {
  2567. buf = read_object_file(oid, &type, &size);
  2568. } else {
  2569. type = oe->type;
  2570. buf = gfi_unpack_entry(oe, &size);
  2571. }
  2572. /*
  2573. * Output based on batch_one_object() from cat-file.c.
  2574. */
  2575. if (type <= 0) {
  2576. strbuf_reset(&line);
  2577. strbuf_addf(&line, "%s missing\n", oid_to_hex(oid));
  2578. cat_blob_write(line.buf, line.len);
  2579. strbuf_release(&line);
  2580. free(buf);
  2581. return;
  2582. }
  2583. if (!buf)
  2584. die("Can't read object %s", oid_to_hex(oid));
  2585. if (type != OBJ_BLOB)
  2586. die("Object %s is a %s but a blob was expected.",
  2587. oid_to_hex(oid), type_name(type));
  2588. strbuf_reset(&line);
  2589. strbuf_addf(&line, "%s %s %"PRIuMAX"\n", oid_to_hex(oid),
  2590. type_name(type), (uintmax_t)size);
  2591. cat_blob_write(line.buf, line.len);
  2592. strbuf_release(&line);
  2593. cat_blob_write(buf, size);
  2594. cat_blob_write("\n", 1);
  2595. if (oe && oe->pack_id == pack_id) {
  2596. last_blob.offset = oe->idx.offset;
  2597. strbuf_attach(&last_blob.data, buf, size, size);
  2598. last_blob.depth = oe->depth;
  2599. } else
  2600. free(buf);
  2601. }
  2602. static void parse_get_mark(const char *p)
  2603. {
  2604. struct object_entry *oe;
  2605. char output[GIT_MAX_HEXSZ + 2];
  2606. /* get-mark SP <object> LF */
  2607. if (*p != ':')
  2608. die("Not a mark: %s", p);
  2609. oe = find_mark(marks, parse_mark_ref_eol(p));
  2610. if (!oe)
  2611. die("Unknown mark: %s", command_buf.buf);
  2612. xsnprintf(output, sizeof(output), "%s\n", oid_to_hex(&oe->idx.oid));
  2613. cat_blob_write(output, the_hash_algo->hexsz + 1);
  2614. }
  2615. static void parse_cat_blob(const char *p)
  2616. {
  2617. struct object_entry *oe;
  2618. struct object_id oid;
  2619. /* cat-blob SP <object> LF */
  2620. if (*p == ':') {
  2621. oe = find_mark(marks, parse_mark_ref_eol(p));
  2622. if (!oe)
  2623. die("Unknown mark: %s", command_buf.buf);
  2624. oidcpy(&oid, &oe->idx.oid);
  2625. } else {
  2626. if (parse_mapped_oid_hex(p, &oid, &p))
  2627. die("Invalid dataref: %s", command_buf.buf);
  2628. if (*p)
  2629. die("Garbage after SHA1: %s", command_buf.buf);
  2630. oe = find_object(&oid);
  2631. }
  2632. cat_blob(oe, &oid);
  2633. }
  2634. static struct object_entry *dereference(struct object_entry *oe,
  2635. struct object_id *oid)
  2636. {
  2637. unsigned long size;
  2638. char *buf = NULL;
  2639. const unsigned hexsz = the_hash_algo->hexsz;
  2640. if (!oe) {
  2641. enum object_type type = oid_object_info(the_repository, oid,
  2642. NULL);
  2643. if (type < 0)
  2644. die("object not found: %s", oid_to_hex(oid));
  2645. /* cache it! */
  2646. oe = insert_object(oid);
  2647. oe->type = type;
  2648. oe->pack_id = MAX_PACK_ID;
  2649. oe->idx.offset = 1;
  2650. }
  2651. switch (oe->type) {
  2652. case OBJ_TREE: /* easy case. */
  2653. return oe;
  2654. case OBJ_COMMIT:
  2655. case OBJ_TAG:
  2656. break;
  2657. default:
  2658. die("Not a tree-ish: %s", command_buf.buf);
  2659. }
  2660. if (oe->pack_id != MAX_PACK_ID) { /* in a pack being written */
  2661. buf = gfi_unpack_entry(oe, &size);
  2662. } else {
  2663. enum object_type unused;
  2664. buf = read_object_file(oid, &unused, &size);
  2665. }
  2666. if (!buf)
  2667. die("Can't load object %s", oid_to_hex(oid));
  2668. /* Peel one layer. */
  2669. switch (oe->type) {
  2670. case OBJ_TAG:
  2671. if (size < hexsz + strlen("object ") ||
  2672. get_oid_hex(buf + strlen("object "), oid))
  2673. die("Invalid SHA1 in tag: %s", command_buf.buf);
  2674. break;
  2675. case OBJ_COMMIT:
  2676. if (size < hexsz + strlen("tree ") ||
  2677. get_oid_hex(buf + strlen("tree "), oid))
  2678. die("Invalid SHA1 in commit: %s", command_buf.buf);
  2679. }
  2680. free(buf);
  2681. return find_object(oid);
  2682. }
  2683. static void insert_mapped_mark(uintmax_t mark, void *object, void *cbp)
  2684. {
  2685. struct object_id *fromoid = object;
  2686. struct object_id *tooid = find_mark(cbp, mark);
  2687. int ret;
  2688. khiter_t it;
  2689. it = kh_put_oid_map(sub_oid_map, *fromoid, &ret);
  2690. /* We've already seen this object. */
  2691. if (ret == 0)
  2692. return;
  2693. kh_value(sub_oid_map, it) = tooid;
  2694. }
  2695. static void build_mark_map_one(struct mark_set *from, struct mark_set *to)
  2696. {
  2697. for_each_mark(from, 0, insert_mapped_mark, to);
  2698. }
  2699. static void build_mark_map(struct string_list *from, struct string_list *to)
  2700. {
  2701. struct string_list_item *fromp, *top;
  2702. sub_oid_map = kh_init_oid_map();
  2703. for_each_string_list_item(fromp, from) {
  2704. top = string_list_lookup(to, fromp->string);
  2705. if (!fromp->util) {
  2706. die(_("Missing from marks for submodule '%s'"), fromp->string);
  2707. } else if (!top || !top->util) {
  2708. die(_("Missing to marks for submodule '%s'"), fromp->string);
  2709. }
  2710. build_mark_map_one(fromp->util, top->util);
  2711. }
  2712. }
  2713. static struct object_entry *parse_treeish_dataref(const char **p)
  2714. {
  2715. struct object_id oid;
  2716. struct object_entry *e;
  2717. if (**p == ':') { /* <mark> */
  2718. e = find_mark(marks, parse_mark_ref_space(p));
  2719. if (!e)
  2720. die("Unknown mark: %s", command_buf.buf);
  2721. oidcpy(&oid, &e->idx.oid);
  2722. } else { /* <sha1> */
  2723. if (parse_mapped_oid_hex(*p, &oid, p))
  2724. die("Invalid dataref: %s", command_buf.buf);
  2725. e = find_object(&oid);
  2726. if (*(*p)++ != ' ')
  2727. die("Missing space after tree-ish: %s", command_buf.buf);
  2728. }
  2729. while (!e || e->type != OBJ_TREE)
  2730. e = dereference(e, &oid);
  2731. return e;
  2732. }
  2733. static void print_ls(int mode, const unsigned char *hash, const char *path)
  2734. {
  2735. static struct strbuf line = STRBUF_INIT;
  2736. /* See show_tree(). */
  2737. const char *type =
  2738. S_ISGITLINK(mode) ? commit_type :
  2739. S_ISDIR(mode) ? tree_type :
  2740. blob_type;
  2741. if (!mode) {
  2742. /* missing SP path LF */
  2743. strbuf_reset(&line);
  2744. strbuf_addstr(&line, "missing ");
  2745. quote_c_style(path, &line, NULL, 0);
  2746. strbuf_addch(&line, '\n');
  2747. } else {
  2748. /* mode SP type SP object_name TAB path LF */
  2749. strbuf_reset(&line);
  2750. strbuf_addf(&line, "%06o %s %s\t",
  2751. mode & ~NO_DELTA, type, hash_to_hex(hash));
  2752. quote_c_style(path, &line, NULL, 0);
  2753. strbuf_addch(&line, '\n');
  2754. }
  2755. cat_blob_write(line.buf, line.len);
  2756. }
  2757. static void parse_ls(const char *p, struct branch *b)
  2758. {
  2759. struct tree_entry *root = NULL;
  2760. struct tree_entry leaf = {NULL};
  2761. /* ls SP (<tree-ish> SP)? <path> */
  2762. if (*p == '"') {
  2763. if (!b)
  2764. die("Not in a commit: %s", command_buf.buf);
  2765. root = &b->branch_tree;
  2766. } else {
  2767. struct object_entry *e = parse_treeish_dataref(&p);
  2768. root = new_tree_entry();
  2769. oidcpy(&root->versions[1].oid, &e->idx.oid);
  2770. if (!is_null_oid(&root->versions[1].oid))
  2771. root->versions[1].mode = S_IFDIR;
  2772. load_tree(root);
  2773. }
  2774. if (*p == '"') {
  2775. static struct strbuf uq = STRBUF_INIT;
  2776. const char *endp;
  2777. strbuf_reset(&uq);
  2778. if (unquote_c_style(&uq, p, &endp))
  2779. die("Invalid path: %s", command_buf.buf);
  2780. if (*endp)
  2781. die("Garbage after path in: %s", command_buf.buf);
  2782. p = uq.buf;
  2783. }
  2784. tree_content_get(root, p, &leaf, 1);
  2785. /*
  2786. * A directory in preparation would have a sha1 of zero
  2787. * until it is saved. Save, for simplicity.
  2788. */
  2789. if (S_ISDIR(leaf.versions[1].mode))
  2790. store_tree(&leaf);
  2791. print_ls(leaf.versions[1].mode, leaf.versions[1].oid.hash, p);
  2792. if (leaf.tree)
  2793. release_tree_content_recursive(leaf.tree);
  2794. if (!b || root != &b->branch_tree)
  2795. release_tree_entry(root);
  2796. }
  2797. static void checkpoint(void)
  2798. {
  2799. checkpoint_requested = 0;
  2800. if (object_count) {
  2801. cycle_packfile();
  2802. }
  2803. dump_branches();
  2804. dump_tags();
  2805. dump_marks();
  2806. }
  2807. static void parse_checkpoint(void)
  2808. {
  2809. checkpoint_requested = 1;
  2810. skip_optional_lf();
  2811. }
  2812. static void parse_progress(void)
  2813. {
  2814. fwrite(command_buf.buf, 1, command_buf.len, stdout);
  2815. fputc('\n', stdout);
  2816. fflush(stdout);
  2817. skip_optional_lf();
  2818. }
  2819. static void parse_alias(void)
  2820. {
  2821. struct object_entry *e;
  2822. struct branch b;
  2823. skip_optional_lf();
  2824. read_next_command();
  2825. /* mark ... */
  2826. parse_mark();
  2827. if (!next_mark)
  2828. die(_("Expected 'mark' command, got %s"), command_buf.buf);
  2829. /* to ... */
  2830. memset(&b, 0, sizeof(b));
  2831. if (!parse_objectish_with_prefix(&b, "to "))
  2832. die(_("Expected 'to' command, got %s"), command_buf.buf);
  2833. e = find_object(&b.oid);
  2834. assert(e);
  2835. insert_mark(marks, next_mark, e);
  2836. }
  2837. static char* make_fast_import_path(const char *path)
  2838. {
  2839. if (!relative_marks_paths || is_absolute_path(path))
  2840. return xstrdup(path);
  2841. return git_pathdup("info/fast-import/%s", path);
  2842. }
  2843. static void option_import_marks(const char *marks,
  2844. int from_stream, int ignore_missing)
  2845. {
  2846. if (import_marks_file) {
  2847. if (from_stream)
  2848. die("Only one import-marks command allowed per stream");
  2849. /* read previous mark file */
  2850. if(!import_marks_file_from_stream)
  2851. read_marks();
  2852. }
  2853. import_marks_file = make_fast_import_path(marks);
  2854. import_marks_file_from_stream = from_stream;
  2855. import_marks_file_ignore_missing = ignore_missing;
  2856. }
  2857. static void option_date_format(const char *fmt)
  2858. {
  2859. if (!strcmp(fmt, "raw"))
  2860. whenspec = WHENSPEC_RAW;
  2861. else if (!strcmp(fmt, "raw-permissive"))
  2862. whenspec = WHENSPEC_RAW_PERMISSIVE;
  2863. else if (!strcmp(fmt, "rfc2822"))
  2864. whenspec = WHENSPEC_RFC2822;
  2865. else if (!strcmp(fmt, "now"))
  2866. whenspec = WHENSPEC_NOW;
  2867. else
  2868. die("unknown --date-format argument %s", fmt);
  2869. }
  2870. static unsigned long ulong_arg(const char *option, const char *arg)
  2871. {
  2872. char *endptr;
  2873. unsigned long rv = strtoul(arg, &endptr, 0);
  2874. if (strchr(arg, '-') || endptr == arg || *endptr)
  2875. die("%s: argument must be a non-negative integer", option);
  2876. return rv;
  2877. }
  2878. static void option_depth(const char *depth)
  2879. {
  2880. max_depth = ulong_arg("--depth", depth);
  2881. if (max_depth > MAX_DEPTH)
  2882. die("--depth cannot exceed %u", MAX_DEPTH);
  2883. }
  2884. static void option_active_branches(const char *branches)
  2885. {
  2886. max_active_branches = ulong_arg("--active-branches", branches);
  2887. }
  2888. static void option_export_marks(const char *marks)
  2889. {
  2890. export_marks_file = make_fast_import_path(marks);
  2891. }
  2892. static void option_cat_blob_fd(const char *fd)
  2893. {
  2894. unsigned long n = ulong_arg("--cat-blob-fd", fd);
  2895. if (n > (unsigned long) INT_MAX)
  2896. die("--cat-blob-fd cannot exceed %d", INT_MAX);
  2897. cat_blob_fd = (int) n;
  2898. }
  2899. static void option_export_pack_edges(const char *edges)
  2900. {
  2901. if (pack_edges)
  2902. fclose(pack_edges);
  2903. pack_edges = xfopen(edges, "a");
  2904. }
  2905. static void option_rewrite_submodules(const char *arg, struct string_list *list)
  2906. {
  2907. struct mark_set *ms;
  2908. FILE *fp;
  2909. char *s = xstrdup(arg);
  2910. char *f = strchr(s, ':');
  2911. if (!f)
  2912. die(_("Expected format name:filename for submodule rewrite option"));
  2913. *f = '\0';
  2914. f++;
  2915. ms = xcalloc(1, sizeof(*ms));
  2916. string_list_insert(list, s)->util = ms;
  2917. fp = fopen(f, "r");
  2918. if (!fp)
  2919. die_errno("cannot read '%s'", f);
  2920. read_mark_file(ms, fp, insert_oid_entry);
  2921. fclose(fp);
  2922. }
  2923. static int parse_one_option(const char *option)
  2924. {
  2925. if (skip_prefix(option, "max-pack-size=", &option)) {
  2926. unsigned long v;
  2927. if (!git_parse_ulong(option, &v))
  2928. return 0;
  2929. if (v < 8192) {
  2930. warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v);
  2931. v *= 1024 * 1024;
  2932. } else if (v < 1024 * 1024) {
  2933. warning("minimum max-pack-size is 1 MiB");
  2934. v = 1024 * 1024;
  2935. }
  2936. max_packsize = v;
  2937. } else if (skip_prefix(option, "big-file-threshold=", &option)) {
  2938. unsigned long v;
  2939. if (!git_parse_ulong(option, &v))
  2940. return 0;
  2941. big_file_threshold = v;
  2942. } else if (skip_prefix(option, "depth=", &option)) {
  2943. option_depth(option);
  2944. } else if (skip_prefix(option, "active-branches=", &option)) {
  2945. option_active_branches(option);
  2946. } else if (skip_prefix(option, "export-pack-edges=", &option)) {
  2947. option_export_pack_edges(option);
  2948. } else if (!strcmp(option, "quiet")) {
  2949. show_stats = 0;
  2950. } else if (!strcmp(option, "stats")) {
  2951. show_stats = 1;
  2952. } else if (!strcmp(option, "allow-unsafe-features")) {
  2953. ; /* already handled during early option parsing */
  2954. } else {
  2955. return 0;
  2956. }
  2957. return 1;
  2958. }
  2959. static void check_unsafe_feature(const char *feature, int from_stream)
  2960. {
  2961. if (from_stream && !allow_unsafe_features)
  2962. die(_("feature '%s' forbidden in input without --allow-unsafe-features"),
  2963. feature);
  2964. }
  2965. static int parse_one_feature(const char *feature, int from_stream)
  2966. {
  2967. const char *arg;
  2968. if (skip_prefix(feature, "date-format=", &arg)) {
  2969. option_date_format(arg);
  2970. } else if (skip_prefix(feature, "import-marks=", &arg)) {
  2971. check_unsafe_feature("import-marks", from_stream);
  2972. option_import_marks(arg, from_stream, 0);
  2973. } else if (skip_prefix(feature, "import-marks-if-exists=", &arg)) {
  2974. check_unsafe_feature("import-marks-if-exists", from_stream);
  2975. option_import_marks(arg, from_stream, 1);
  2976. } else if (skip_prefix(feature, "export-marks=", &arg)) {
  2977. check_unsafe_feature(feature, from_stream);
  2978. option_export_marks(arg);
  2979. } else if (!strcmp(feature, "alias")) {
  2980. ; /* Don't die - this feature is supported */
  2981. } else if (skip_prefix(feature, "rewrite-submodules-to=", &arg)) {
  2982. option_rewrite_submodules(arg, &sub_marks_to);
  2983. } else if (skip_prefix(feature, "rewrite-submodules-from=", &arg)) {
  2984. option_rewrite_submodules(arg, &sub_marks_from);
  2985. } else if (skip_prefix(feature, "rewrite-submodules-from=", &arg)) {
  2986. } else if (!strcmp(feature, "get-mark")) {
  2987. ; /* Don't die - this feature is supported */
  2988. } else if (!strcmp(feature, "cat-blob")) {
  2989. ; /* Don't die - this feature is supported */
  2990. } else if (!strcmp(feature, "relative-marks")) {
  2991. relative_marks_paths = 1;
  2992. } else if (!strcmp(feature, "no-relative-marks")) {
  2993. relative_marks_paths = 0;
  2994. } else if (!strcmp(feature, "done")) {
  2995. require_explicit_termination = 1;
  2996. } else if (!strcmp(feature, "force")) {
  2997. force_update = 1;
  2998. } else if (!strcmp(feature, "notes") || !strcmp(feature, "ls")) {
  2999. ; /* do nothing; we have the feature */
  3000. } else {
  3001. return 0;
  3002. }
  3003. return 1;
  3004. }
  3005. static void parse_feature(const char *feature)
  3006. {
  3007. if (seen_data_command)
  3008. die("Got feature command '%s' after data command", feature);
  3009. if (parse_one_feature(feature, 1))
  3010. return;
  3011. die("This version of fast-import does not support feature %s.", feature);
  3012. }
  3013. static void parse_option(const char *option)
  3014. {
  3015. if (seen_data_command)
  3016. die("Got option command '%s' after data command", option);
  3017. if (parse_one_option(option))
  3018. return;
  3019. die("This version of fast-import does not support option: %s", option);
  3020. }
  3021. static void git_pack_config(void)
  3022. {
  3023. int indexversion_value;
  3024. int limit;
  3025. unsigned long packsizelimit_value;
  3026. if (!git_config_get_ulong("pack.depth", &max_depth)) {
  3027. if (max_depth > MAX_DEPTH)
  3028. max_depth = MAX_DEPTH;
  3029. }
  3030. if (!git_config_get_int("pack.indexversion", &indexversion_value)) {
  3031. pack_idx_opts.version = indexversion_value;
  3032. if (pack_idx_opts.version > 2)
  3033. git_die_config("pack.indexversion",
  3034. "bad pack.indexversion=%"PRIu32, pack_idx_opts.version);
  3035. }
  3036. if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value))
  3037. max_packsize = packsizelimit_value;
  3038. if (!git_config_get_int("fastimport.unpacklimit", &limit))
  3039. unpack_limit = limit;
  3040. else if (!git_config_get_int("transfer.unpacklimit", &limit))
  3041. unpack_limit = limit;
  3042. git_config(git_default_config, NULL);
  3043. }
  3044. static const char fast_import_usage[] =
  3045. "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
  3046. static void parse_argv(void)
  3047. {
  3048. unsigned int i;
  3049. for (i = 1; i < global_argc; i++) {
  3050. const char *a = global_argv[i];
  3051. if (*a != '-' || !strcmp(a, "--"))
  3052. break;
  3053. if (!skip_prefix(a, "--", &a))
  3054. die("unknown option %s", a);
  3055. if (parse_one_option(a))
  3056. continue;
  3057. if (parse_one_feature(a, 0))
  3058. continue;
  3059. if (skip_prefix(a, "cat-blob-fd=", &a)) {
  3060. option_cat_blob_fd(a);
  3061. continue;
  3062. }
  3063. die("unknown option --%s", a);
  3064. }
  3065. if (i != global_argc)
  3066. usage(fast_import_usage);
  3067. seen_data_command = 1;
  3068. if (import_marks_file)
  3069. read_marks();
  3070. build_mark_map(&sub_marks_from, &sub_marks_to);
  3071. }
  3072. int cmd_fast_import(int argc, const char **argv, const char *prefix)
  3073. {
  3074. unsigned int i;
  3075. if (argc == 2 && !strcmp(argv[1], "-h"))
  3076. usage(fast_import_usage);
  3077. reset_pack_idx_option(&pack_idx_opts);
  3078. git_pack_config();
  3079. alloc_objects(object_entry_alloc);
  3080. strbuf_init(&command_buf, 0);
  3081. atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
  3082. branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
  3083. avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
  3084. marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
  3085. hashmap_init(&object_table, object_entry_hashcmp, NULL, 0);
  3086. /*
  3087. * We don't parse most options until after we've seen the set of
  3088. * "feature" lines at the start of the stream (which allows the command
  3089. * line to override stream data). But we must do an early parse of any
  3090. * command-line options that impact how we interpret the feature lines.
  3091. */
  3092. for (i = 1; i < argc; i++) {
  3093. const char *arg = argv[i];
  3094. if (*arg != '-' || !strcmp(arg, "--"))
  3095. break;
  3096. if (!strcmp(arg, "--allow-unsafe-features"))
  3097. allow_unsafe_features = 1;
  3098. }
  3099. global_argc = argc;
  3100. global_argv = argv;
  3101. rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free));
  3102. for (i = 0; i < (cmd_save - 1); i++)
  3103. rc_free[i].next = &rc_free[i + 1];
  3104. rc_free[cmd_save - 1].next = NULL;
  3105. start_packfile();
  3106. set_die_routine(die_nicely);
  3107. set_checkpoint_signal();
  3108. while (read_next_command() != EOF) {
  3109. const char *v;
  3110. if (!strcmp("blob", command_buf.buf))
  3111. parse_new_blob();
  3112. else if (skip_prefix(command_buf.buf, "commit ", &v))
  3113. parse_new_commit(v);
  3114. else if (skip_prefix(command_buf.buf, "tag ", &v))
  3115. parse_new_tag(v);
  3116. else if (skip_prefix(command_buf.buf, "reset ", &v))
  3117. parse_reset_branch(v);
  3118. else if (skip_prefix(command_buf.buf, "ls ", &v))
  3119. parse_ls(v, NULL);
  3120. else if (skip_prefix(command_buf.buf, "cat-blob ", &v))
  3121. parse_cat_blob(v);
  3122. else if (skip_prefix(command_buf.buf, "get-mark ", &v))
  3123. parse_get_mark(v);
  3124. else if (!strcmp("checkpoint", command_buf.buf))
  3125. parse_checkpoint();
  3126. else if (!strcmp("done", command_buf.buf))
  3127. break;
  3128. else if (!strcmp("alias", command_buf.buf))
  3129. parse_alias();
  3130. else if (starts_with(command_buf.buf, "progress "))
  3131. parse_progress();
  3132. else if (skip_prefix(command_buf.buf, "feature ", &v))
  3133. parse_feature(v);
  3134. else if (skip_prefix(command_buf.buf, "option git ", &v))
  3135. parse_option(v);
  3136. else if (starts_with(command_buf.buf, "option "))
  3137. /* ignore non-git options*/;
  3138. else
  3139. die("Unsupported command: %s", command_buf.buf);
  3140. if (checkpoint_requested)
  3141. checkpoint();
  3142. }
  3143. /* argv hasn't been parsed yet, do so */
  3144. if (!seen_data_command)
  3145. parse_argv();
  3146. if (require_explicit_termination && feof(stdin))
  3147. die("stream ends early");
  3148. end_packfile();
  3149. dump_branches();
  3150. dump_tags();
  3151. unkeep_all_packs();
  3152. dump_marks();
  3153. if (pack_edges)
  3154. fclose(pack_edges);
  3155. if (show_stats) {
  3156. uintmax_t total_count = 0, duplicate_count = 0;
  3157. for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
  3158. total_count += object_count_by_type[i];
  3159. for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
  3160. duplicate_count += duplicate_count_by_type[i];
  3161. fprintf(stderr, "%s statistics:\n", argv[0]);
  3162. fprintf(stderr, "---------------------------------------------------------------------\n");
  3163. fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
  3164. fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
  3165. fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB], delta_count_attempts_by_type[OBJ_BLOB]);
  3166. fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE], delta_count_attempts_by_type[OBJ_TREE]);
  3167. fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT], delta_count_attempts_by_type[OBJ_COMMIT]);
  3168. fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG], delta_count_attempts_by_type[OBJ_TAG]);
  3169. fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
  3170. fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
  3171. fprintf(stderr, " atoms: %10u\n", atom_cnt);
  3172. fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (tree_entry_allocd + fi_mem_pool.pool_alloc + alloc_count*sizeof(struct object_entry))/1024);
  3173. fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd + fi_mem_pool.pool_alloc) /1024));
  3174. fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
  3175. fprintf(stderr, "---------------------------------------------------------------------\n");
  3176. pack_report();
  3177. fprintf(stderr, "---------------------------------------------------------------------\n");
  3178. fprintf(stderr, "\n");
  3179. }
  3180. return failure ? 1 : 0;
  3181. }