]>
Commit | Line | Data |
---|---|---|
463acbe1 SP |
1 | /* |
2 | Format of STDIN stream: | |
3 | ||
4 | stream ::= cmd*; | |
5 | ||
6 | cmd ::= new_blob | |
c44cdc7e | 7 | | new_commit |
463acbe1 | 8 | | new_tag |
5fced8dc | 9 | | reset_branch |
8c1f22da | 10 | | checkpoint |
ac053c02 | 11 | | progress |
463acbe1 SP |
12 | ; |
13 | ||
c44cdc7e | 14 | new_blob ::= 'blob' lf |
c905e090 | 15 | mark? |
c44cdc7e SP |
16 | file_content; |
17 | file_content ::= data; | |
463acbe1 | 18 | |
c44cdc7e | 19 | new_commit ::= 'commit' sp ref_str lf |
00e2b884 | 20 | mark? |
63e0c8b3 SP |
21 | ('author' sp name '<' email '>' when lf)? |
22 | 'committer' sp name '<' email '>' when lf | |
00e2b884 | 23 | commit_msg |
02f3389d | 24 | ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)? |
62b6f483 | 25 | ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)* |
c44cdc7e | 26 | file_change* |
1fdb649c | 27 | lf?; |
c44cdc7e | 28 | commit_msg ::= data; |
463acbe1 | 29 | |
b6f3481b SP |
30 | file_change ::= file_clr |
31 | | file_del | |
32 | | file_rnm | |
33 | | file_cpy | |
34 | | file_obm | |
35 | | file_inm; | |
825769a8 | 36 | file_clr ::= 'deleteall' lf; |
b715cfbb | 37 | file_del ::= 'D' sp path_str lf; |
f39a946a | 38 | file_rnm ::= 'R' sp path_str sp path_str lf; |
b6f3481b | 39 | file_cpy ::= 'C' sp path_str sp path_str lf; |
b715cfbb SP |
40 | file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf; |
41 | file_inm ::= 'M' sp mode sp 'inline' sp path_str lf | |
42 | data; | |
c44cdc7e SP |
43 | |
44 | new_tag ::= 'tag' sp tag_str lf | |
45 | 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf | |
c905e090 | 46 | 'tagger' sp name '<' email '>' when lf |
c44cdc7e SP |
47 | tag_msg; |
48 | tag_msg ::= data; | |
49 | ||
9938ffc5 SP |
50 | reset_branch ::= 'reset' sp ref_str lf |
51 | ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)? | |
1fdb649c | 52 | lf?; |
5fced8dc | 53 | |
7bfe6e26 | 54 | checkpoint ::= 'checkpoint' lf |
1fdb649c | 55 | lf?; |
7bfe6e26 | 56 | |
ac053c02 SP |
57 | progress ::= 'progress' sp not_lf* lf |
58 | lf?; | |
59 | ||
c44cdc7e SP |
60 | # note: the first idnum in a stream should be 1 and subsequent |
61 | # idnums should not have gaps between values as this will cause | |
62 | # the stream parser to reserve space for the gapped values. An | |
c905e090 | 63 | # idnum can be updated in the future to a new object by issuing |
c44cdc7e | 64 | # a new mark directive with the old idnum. |
c905e090 | 65 | # |
c44cdc7e | 66 | mark ::= 'mark' sp idnum lf; |
3b4dce02 | 67 | data ::= (delimited_data | exact_data) |
2c570cde | 68 | lf?; |
3b4dce02 SP |
69 | |
70 | # note: delim may be any string but must not contain lf. | |
71 | # data_line may contain any data but must not be exactly | |
72 | # delim. | |
73 | delimited_data ::= 'data' sp '<<' delim lf | |
74 | (data_line lf)* | |
c905e090 | 75 | delim lf; |
c44cdc7e SP |
76 | |
77 | # note: declen indicates the length of binary_data in bytes. | |
3b4dce02 | 78 | # declen does not include the lf preceeding the binary data. |
c44cdc7e | 79 | # |
3b4dce02 SP |
80 | exact_data ::= 'data' sp declen lf |
81 | binary_data; | |
c44cdc7e SP |
82 | |
83 | # note: quoted strings are C-style quoting supporting \c for | |
84 | # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn | |
c905e090 | 85 | # is the signed byte value in octal. Note that the only |
c44cdc7e SP |
86 | # characters which must actually be escaped to protect the |
87 | # stream formatting is: \, " and LF. Otherwise these values | |
c905e090 | 88 | # are UTF8. |
c44cdc7e | 89 | # |
6c3aac1c SP |
90 | ref_str ::= ref; |
91 | sha1exp_str ::= sha1exp; | |
92 | tag_str ::= tag; | |
c44cdc7e | 93 | path_str ::= path | '"' quoted(path) '"' ; |
b715cfbb SP |
94 | mode ::= '100644' | '644' |
95 | | '100755' | '755' | |
9981b6d9 | 96 | | '120000' |
b715cfbb | 97 | ; |
c44cdc7e SP |
98 | |
99 | declen ::= # unsigned 32 bit value, ascii base10 notation; | |
2104838b | 100 | bigint ::= # unsigned integer value, ascii base10 notation; |
463acbe1 | 101 | binary_data ::= # file content, not interpreted; |
c44cdc7e | 102 | |
63e0c8b3 SP |
103 | when ::= raw_when | rfc2822_when; |
104 | raw_when ::= ts sp tz; | |
105 | rfc2822_when ::= # Valid RFC 2822 date and time; | |
106 | ||
463acbe1 SP |
107 | sp ::= # ASCII space character; |
108 | lf ::= # ASCII newline (LF) character; | |
c44cdc7e SP |
109 | |
110 | # note: a colon (':') must precede the numerical value assigned to | |
c905e090 | 111 | # an idnum. This is to distinguish it from a ref or tag name as |
c44cdc7e | 112 | # GIT does not permit ':' in ref or tag strings. |
c905e090 | 113 | # |
2104838b | 114 | idnum ::= ':' bigint; |
c44cdc7e SP |
115 | path ::= # GIT style file path, e.g. "a/b/c"; |
116 | ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT"; | |
117 | tag ::= # GIT tag name, e.g. "FIREFOX_1_5"; | |
463acbe1 SP |
118 | sha1exp ::= # Any valid GIT SHA1 expression; |
119 | hexsha1 ::= # SHA1 in hexadecimal format; | |
c44cdc7e SP |
120 | |
121 | # note: name and email are UTF8 strings, however name must not | |
c905e090 | 122 | # contain '<' or lf and email must not contain any of the |
c44cdc7e | 123 | # following: '<', '>', lf. |
c905e090 | 124 | # |
c44cdc7e | 125 | name ::= # valid GIT author/committer name; |
463acbe1 | 126 | email ::= # valid GIT author/committer email; |
c44cdc7e SP |
127 | ts ::= # time since the epoch in seconds, ascii base10 notation; |
128 | tz ::= # GIT style timezone; | |
401d53fa SP |
129 | |
130 | # note: comments may appear anywhere in the input, except | |
131 | # within a data command. Any form of the data command | |
132 | # always escapes the related input from comment processing. | |
133 | # | |
134 | # In case it is not clear, the '#' that starts the comment | |
135 | # must be the first character on that the line (an lf have | |
136 | # preceeded it). | |
137 | # | |
138 | comment ::= '#' not_lf* lf; | |
139 | not_lf ::= # Any byte that is not ASCII newline (LF); | |
463acbe1 SP |
140 | */ |
141 | ||
db5e523f SP |
142 | #include "builtin.h" |
143 | #include "cache.h" | |
144 | #include "object.h" | |
145 | #include "blob.h" | |
463acbe1 | 146 | #include "tree.h" |
7073e69e | 147 | #include "commit.h" |
db5e523f SP |
148 | #include "delta.h" |
149 | #include "pack.h" | |
463acbe1 | 150 | #include "refs.h" |
db5e523f | 151 | #include "csum-file.h" |
c44cdc7e | 152 | #include "quote.h" |
db5e523f | 153 | |
69e74e74 SP |
154 | #define PACK_ID_BITS 16 |
155 | #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1) | |
156 | ||
27d6d290 SP |
157 | struct object_entry |
158 | { | |
159 | struct object_entry *next; | |
10831c55 | 160 | uint32_t offset; |
03842d8e | 161 | unsigned type : TYPE_BITS; |
69e74e74 | 162 | unsigned pack_id : PACK_ID_BITS; |
27d6d290 SP |
163 | unsigned char sha1[20]; |
164 | }; | |
165 | ||
463acbe1 | 166 | struct object_entry_pool |
27d6d290 | 167 | { |
463acbe1 | 168 | struct object_entry_pool *next_pool; |
27d6d290 SP |
169 | struct object_entry *next_free; |
170 | struct object_entry *end; | |
ac47a738 | 171 | struct object_entry entries[FLEX_ARRAY]; /* more */ |
27d6d290 SP |
172 | }; |
173 | ||
d8397168 SP |
174 | struct mark_set |
175 | { | |
d8397168 SP |
176 | union { |
177 | struct object_entry *marked[1024]; | |
178 | struct mark_set *sets[1024]; | |
179 | } data; | |
6f64f6d9 | 180 | unsigned int shift; |
d8397168 SP |
181 | }; |
182 | ||
ac47a738 SP |
183 | struct last_object |
184 | { | |
185 | void *data; | |
4cabf858 | 186 | unsigned long len; |
10831c55 | 187 | uint32_t offset; |
6bb5b329 | 188 | unsigned int depth; |
d489bc14 | 189 | unsigned no_free:1; |
ac47a738 SP |
190 | }; |
191 | ||
463acbe1 SP |
192 | struct mem_pool |
193 | { | |
194 | struct mem_pool *next_pool; | |
195 | char *next_free; | |
196 | char *end; | |
197 | char space[FLEX_ARRAY]; /* more */ | |
198 | }; | |
199 | ||
200 | struct atom_str | |
201 | { | |
202 | struct atom_str *next_atom; | |
10831c55 | 203 | unsigned short str_len; |
463acbe1 SP |
204 | char str_dat[FLEX_ARRAY]; /* more */ |
205 | }; | |
206 | ||
207 | struct tree_content; | |
6bb5b329 SP |
208 | struct tree_entry |
209 | { | |
463acbe1 SP |
210 | struct tree_content *tree; |
211 | struct atom_str* name; | |
4cabf858 SP |
212 | struct tree_entry_ms |
213 | { | |
10831c55 | 214 | uint16_t mode; |
4cabf858 SP |
215 | unsigned char sha1[20]; |
216 | } versions[2]; | |
6bb5b329 SP |
217 | }; |
218 | ||
463acbe1 | 219 | struct tree_content |
6bb5b329 | 220 | { |
463acbe1 SP |
221 | unsigned int entry_capacity; /* must match avail_tree_content */ |
222 | unsigned int entry_count; | |
4cabf858 | 223 | unsigned int delta_depth; |
463acbe1 SP |
224 | struct tree_entry *entries[FLEX_ARRAY]; /* more */ |
225 | }; | |
226 | ||
227 | struct avail_tree_content | |
228 | { | |
229 | unsigned int entry_capacity; /* must match tree_content */ | |
230 | struct avail_tree_content *next_avail; | |
6bb5b329 SP |
231 | }; |
232 | ||
233 | struct branch | |
234 | { | |
463acbe1 SP |
235 | struct branch *table_next_branch; |
236 | struct branch *active_next_branch; | |
6bb5b329 | 237 | const char *name; |
463acbe1 | 238 | struct tree_entry branch_tree; |
69e74e74 | 239 | uintmax_t last_commit; |
734c91f9 SP |
240 | unsigned active : 1; |
241 | unsigned pack_id : PACK_ID_BITS; | |
463acbe1 | 242 | unsigned char sha1[20]; |
6bb5b329 SP |
243 | }; |
244 | ||
72303d44 SP |
245 | struct tag |
246 | { | |
247 | struct tag *next_tag; | |
248 | const char *name; | |
2369ed79 | 249 | unsigned int pack_id; |
72303d44 SP |
250 | unsigned char sha1[20]; |
251 | }; | |
252 | ||
e2eb469d SP |
253 | struct dbuf |
254 | { | |
255 | void *buffer; | |
256 | size_t capacity; | |
257 | }; | |
258 | ||
62b6f483 SP |
259 | struct hash_list |
260 | { | |
261 | struct hash_list *next; | |
262 | unsigned char sha1[20]; | |
263 | }; | |
463acbe1 | 264 | |
63e0c8b3 SP |
265 | typedef enum { |
266 | WHENSPEC_RAW = 1, | |
267 | WHENSPEC_RFC2822, | |
268 | WHENSPEC_NOW, | |
269 | } whenspec_type; | |
270 | ||
904b1941 SP |
271 | struct recent_command |
272 | { | |
273 | struct recent_command *prev; | |
274 | struct recent_command *next; | |
275 | char *buf; | |
276 | }; | |
277 | ||
0ea9f045 | 278 | /* Configured limits on output */ |
d5c57b28 | 279 | static unsigned long max_depth = 10; |
6777a59f | 280 | static off_t max_packsize = (1LL << 32) - 1; |
7073e69e | 281 | static int force_update; |
0ea9f045 SP |
282 | |
283 | /* Stats and misc. counters */ | |
284 | static uintmax_t alloc_count; | |
0ea9f045 SP |
285 | static uintmax_t marks_set_count; |
286 | static uintmax_t object_count_by_type[1 << TYPE_BITS]; | |
287 | static uintmax_t duplicate_count_by_type[1 << TYPE_BITS]; | |
288 | static uintmax_t delta_count_by_type[1 << TYPE_BITS]; | |
a7ddc487 | 289 | static unsigned long object_count; |
6bb5b329 | 290 | static unsigned long branch_count; |
d6c7eb2c | 291 | static unsigned long branch_load_count; |
7073e69e | 292 | static int failure; |
bdf1c06d | 293 | static FILE *pack_edges; |
ac47a738 | 294 | |
463acbe1 SP |
295 | /* Memory pools */ |
296 | static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool); | |
297 | static size_t total_allocd; | |
298 | static struct mem_pool *mem_pool; | |
299 | ||
c44cdc7e | 300 | /* Atom management */ |
463acbe1 SP |
301 | static unsigned int atom_table_sz = 4451; |
302 | static unsigned int atom_cnt; | |
303 | static struct atom_str **atom_table; | |
304 | ||
305 | /* The .pack file being generated */ | |
7bfe6e26 | 306 | static unsigned int pack_id; |
d489bc14 | 307 | static struct packed_git *pack_data; |
7bfe6e26 | 308 | static struct packed_git **all_packs; |
41e5257f | 309 | static unsigned long pack_size; |
ac47a738 SP |
310 | |
311 | /* Table of objects we've written. */ | |
4cabf858 | 312 | static unsigned int object_entry_alloc = 5000; |
463acbe1 SP |
313 | static struct object_entry_pool *blocks; |
314 | static struct object_entry *object_table[1 << 16]; | |
d8397168 | 315 | static struct mark_set *marks; |
a6a1a831 | 316 | static const char* mark_file; |
ac47a738 SP |
317 | |
318 | /* Our last blob */ | |
463acbe1 SP |
319 | static struct last_object last_blob; |
320 | ||
321 | /* Tree management */ | |
322 | static unsigned int tree_entry_alloc = 1000; | |
323 | static void *avail_tree_entry; | |
324 | static unsigned int avail_tree_table_sz = 100; | |
325 | static struct avail_tree_content **avail_tree_table; | |
e2eb469d SP |
326 | static struct dbuf old_tree; |
327 | static struct dbuf new_tree; | |
8bcce301 | 328 | |
6bb5b329 | 329 | /* Branch data */ |
d5c57b28 SP |
330 | static unsigned long max_active_branches = 5; |
331 | static unsigned long cur_active_branches; | |
332 | static unsigned long branch_table_sz = 1039; | |
463acbe1 SP |
333 | static struct branch **branch_table; |
334 | static struct branch *active_branches; | |
335 | ||
72303d44 SP |
336 | /* Tag data */ |
337 | static struct tag *first_tag; | |
338 | static struct tag *last_tag; | |
339 | ||
c44cdc7e | 340 | /* Input stream parsing */ |
63e0c8b3 | 341 | static whenspec_type whenspec = WHENSPEC_RAW; |
4a241d79 | 342 | static struct strbuf command_buf = STRBUF_INIT; |
1fdb649c | 343 | static int unread_command_buf; |
904b1941 SP |
344 | static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL}; |
345 | static struct recent_command *cmd_tail = &cmd_hist; | |
346 | static struct recent_command *rc_free; | |
347 | static unsigned int cmd_save = 100; | |
0ea9f045 | 348 | static uintmax_t next_mark; |
243f801d | 349 | static struct dbuf new_data; |
c44cdc7e | 350 | |
8acb3297 SP |
351 | static void write_branch_report(FILE *rpt, struct branch *b) |
352 | { | |
353 | fprintf(rpt, "%s:\n", b->name); | |
354 | ||
355 | fprintf(rpt, " status :"); | |
356 | if (b->active) | |
357 | fputs(" active", rpt); | |
358 | if (b->branch_tree.tree) | |
359 | fputs(" loaded", rpt); | |
360 | if (is_null_sha1(b->branch_tree.versions[1].sha1)) | |
361 | fputs(" dirty", rpt); | |
362 | fputc('\n', rpt); | |
363 | ||
364 | fprintf(rpt, " tip commit : %s\n", sha1_to_hex(b->sha1)); | |
365 | fprintf(rpt, " old tree : %s\n", sha1_to_hex(b->branch_tree.versions[0].sha1)); | |
366 | fprintf(rpt, " cur tree : %s\n", sha1_to_hex(b->branch_tree.versions[1].sha1)); | |
367 | fprintf(rpt, " commit clock: %" PRIuMAX "\n", b->last_commit); | |
368 | ||
369 | fputs(" last pack : ", rpt); | |
370 | if (b->pack_id < MAX_PACK_ID) | |
371 | fprintf(rpt, "%u", b->pack_id); | |
372 | fputc('\n', rpt); | |
373 | ||
374 | fputc('\n', rpt); | |
375 | } | |
376 | ||
4bf53833 | 377 | static void write_crash_report(const char *err) |
8acb3297 SP |
378 | { |
379 | char *loc = git_path("fast_import_crash_%d", getpid()); | |
380 | FILE *rpt = fopen(loc, "w"); | |
381 | struct branch *b; | |
382 | unsigned long lu; | |
904b1941 | 383 | struct recent_command *rc; |
8acb3297 SP |
384 | |
385 | if (!rpt) { | |
386 | error("can't write crash report %s: %s", loc, strerror(errno)); | |
387 | return; | |
388 | } | |
389 | ||
390 | fprintf(stderr, "fast-import: dumping crash report to %s\n", loc); | |
391 | ||
392 | fprintf(rpt, "fast-import crash report:\n"); | |
393 | fprintf(rpt, " fast-import process: %d\n", getpid()); | |
394 | fprintf(rpt, " parent process : %d\n", getppid()); | |
395 | fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_LOCAL)); | |
396 | fputc('\n', rpt); | |
397 | ||
398 | fputs("fatal: ", rpt); | |
4bf53833 | 399 | fputs(err, rpt); |
8acb3297 SP |
400 | fputc('\n', rpt); |
401 | ||
904b1941 SP |
402 | fputc('\n', rpt); |
403 | fputs("Most Recent Commands Before Crash\n", rpt); | |
404 | fputs("---------------------------------\n", rpt); | |
405 | for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) { | |
406 | if (rc->next == &cmd_hist) | |
407 | fputs("* ", rpt); | |
408 | else | |
409 | fputs(" ", rpt); | |
410 | fputs(rc->buf, rpt); | |
411 | fputc('\n', rpt); | |
412 | } | |
413 | ||
8acb3297 SP |
414 | fputc('\n', rpt); |
415 | fputs("Active Branch LRU\n", rpt); | |
416 | fputs("-----------------\n", rpt); | |
417 | fprintf(rpt, " active_branches = %lu cur, %lu max\n", | |
418 | cur_active_branches, | |
419 | max_active_branches); | |
420 | fputc('\n', rpt); | |
421 | fputs(" pos clock name\n", rpt); | |
422 | fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt); | |
423 | for (b = active_branches, lu = 0; b; b = b->active_next_branch) | |
424 | fprintf(rpt, " %2lu) %6" PRIuMAX" %s\n", | |
425 | ++lu, b->last_commit, b->name); | |
426 | ||
427 | fputc('\n', rpt); | |
428 | fputs("Inactive Branches\n", rpt); | |
429 | fputs("-----------------\n", rpt); | |
430 | for (lu = 0; lu < branch_table_sz; lu++) { | |
431 | for (b = branch_table[lu]; b; b = b->table_next_branch) | |
432 | write_branch_report(rpt, b); | |
433 | } | |
434 | ||
435 | fputc('\n', rpt); | |
436 | fputs("-------------------\n", rpt); | |
437 | fputs("END OF CRASH REPORT\n", rpt); | |
438 | fclose(rpt); | |
439 | } | |
440 | ||
441 | static NORETURN void die_nicely(const char *err, va_list params) | |
442 | { | |
443 | static int zombie; | |
4bf53833 | 444 | char message[2 * PATH_MAX]; |
8acb3297 | 445 | |
4bf53833 | 446 | vsnprintf(message, sizeof(message), err, params); |
8acb3297 | 447 | fputs("fatal: ", stderr); |
4bf53833 | 448 | fputs(message, stderr); |
8acb3297 SP |
449 | fputc('\n', stderr); |
450 | ||
451 | if (!zombie) { | |
452 | zombie = 1; | |
4bf53833 | 453 | write_crash_report(message); |
8acb3297 | 454 | } |
8acb3297 SP |
455 | exit(128); |
456 | } | |
6bb5b329 | 457 | |
03842d8e | 458 | static void alloc_objects(unsigned int cnt) |
8bcce301 | 459 | { |
463acbe1 | 460 | struct object_entry_pool *b; |
27d6d290 | 461 | |
463acbe1 | 462 | b = xmalloc(sizeof(struct object_entry_pool) |
27d6d290 | 463 | + cnt * sizeof(struct object_entry)); |
463acbe1 | 464 | b->next_pool = blocks; |
27d6d290 SP |
465 | b->next_free = b->entries; |
466 | b->end = b->entries + cnt; | |
467 | blocks = b; | |
468 | alloc_count += cnt; | |
469 | } | |
8bcce301 | 470 | |
e5b1444b | 471 | static struct object_entry *new_object(unsigned char *sha1) |
8bcce301 | 472 | { |
27d6d290 | 473 | struct object_entry *e; |
8bcce301 | 474 | |
27d6d290 | 475 | if (blocks->next_free == blocks->end) |
463acbe1 | 476 | alloc_objects(object_entry_alloc); |
8bcce301 | 477 | |
27d6d290 | 478 | e = blocks->next_free++; |
445b8599 | 479 | hashcpy(e->sha1, sha1); |
27d6d290 | 480 | return e; |
8bcce301 SP |
481 | } |
482 | ||
e5b1444b | 483 | static struct object_entry *find_object(unsigned char *sha1) |
463acbe1 SP |
484 | { |
485 | unsigned int h = sha1[0] << 8 | sha1[1]; | |
486 | struct object_entry *e; | |
487 | for (e = object_table[h]; e; e = e->next) | |
445b8599 | 488 | if (!hashcmp(sha1, e->sha1)) |
463acbe1 SP |
489 | return e; |
490 | return NULL; | |
491 | } | |
492 | ||
e5b1444b | 493 | static struct object_entry *insert_object(unsigned char *sha1) |
8bcce301 SP |
494 | { |
495 | unsigned int h = sha1[0] << 8 | sha1[1]; | |
27d6d290 | 496 | struct object_entry *e = object_table[h]; |
463acbe1 | 497 | struct object_entry *p = NULL; |
8bcce301 SP |
498 | |
499 | while (e) { | |
445b8599 | 500 | if (!hashcmp(sha1, e->sha1)) |
8bcce301 SP |
501 | return e; |
502 | p = e; | |
503 | e = e->next; | |
504 | } | |
505 | ||
506 | e = new_object(sha1); | |
463acbe1 | 507 | e->next = NULL; |
8bcce301 SP |
508 | e->offset = 0; |
509 | if (p) | |
510 | p->next = e; | |
511 | else | |
27d6d290 | 512 | object_table[h] = e; |
8bcce301 SP |
513 | return e; |
514 | } | |
db5e523f | 515 | |
463acbe1 SP |
516 | static unsigned int hc_str(const char *s, size_t len) |
517 | { | |
518 | unsigned int r = 0; | |
519 | while (len-- > 0) | |
520 | r = r * 31 + *s++; | |
521 | return r; | |
522 | } | |
523 | ||
e5b1444b | 524 | static void *pool_alloc(size_t len) |
463acbe1 SP |
525 | { |
526 | struct mem_pool *p; | |
527 | void *r; | |
528 | ||
529 | for (p = mem_pool; p; p = p->next_pool) | |
530 | if ((p->end - p->next_free >= len)) | |
531 | break; | |
532 | ||
533 | if (!p) { | |
534 | if (len >= (mem_pool_alloc/2)) { | |
535 | total_allocd += len; | |
536 | return xmalloc(len); | |
537 | } | |
538 | total_allocd += sizeof(struct mem_pool) + mem_pool_alloc; | |
539 | p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc); | |
540 | p->next_pool = mem_pool; | |
541 | p->next_free = p->space; | |
542 | p->end = p->next_free + mem_pool_alloc; | |
543 | mem_pool = p; | |
544 | } | |
545 | ||
546 | r = p->next_free; | |
8d8928b0 SP |
547 | /* round out to a pointer alignment */ |
548 | if (len & (sizeof(void*) - 1)) | |
549 | len += sizeof(void*) - (len & (sizeof(void*) - 1)); | |
463acbe1 SP |
550 | p->next_free += len; |
551 | return r; | |
552 | } | |
553 | ||
e5b1444b | 554 | static void *pool_calloc(size_t count, size_t size) |
463acbe1 SP |
555 | { |
556 | size_t len = count * size; | |
557 | void *r = pool_alloc(len); | |
558 | memset(r, 0, len); | |
559 | return r; | |
560 | } | |
561 | ||
e5b1444b | 562 | static char *pool_strdup(const char *s) |
463acbe1 SP |
563 | { |
564 | char *r = pool_alloc(strlen(s) + 1); | |
565 | strcpy(r, s); | |
566 | return r; | |
567 | } | |
568 | ||
243f801d SP |
569 | static void size_dbuf(struct dbuf *b, size_t maxlen) |
570 | { | |
571 | if (b->buffer) { | |
572 | if (b->capacity >= maxlen) | |
573 | return; | |
574 | free(b->buffer); | |
575 | } | |
576 | b->capacity = ((maxlen / 1024) + 1) * 1024; | |
577 | b->buffer = xmalloc(b->capacity); | |
578 | } | |
579 | ||
0ea9f045 | 580 | static void insert_mark(uintmax_t idnum, struct object_entry *oe) |
d8397168 SP |
581 | { |
582 | struct mark_set *s = marks; | |
583 | while ((idnum >> s->shift) >= 1024) { | |
584 | s = pool_calloc(1, sizeof(struct mark_set)); | |
585 | s->shift = marks->shift + 10; | |
586 | s->data.sets[0] = marks; | |
587 | marks = s; | |
588 | } | |
589 | while (s->shift) { | |
0ea9f045 | 590 | uintmax_t i = idnum >> s->shift; |
d8397168 SP |
591 | idnum -= i << s->shift; |
592 | if (!s->data.sets[i]) { | |
593 | s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set)); | |
594 | s->data.sets[i]->shift = s->shift - 10; | |
595 | } | |
596 | s = s->data.sets[i]; | |
597 | } | |
598 | if (!s->data.marked[idnum]) | |
599 | marks_set_count++; | |
600 | s->data.marked[idnum] = oe; | |
601 | } | |
602 | ||
e5b1444b | 603 | static struct object_entry *find_mark(uintmax_t idnum) |
d8397168 | 604 | { |
0ea9f045 | 605 | uintmax_t orig_idnum = idnum; |
d8397168 SP |
606 | struct mark_set *s = marks; |
607 | struct object_entry *oe = NULL; | |
608 | if ((idnum >> s->shift) < 1024) { | |
609 | while (s && s->shift) { | |
0ea9f045 | 610 | uintmax_t i = idnum >> s->shift; |
d8397168 SP |
611 | idnum -= i << s->shift; |
612 | s = s->data.sets[i]; | |
613 | } | |
614 | if (s) | |
615 | oe = s->data.marked[idnum]; | |
616 | } | |
617 | if (!oe) | |
3efb1f34 | 618 | die("mark :%" PRIuMAX " not declared", orig_idnum); |
d8397168 SP |
619 | return oe; |
620 | } | |
621 | ||
e5b1444b | 622 | static struct atom_str *to_atom(const char *s, unsigned short len) |
463acbe1 SP |
623 | { |
624 | unsigned int hc = hc_str(s, len) % atom_table_sz; | |
625 | struct atom_str *c; | |
626 | ||
627 | for (c = atom_table[hc]; c; c = c->next_atom) | |
628 | if (c->str_len == len && !strncmp(s, c->str_dat, len)) | |
629 | return c; | |
630 | ||
631 | c = pool_alloc(sizeof(struct atom_str) + len + 1); | |
632 | c->str_len = len; | |
633 | strncpy(c->str_dat, s, len); | |
634 | c->str_dat[len] = 0; | |
635 | c->next_atom = atom_table[hc]; | |
636 | atom_table[hc] = c; | |
637 | atom_cnt++; | |
638 | return c; | |
639 | } | |
640 | ||
e5b1444b | 641 | static struct branch *lookup_branch(const char *name) |
463acbe1 SP |
642 | { |
643 | unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; | |
644 | struct branch *b; | |
645 | ||
646 | for (b = branch_table[hc]; b; b = b->table_next_branch) | |
647 | if (!strcmp(name, b->name)) | |
648 | return b; | |
649 | return NULL; | |
650 | } | |
651 | ||
e5b1444b | 652 | static struct branch *new_branch(const char *name) |
463acbe1 SP |
653 | { |
654 | unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz; | |
655 | struct branch* b = lookup_branch(name); | |
656 | ||
657 | if (b) | |
658 | die("Invalid attempt to create duplicate branch: %s", name); | |
ea08a6fd SP |
659 | switch (check_ref_format(name)) { |
660 | case 0: break; /* its valid */ | |
661 | case -2: break; /* valid, but too few '/', allow anyway */ | |
662 | default: | |
c44cdc7e | 663 | die("Branch name doesn't conform to GIT standards: %s", name); |
ea08a6fd | 664 | } |
463acbe1 SP |
665 | |
666 | b = pool_calloc(1, sizeof(struct branch)); | |
667 | b->name = pool_strdup(name); | |
668 | b->table_next_branch = branch_table[hc]; | |
8a8c55ea SP |
669 | b->branch_tree.versions[0].mode = S_IFDIR; |
670 | b->branch_tree.versions[1].mode = S_IFDIR; | |
734c91f9 | 671 | b->active = 0; |
69e74e74 | 672 | b->pack_id = MAX_PACK_ID; |
463acbe1 SP |
673 | branch_table[hc] = b; |
674 | branch_count++; | |
675 | return b; | |
676 | } | |
677 | ||
678 | static unsigned int hc_entries(unsigned int cnt) | |
679 | { | |
680 | cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8; | |
681 | return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1; | |
682 | } | |
683 | ||
e5b1444b | 684 | static struct tree_content *new_tree_content(unsigned int cnt) |
463acbe1 SP |
685 | { |
686 | struct avail_tree_content *f, *l = NULL; | |
687 | struct tree_content *t; | |
688 | unsigned int hc = hc_entries(cnt); | |
689 | ||
690 | for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail) | |
691 | if (f->entry_capacity >= cnt) | |
692 | break; | |
693 | ||
694 | if (f) { | |
695 | if (l) | |
696 | l->next_avail = f->next_avail; | |
697 | else | |
698 | avail_tree_table[hc] = f->next_avail; | |
699 | } else { | |
700 | cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt; | |
701 | f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt); | |
702 | f->entry_capacity = cnt; | |
703 | } | |
704 | ||
705 | t = (struct tree_content*)f; | |
706 | t->entry_count = 0; | |
4cabf858 | 707 | t->delta_depth = 0; |
463acbe1 SP |
708 | return t; |
709 | } | |
710 | ||
711 | static void release_tree_entry(struct tree_entry *e); | |
712 | static void release_tree_content(struct tree_content *t) | |
713 | { | |
714 | struct avail_tree_content *f = (struct avail_tree_content*)t; | |
715 | unsigned int hc = hc_entries(f->entry_capacity); | |
afde8dd9 SP |
716 | f->next_avail = avail_tree_table[hc]; |
717 | avail_tree_table[hc] = f; | |
718 | } | |
719 | ||
720 | static void release_tree_content_recursive(struct tree_content *t) | |
721 | { | |
463acbe1 SP |
722 | unsigned int i; |
723 | for (i = 0; i < t->entry_count; i++) | |
724 | release_tree_entry(t->entries[i]); | |
afde8dd9 | 725 | release_tree_content(t); |
463acbe1 SP |
726 | } |
727 | ||
e5b1444b | 728 | static struct tree_content *grow_tree_content( |
463acbe1 SP |
729 | struct tree_content *t, |
730 | int amt) | |
731 | { | |
732 | struct tree_content *r = new_tree_content(t->entry_count + amt); | |
733 | r->entry_count = t->entry_count; | |
4cabf858 | 734 | r->delta_depth = t->delta_depth; |
463acbe1 SP |
735 | memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0])); |
736 | release_tree_content(t); | |
737 | return r; | |
738 | } | |
739 | ||
e5b1444b | 740 | static struct tree_entry *new_tree_entry(void) |
463acbe1 SP |
741 | { |
742 | struct tree_entry *e; | |
743 | ||
744 | if (!avail_tree_entry) { | |
745 | unsigned int n = tree_entry_alloc; | |
8435a9cb | 746 | total_allocd += n * sizeof(struct tree_entry); |
463acbe1 | 747 | avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry)); |
2eb26d84 | 748 | while (n-- > 1) { |
463acbe1 SP |
749 | *((void**)e) = e + 1; |
750 | e++; | |
751 | } | |
35ef237c | 752 | *((void**)e) = NULL; |
463acbe1 SP |
753 | } |
754 | ||
755 | e = avail_tree_entry; | |
756 | avail_tree_entry = *((void**)e); | |
757 | return e; | |
758 | } | |
759 | ||
760 | static void release_tree_entry(struct tree_entry *e) | |
761 | { | |
762 | if (e->tree) | |
afde8dd9 | 763 | release_tree_content_recursive(e->tree); |
463acbe1 SP |
764 | *((void**)e) = avail_tree_entry; |
765 | avail_tree_entry = e; | |
766 | } | |
767 | ||
b6f3481b SP |
768 | static struct tree_content *dup_tree_content(struct tree_content *s) |
769 | { | |
770 | struct tree_content *d; | |
771 | struct tree_entry *a, *b; | |
772 | unsigned int i; | |
773 | ||
774 | if (!s) | |
775 | return NULL; | |
776 | d = new_tree_content(s->entry_count); | |
777 | for (i = 0; i < s->entry_count; i++) { | |
778 | a = s->entries[i]; | |
779 | b = new_tree_entry(); | |
780 | memcpy(b, a, sizeof(*a)); | |
781 | if (a->tree && is_null_sha1(b->versions[1].sha1)) | |
782 | b->tree = dup_tree_content(a->tree); | |
783 | else | |
784 | b->tree = NULL; | |
785 | d->entries[i] = b; | |
786 | } | |
787 | d->entry_count = s->entry_count; | |
788 | d->delta_depth = s->delta_depth; | |
789 | ||
790 | return d; | |
791 | } | |
792 | ||
fd99224e | 793 | static void start_packfile(void) |
f70b6534 | 794 | { |
8455e484 | 795 | static char tmpfile[PATH_MAX]; |
7bfe6e26 | 796 | struct packed_git *p; |
f70b6534 | 797 | struct pack_header hdr; |
0fcbcae7 | 798 | int pack_fd; |
f70b6534 | 799 | |
8455e484 | 800 | snprintf(tmpfile, sizeof(tmpfile), |
0e55181f | 801 | "%s/tmp_pack_XXXXXX", get_object_directory()); |
7647b17f | 802 | pack_fd = xmkstemp(tmpfile); |
8455e484 SP |
803 | p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2); |
804 | strcpy(p->pack_name, tmpfile); | |
7bfe6e26 | 805 | p->pack_fd = pack_fd; |
f70b6534 SP |
806 | |
807 | hdr.hdr_signature = htonl(PACK_SIGNATURE); | |
808 | hdr.hdr_version = htonl(2); | |
809 | hdr.hdr_entries = 0; | |
0fcbcae7 | 810 | write_or_die(p->pack_fd, &hdr, sizeof(hdr)); |
7bfe6e26 SP |
811 | |
812 | pack_data = p; | |
f70b6534 SP |
813 | pack_size = sizeof(hdr); |
814 | object_count = 0; | |
7bfe6e26 SP |
815 | |
816 | all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1)); | |
817 | all_packs[pack_id] = p; | |
f70b6534 SP |
818 | } |
819 | ||
f70b6534 SP |
820 | static int oecmp (const void *a_, const void *b_) |
821 | { | |
822 | struct object_entry *a = *((struct object_entry**)a_); | |
823 | struct object_entry *b = *((struct object_entry**)b_); | |
824 | return hashcmp(a->sha1, b->sha1); | |
825 | } | |
826 | ||
e5b1444b | 827 | static char *create_index(void) |
f70b6534 | 828 | { |
8455e484 SP |
829 | static char tmpfile[PATH_MAX]; |
830 | SHA_CTX ctx; | |
f70b6534 SP |
831 | struct sha1file *f; |
832 | struct object_entry **idx, **c, **last, *e; | |
833 | struct object_entry_pool *o; | |
ebea9dd4 | 834 | uint32_t array[256]; |
8455e484 | 835 | int i, idx_fd; |
f70b6534 SP |
836 | |
837 | /* Build the sorted table of object IDs. */ | |
838 | idx = xmalloc(object_count * sizeof(struct object_entry*)); | |
839 | c = idx; | |
840 | for (o = blocks; o; o = o->next_pool) | |
d9ee53ce SP |
841 | for (e = o->next_free; e-- != o->entries;) |
842 | if (pack_id == e->pack_id) | |
843 | *c++ = e; | |
f70b6534 | 844 | last = idx + object_count; |
2fce1f3c SP |
845 | if (c != last) |
846 | die("internal consistency error creating the index"); | |
f70b6534 SP |
847 | qsort(idx, object_count, sizeof(struct object_entry*), oecmp); |
848 | ||
849 | /* Generate the fan-out array. */ | |
850 | c = idx; | |
851 | for (i = 0; i < 256; i++) { | |
852 | struct object_entry **next = c;; | |
853 | while (next < last) { | |
854 | if ((*next)->sha1[0] != i) | |
855 | break; | |
856 | next++; | |
857 | } | |
858 | array[i] = htonl(next - idx); | |
859 | c = next; | |
860 | } | |
861 | ||
8455e484 | 862 | snprintf(tmpfile, sizeof(tmpfile), |
0e55181f | 863 | "%s/tmp_idx_XXXXXX", get_object_directory()); |
7647b17f | 864 | idx_fd = xmkstemp(tmpfile); |
8455e484 | 865 | f = sha1fd(idx_fd, tmpfile); |
f70b6534 | 866 | sha1write(f, array, 256 * sizeof(int)); |
8455e484 | 867 | SHA1_Init(&ctx); |
f70b6534 | 868 | for (c = idx; c != last; c++) { |
ebea9dd4 | 869 | uint32_t offset = htonl((*c)->offset); |
f70b6534 SP |
870 | sha1write(f, &offset, 4); |
871 | sha1write(f, (*c)->sha1, sizeof((*c)->sha1)); | |
8455e484 | 872 | SHA1_Update(&ctx, (*c)->sha1, 20); |
f70b6534 | 873 | } |
09543c96 | 874 | sha1write(f, pack_data->sha1, sizeof(pack_data->sha1)); |
8455e484 | 875 | sha1close(f, NULL, 1); |
f70b6534 | 876 | free(idx); |
8455e484 SP |
877 | SHA1_Final(pack_data->sha1, &ctx); |
878 | return tmpfile; | |
879 | } | |
880 | ||
e5b1444b | 881 | static char *keep_pack(char *curr_index_name) |
8455e484 SP |
882 | { |
883 | static char name[PATH_MAX]; | |
3a55602e | 884 | static const char *keep_msg = "fast-import"; |
8455e484 SP |
885 | int keep_fd; |
886 | ||
887 | chmod(pack_data->pack_name, 0444); | |
888 | chmod(curr_index_name, 0444); | |
889 | ||
890 | snprintf(name, sizeof(name), "%s/pack/pack-%s.keep", | |
891 | get_object_directory(), sha1_to_hex(pack_data->sha1)); | |
892 | keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600); | |
893 | if (keep_fd < 0) | |
894 | die("cannot create keep file"); | |
895 | write(keep_fd, keep_msg, strlen(keep_msg)); | |
896 | close(keep_fd); | |
897 | ||
898 | snprintf(name, sizeof(name), "%s/pack/pack-%s.pack", | |
899 | get_object_directory(), sha1_to_hex(pack_data->sha1)); | |
900 | if (move_temp_to_file(pack_data->pack_name, name)) | |
901 | die("cannot store pack file"); | |
8455e484 SP |
902 | |
903 | snprintf(name, sizeof(name), "%s/pack/pack-%s.idx", | |
904 | get_object_directory(), sha1_to_hex(pack_data->sha1)); | |
905 | if (move_temp_to_file(curr_index_name, name)) | |
906 | die("cannot store index file"); | |
907 | return name; | |
908 | } | |
909 | ||
fd99224e | 910 | static void unkeep_all_packs(void) |
8455e484 SP |
911 | { |
912 | static char name[PATH_MAX]; | |
913 | int k; | |
914 | ||
915 | for (k = 0; k < pack_id; k++) { | |
916 | struct packed_git *p = all_packs[k]; | |
917 | snprintf(name, sizeof(name), "%s/pack/pack-%s.keep", | |
918 | get_object_directory(), sha1_to_hex(p->sha1)); | |
919 | unlink(name); | |
920 | } | |
f70b6534 SP |
921 | } |
922 | ||
fd99224e | 923 | static void end_packfile(void) |
f70b6534 | 924 | { |
7bfe6e26 SP |
925 | struct packed_git *old_p = pack_data, *new_p; |
926 | ||
3e005baf | 927 | if (object_count) { |
8455e484 | 928 | char *idx_name; |
2369ed79 SP |
929 | int i; |
930 | struct branch *b; | |
931 | struct tag *t; | |
8455e484 | 932 | |
8b0eca7c DH |
933 | fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1, |
934 | pack_data->pack_name, object_count); | |
935 | close(pack_data->pack_fd); | |
8455e484 | 936 | idx_name = keep_pack(create_index()); |
3e005baf SP |
937 | |
938 | /* Register the packfile with core git's machinary. */ | |
939 | new_p = add_packed_git(idx_name, strlen(idx_name), 1); | |
940 | if (!new_p) | |
941 | die("core git rejected index %s", idx_name); | |
942 | new_p->windows = old_p->windows; | |
2369ed79 | 943 | all_packs[pack_id] = new_p; |
3e005baf | 944 | install_packed_git(new_p); |
2369ed79 SP |
945 | |
946 | /* Print the boundary */ | |
bdf1c06d SP |
947 | if (pack_edges) { |
948 | fprintf(pack_edges, "%s:", new_p->pack_name); | |
949 | for (i = 0; i < branch_table_sz; i++) { | |
950 | for (b = branch_table[i]; b; b = b->table_next_branch) { | |
951 | if (b->pack_id == pack_id) | |
952 | fprintf(pack_edges, " %s", sha1_to_hex(b->sha1)); | |
953 | } | |
2369ed79 | 954 | } |
bdf1c06d SP |
955 | for (t = first_tag; t; t = t->next_tag) { |
956 | if (t->pack_id == pack_id) | |
957 | fprintf(pack_edges, " %s", sha1_to_hex(t->sha1)); | |
958 | } | |
959 | fputc('\n', pack_edges); | |
960 | fflush(pack_edges); | |
2369ed79 | 961 | } |
2369ed79 SP |
962 | |
963 | pack_id++; | |
3e005baf | 964 | } |
12801587 | 965 | else |
3e005baf | 966 | unlink(old_p->pack_name); |
7bfe6e26 | 967 | free(old_p); |
7bfe6e26 SP |
968 | |
969 | /* We can't carry a delta across packfiles. */ | |
970 | free(last_blob.data); | |
971 | last_blob.data = NULL; | |
972 | last_blob.len = 0; | |
973 | last_blob.offset = 0; | |
974 | last_blob.depth = 0; | |
f70b6534 SP |
975 | } |
976 | ||
820b9310 | 977 | static void cycle_packfile(void) |
d9ee53ce SP |
978 | { |
979 | end_packfile(); | |
980 | start_packfile(); | |
981 | } | |
982 | ||
c44cdc7e | 983 | static size_t encode_header( |
ac47a738 | 984 | enum object_type type, |
c44cdc7e | 985 | size_t size, |
ac47a738 | 986 | unsigned char *hdr) |
db5e523f SP |
987 | { |
988 | int n = 1; | |
989 | unsigned char c; | |
990 | ||
1fcdd62a | 991 | if (type < OBJ_COMMIT || type > OBJ_REF_DELTA) |
db5e523f SP |
992 | die("bad type %d", type); |
993 | ||
994 | c = (type << 4) | (size & 15); | |
995 | size >>= 4; | |
996 | while (size) { | |
997 | *hdr++ = c | 0x80; | |
998 | c = size & 0x7f; | |
999 | size >>= 7; | |
1000 | n++; | |
1001 | } | |
1002 | *hdr = c; | |
1003 | return n; | |
1004 | } | |
1005 | ||
ac47a738 SP |
1006 | static int store_object( |
1007 | enum object_type type, | |
1008 | void *dat, | |
c44cdc7e | 1009 | size_t datlen, |
6bb5b329 | 1010 | struct last_object *last, |
d8397168 | 1011 | unsigned char *sha1out, |
0ea9f045 | 1012 | uintmax_t mark) |
db5e523f | 1013 | { |
db5e523f | 1014 | void *out, *delta; |
ac47a738 SP |
1015 | struct object_entry *e; |
1016 | unsigned char hdr[96]; | |
1017 | unsigned char sha1[20]; | |
db5e523f | 1018 | unsigned long hdrlen, deltalen; |
ac47a738 SP |
1019 | SHA_CTX c; |
1020 | z_stream s; | |
1021 | ||
df843662 | 1022 | hdrlen = sprintf((char*)hdr,"%s %lu", typename(type), |
40db58b8 | 1023 | (unsigned long)datlen) + 1; |
ac47a738 SP |
1024 | SHA1_Init(&c); |
1025 | SHA1_Update(&c, hdr, hdrlen); | |
1026 | SHA1_Update(&c, dat, datlen); | |
1027 | SHA1_Final(sha1, &c); | |
6bb5b329 | 1028 | if (sha1out) |
445b8599 | 1029 | hashcpy(sha1out, sha1); |
ac47a738 SP |
1030 | |
1031 | e = insert_object(sha1); | |
d8397168 SP |
1032 | if (mark) |
1033 | insert_mark(mark, e); | |
ac47a738 | 1034 | if (e->offset) { |
6143f064 | 1035 | duplicate_count_by_type[type]++; |
463acbe1 | 1036 | return 1; |
a5c1780a SP |
1037 | } else if (find_sha1_pack(sha1, packed_git)) { |
1038 | e->type = type; | |
1039 | e->pack_id = MAX_PACK_ID; | |
1040 | e->offset = 1; /* just not zero! */ | |
1041 | duplicate_count_by_type[type]++; | |
1042 | return 1; | |
ac47a738 | 1043 | } |
db5e523f | 1044 | |
d9ee53ce | 1045 | if (last && last->data && last->depth < max_depth) { |
ac47a738 | 1046 | delta = diff_delta(last->data, last->len, |
db5e523f SP |
1047 | dat, datlen, |
1048 | &deltalen, 0); | |
d9ee53ce SP |
1049 | if (delta && deltalen >= datlen) { |
1050 | free(delta); | |
1051 | delta = NULL; | |
1052 | } | |
1053 | } else | |
1054 | delta = NULL; | |
db5e523f SP |
1055 | |
1056 | memset(&s, 0, sizeof(s)); | |
1057 | deflateInit(&s, zlib_compression_level); | |
d9ee53ce SP |
1058 | if (delta) { |
1059 | s.next_in = delta; | |
1060 | s.avail_in = deltalen; | |
1061 | } else { | |
1062 | s.next_in = dat; | |
1063 | s.avail_in = datlen; | |
1064 | } | |
1065 | s.avail_out = deflateBound(&s, s.avail_in); | |
1066 | s.next_out = out = xmalloc(s.avail_out); | |
1067 | while (deflate(&s, Z_FINISH) == Z_OK) | |
1068 | /* nothing */; | |
1069 | deflateEnd(&s); | |
1070 | ||
1071 | /* Determine if we should auto-checkpoint. */ | |
e5808826 | 1072 | if ((pack_size + 60 + s.total_out) > max_packsize |
d9ee53ce SP |
1073 | || (pack_size + 60 + s.total_out) < pack_size) { |
1074 | ||
1075 | /* This new object needs to *not* have the current pack_id. */ | |
1076 | e->pack_id = pack_id + 1; | |
820b9310 | 1077 | cycle_packfile(); |
d9ee53ce SP |
1078 | |
1079 | /* We cannot carry a delta into the new pack. */ | |
1080 | if (delta) { | |
1081 | free(delta); | |
1082 | delta = NULL; | |
5d6f3ef6 SP |
1083 | |
1084 | memset(&s, 0, sizeof(s)); | |
1085 | deflateInit(&s, zlib_compression_level); | |
1086 | s.next_in = dat; | |
1087 | s.avail_in = datlen; | |
1088 | s.avail_out = deflateBound(&s, s.avail_in); | |
1089 | s.next_out = out = xrealloc(out, s.avail_out); | |
1090 | while (deflate(&s, Z_FINISH) == Z_OK) | |
1091 | /* nothing */; | |
1092 | deflateEnd(&s); | |
d9ee53ce | 1093 | } |
d9ee53ce SP |
1094 | } |
1095 | ||
1096 | e->type = type; | |
1097 | e->pack_id = pack_id; | |
1098 | e->offset = pack_size; | |
1099 | object_count++; | |
1100 | object_count_by_type[type]++; | |
db5e523f SP |
1101 | |
1102 | if (delta) { | |
d489bc14 SP |
1103 | unsigned long ofs = e->offset - last->offset; |
1104 | unsigned pos = sizeof(hdr) - 1; | |
1105 | ||
4cabf858 | 1106 | delta_count_by_type[type]++; |
ac47a738 | 1107 | last->depth++; |
d489bc14 SP |
1108 | |
1109 | hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr); | |
0fcbcae7 | 1110 | write_or_die(pack_data->pack_fd, hdr, hdrlen); |
d489bc14 SP |
1111 | pack_size += hdrlen; |
1112 | ||
1113 | hdr[pos] = ofs & 127; | |
1114 | while (ofs >>= 7) | |
1115 | hdr[--pos] = 128 | (--ofs & 127); | |
0fcbcae7 | 1116 | write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos); |
d489bc14 | 1117 | pack_size += sizeof(hdr) - pos; |
db5e523f | 1118 | } else { |
463acbe1 SP |
1119 | if (last) |
1120 | last->depth = 0; | |
ac47a738 | 1121 | hdrlen = encode_header(type, datlen, hdr); |
0fcbcae7 | 1122 | write_or_die(pack_data->pack_fd, hdr, hdrlen); |
41e5257f | 1123 | pack_size += hdrlen; |
db5e523f SP |
1124 | } |
1125 | ||
0fcbcae7 | 1126 | write_or_die(pack_data->pack_fd, out, s.total_out); |
41e5257f | 1127 | pack_size += s.total_out; |
db5e523f SP |
1128 | |
1129 | free(out); | |
e7d06a4b | 1130 | free(delta); |
463acbe1 | 1131 | if (last) { |
e7d06a4b | 1132 | if (!last->no_free) |
463acbe1 SP |
1133 | free(last->data); |
1134 | last->data = dat; | |
d489bc14 | 1135 | last->offset = e->offset; |
463acbe1 | 1136 | last->len = datlen; |
463acbe1 SP |
1137 | } |
1138 | return 0; | |
1139 | } | |
1140 | ||
7bfe6e26 SP |
1141 | static void *gfi_unpack_entry( |
1142 | struct object_entry *oe, | |
1143 | unsigned long *sizep) | |
41e5257f | 1144 | { |
21666f1a | 1145 | enum object_type type; |
7bfe6e26 SP |
1146 | struct packed_git *p = all_packs[oe->pack_id]; |
1147 | if (p == pack_data) | |
1148 | p->pack_size = pack_size + 20; | |
21666f1a | 1149 | return unpack_entry(p, oe->offset, &type, sizep); |
41e5257f SP |
1150 | } |
1151 | ||
10831c55 | 1152 | static const char *get_mode(const char *str, uint16_t *modep) |
463acbe1 SP |
1153 | { |
1154 | unsigned char c; | |
10831c55 | 1155 | uint16_t mode = 0; |
463acbe1 SP |
1156 | |
1157 | while ((c = *str++) != ' ') { | |
1158 | if (c < '0' || c > '7') | |
1159 | return NULL; | |
1160 | mode = (mode << 3) + (c - '0'); | |
1161 | } | |
1162 | *modep = mode; | |
1163 | return str; | |
1164 | } | |
1165 | ||
1166 | static void load_tree(struct tree_entry *root) | |
1167 | { | |
4cabf858 | 1168 | unsigned char* sha1 = root->versions[1].sha1; |
463acbe1 SP |
1169 | struct object_entry *myoe; |
1170 | struct tree_content *t; | |
1171 | unsigned long size; | |
1172 | char *buf; | |
1173 | const char *c; | |
463acbe1 SP |
1174 | |
1175 | root->tree = t = new_tree_content(8); | |
4cabf858 | 1176 | if (is_null_sha1(sha1)) |
463acbe1 SP |
1177 | return; |
1178 | ||
4cabf858 | 1179 | myoe = find_object(sha1); |
20f546a8 | 1180 | if (myoe && myoe->pack_id != MAX_PACK_ID) { |
41e5257f | 1181 | if (myoe->type != OBJ_TREE) |
4cabf858 | 1182 | die("Not a tree: %s", sha1_to_hex(sha1)); |
d489bc14 | 1183 | t->delta_depth = 0; |
7bfe6e26 | 1184 | buf = gfi_unpack_entry(myoe, &size); |
463acbe1 | 1185 | } else { |
21666f1a NP |
1186 | enum object_type type; |
1187 | buf = read_sha1_file(sha1, &type, &size); | |
1188 | if (!buf || type != OBJ_TREE) | |
4cabf858 | 1189 | die("Can't load tree %s", sha1_to_hex(sha1)); |
463acbe1 SP |
1190 | } |
1191 | ||
1192 | c = buf; | |
1193 | while (c != (buf + size)) { | |
1194 | struct tree_entry *e = new_tree_entry(); | |
1195 | ||
1196 | if (t->entry_count == t->entry_capacity) | |
f022f85f | 1197 | root->tree = t = grow_tree_content(t, t->entry_count); |
463acbe1 SP |
1198 | t->entries[t->entry_count++] = e; |
1199 | ||
1200 | e->tree = NULL; | |
4cabf858 | 1201 | c = get_mode(c, &e->versions[1].mode); |
463acbe1 | 1202 | if (!c) |
4cabf858 SP |
1203 | die("Corrupt mode in %s", sha1_to_hex(sha1)); |
1204 | e->versions[0].mode = e->versions[1].mode; | |
061e35c5 | 1205 | e->name = to_atom(c, strlen(c)); |
463acbe1 | 1206 | c += e->name->str_len + 1; |
4cabf858 SP |
1207 | hashcpy(e->versions[0].sha1, (unsigned char*)c); |
1208 | hashcpy(e->versions[1].sha1, (unsigned char*)c); | |
463acbe1 SP |
1209 | c += 20; |
1210 | } | |
1211 | free(buf); | |
1212 | } | |
1213 | ||
4cabf858 | 1214 | static int tecmp0 (const void *_a, const void *_b) |
463acbe1 SP |
1215 | { |
1216 | struct tree_entry *a = *((struct tree_entry**)_a); | |
1217 | struct tree_entry *b = *((struct tree_entry**)_b); | |
1218 | return base_name_compare( | |
4cabf858 SP |
1219 | a->name->str_dat, a->name->str_len, a->versions[0].mode, |
1220 | b->name->str_dat, b->name->str_len, b->versions[0].mode); | |
463acbe1 SP |
1221 | } |
1222 | ||
4cabf858 | 1223 | static int tecmp1 (const void *_a, const void *_b) |
463acbe1 | 1224 | { |
4cabf858 SP |
1225 | struct tree_entry *a = *((struct tree_entry**)_a); |
1226 | struct tree_entry *b = *((struct tree_entry**)_b); | |
1227 | return base_name_compare( | |
1228 | a->name->str_dat, a->name->str_len, a->versions[1].mode, | |
1229 | b->name->str_dat, b->name->str_len, b->versions[1].mode); | |
1230 | } | |
1231 | ||
e2eb469d SP |
1232 | static void mktree(struct tree_content *t, |
1233 | int v, | |
1234 | unsigned long *szp, | |
1235 | struct dbuf *b) | |
4cabf858 SP |
1236 | { |
1237 | size_t maxlen = 0; | |
463acbe1 | 1238 | unsigned int i; |
e2eb469d | 1239 | char *c; |
463acbe1 | 1240 | |
4cabf858 SP |
1241 | if (!v) |
1242 | qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0); | |
1243 | else | |
1244 | qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1); | |
463acbe1 | 1245 | |
463acbe1 | 1246 | for (i = 0; i < t->entry_count; i++) { |
4cabf858 SP |
1247 | if (t->entries[i]->versions[v].mode) |
1248 | maxlen += t->entries[i]->name->str_len + 34; | |
463acbe1 SP |
1249 | } |
1250 | ||
243f801d | 1251 | size_dbuf(b, maxlen); |
e2eb469d | 1252 | c = b->buffer; |
463acbe1 SP |
1253 | for (i = 0; i < t->entry_count; i++) { |
1254 | struct tree_entry *e = t->entries[i]; | |
4cabf858 SP |
1255 | if (!e->versions[v].mode) |
1256 | continue; | |
10831c55 | 1257 | c += sprintf(c, "%o", (unsigned int)e->versions[v].mode); |
463acbe1 SP |
1258 | *c++ = ' '; |
1259 | strcpy(c, e->name->str_dat); | |
1260 | c += e->name->str_len + 1; | |
4cabf858 | 1261 | hashcpy((unsigned char*)c, e->versions[v].sha1); |
463acbe1 SP |
1262 | c += 20; |
1263 | } | |
e2eb469d | 1264 | *szp = c - (char*)b->buffer; |
4cabf858 SP |
1265 | } |
1266 | ||
1267 | static void store_tree(struct tree_entry *root) | |
1268 | { | |
1269 | struct tree_content *t = root->tree; | |
1270 | unsigned int i, j, del; | |
e2eb469d | 1271 | unsigned long new_len; |
4cabf858 | 1272 | struct last_object lo; |
d489bc14 | 1273 | struct object_entry *le; |
4cabf858 SP |
1274 | |
1275 | if (!is_null_sha1(root->versions[1].sha1)) | |
1276 | return; | |
1277 | ||
1278 | for (i = 0; i < t->entry_count; i++) { | |
1279 | if (t->entries[i]->tree) | |
1280 | store_tree(t->entries[i]); | |
1281 | } | |
1282 | ||
d489bc14 | 1283 | le = find_object(root->versions[0].sha1); |
7bfe6e26 SP |
1284 | if (!S_ISDIR(root->versions[0].mode) |
1285 | || !le | |
1286 | || le->pack_id != pack_id) { | |
4cabf858 SP |
1287 | lo.data = NULL; |
1288 | lo.depth = 0; | |
b259157f | 1289 | lo.no_free = 0; |
4cabf858 | 1290 | } else { |
e2eb469d SP |
1291 | mktree(t, 0, &lo.len, &old_tree); |
1292 | lo.data = old_tree.buffer; | |
d489bc14 | 1293 | lo.offset = le->offset; |
4cabf858 | 1294 | lo.depth = t->delta_depth; |
e2eb469d | 1295 | lo.no_free = 1; |
4cabf858 | 1296 | } |
4cabf858 | 1297 | |
8a8c55ea | 1298 | mktree(t, 1, &new_len, &new_tree); |
e2eb469d | 1299 | store_object(OBJ_TREE, new_tree.buffer, new_len, |
4cabf858 | 1300 | &lo, root->versions[1].sha1, 0); |
4cabf858 SP |
1301 | |
1302 | t->delta_depth = lo.depth; | |
4cabf858 SP |
1303 | for (i = 0, j = 0, del = 0; i < t->entry_count; i++) { |
1304 | struct tree_entry *e = t->entries[i]; | |
1305 | if (e->versions[1].mode) { | |
1306 | e->versions[0].mode = e->versions[1].mode; | |
1307 | hashcpy(e->versions[0].sha1, e->versions[1].sha1); | |
1308 | t->entries[j++] = e; | |
1309 | } else { | |
1310 | release_tree_entry(e); | |
1311 | del++; | |
1312 | } | |
1313 | } | |
1314 | t->entry_count -= del; | |
463acbe1 SP |
1315 | } |
1316 | ||
1317 | static int tree_content_set( | |
1318 | struct tree_entry *root, | |
1319 | const char *p, | |
1320 | const unsigned char *sha1, | |
f39a946a SP |
1321 | const uint16_t mode, |
1322 | struct tree_content *subtree) | |
463acbe1 SP |
1323 | { |
1324 | struct tree_content *t = root->tree; | |
1325 | const char *slash1; | |
1326 | unsigned int i, n; | |
1327 | struct tree_entry *e; | |
1328 | ||
1329 | slash1 = strchr(p, '/'); | |
1330 | if (slash1) | |
1331 | n = slash1 - p; | |
1332 | else | |
1333 | n = strlen(p); | |
475d1b33 SP |
1334 | if (!n) |
1335 | die("Empty path component found in input"); | |
f39a946a SP |
1336 | if (!slash1 && !S_ISDIR(mode) && subtree) |
1337 | die("Non-directories cannot have subtrees"); | |
463acbe1 SP |
1338 | |
1339 | for (i = 0; i < t->entry_count; i++) { | |
1340 | e = t->entries[i]; | |
1341 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
1342 | if (!slash1) { | |
f39a946a SP |
1343 | if (!S_ISDIR(mode) |
1344 | && e->versions[1].mode == mode | |
4cabf858 | 1345 | && !hashcmp(e->versions[1].sha1, sha1)) |
463acbe1 | 1346 | return 0; |
4cabf858 SP |
1347 | e->versions[1].mode = mode; |
1348 | hashcpy(e->versions[1].sha1, sha1); | |
f39a946a | 1349 | if (e->tree) |
afde8dd9 | 1350 | release_tree_content_recursive(e->tree); |
f39a946a | 1351 | e->tree = subtree; |
4cabf858 | 1352 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1353 | return 1; |
1354 | } | |
4cabf858 | 1355 | if (!S_ISDIR(e->versions[1].mode)) { |
463acbe1 | 1356 | e->tree = new_tree_content(8); |
4cabf858 | 1357 | e->versions[1].mode = S_IFDIR; |
463acbe1 SP |
1358 | } |
1359 | if (!e->tree) | |
1360 | load_tree(e); | |
f39a946a | 1361 | if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) { |
4cabf858 | 1362 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1363 | return 1; |
1364 | } | |
1365 | return 0; | |
1366 | } | |
1367 | } | |
1368 | ||
1369 | if (t->entry_count == t->entry_capacity) | |
f022f85f | 1370 | root->tree = t = grow_tree_content(t, t->entry_count); |
463acbe1 | 1371 | e = new_tree_entry(); |
061e35c5 | 1372 | e->name = to_atom(p, n); |
4cabf858 SP |
1373 | e->versions[0].mode = 0; |
1374 | hashclr(e->versions[0].sha1); | |
463acbe1 SP |
1375 | t->entries[t->entry_count++] = e; |
1376 | if (slash1) { | |
1377 | e->tree = new_tree_content(8); | |
4cabf858 | 1378 | e->versions[1].mode = S_IFDIR; |
f39a946a | 1379 | tree_content_set(e, slash1 + 1, sha1, mode, subtree); |
463acbe1 | 1380 | } else { |
f39a946a | 1381 | e->tree = subtree; |
4cabf858 SP |
1382 | e->versions[1].mode = mode; |
1383 | hashcpy(e->versions[1].sha1, sha1); | |
463acbe1 | 1384 | } |
4cabf858 | 1385 | hashclr(root->versions[1].sha1); |
463acbe1 SP |
1386 | return 1; |
1387 | } | |
1388 | ||
f39a946a SP |
1389 | static int tree_content_remove( |
1390 | struct tree_entry *root, | |
1391 | const char *p, | |
1392 | struct tree_entry *backup_leaf) | |
463acbe1 SP |
1393 | { |
1394 | struct tree_content *t = root->tree; | |
1395 | const char *slash1; | |
1396 | unsigned int i, n; | |
1397 | struct tree_entry *e; | |
1398 | ||
1399 | slash1 = strchr(p, '/'); | |
1400 | if (slash1) | |
1401 | n = slash1 - p; | |
1402 | else | |
1403 | n = strlen(p); | |
1404 | ||
1405 | for (i = 0; i < t->entry_count; i++) { | |
1406 | e = t->entries[i]; | |
1407 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
4cabf858 | 1408 | if (!slash1 || !S_ISDIR(e->versions[1].mode)) |
463acbe1 SP |
1409 | goto del_entry; |
1410 | if (!e->tree) | |
1411 | load_tree(e); | |
f39a946a | 1412 | if (tree_content_remove(e, slash1 + 1, backup_leaf)) { |
b54d6422 SP |
1413 | for (n = 0; n < e->tree->entry_count; n++) { |
1414 | if (e->tree->entries[n]->versions[1].mode) { | |
1415 | hashclr(root->versions[1].sha1); | |
1416 | return 1; | |
1417 | } | |
1418 | } | |
f39a946a | 1419 | backup_leaf = NULL; |
b54d6422 | 1420 | goto del_entry; |
463acbe1 SP |
1421 | } |
1422 | return 0; | |
1423 | } | |
1424 | } | |
1425 | return 0; | |
1426 | ||
1427 | del_entry: | |
f39a946a SP |
1428 | if (backup_leaf) |
1429 | memcpy(backup_leaf, e, sizeof(*backup_leaf)); | |
1430 | else if (e->tree) | |
4cabf858 | 1431 | release_tree_content_recursive(e->tree); |
f39a946a | 1432 | e->tree = NULL; |
4cabf858 SP |
1433 | e->versions[1].mode = 0; |
1434 | hashclr(e->versions[1].sha1); | |
1435 | hashclr(root->versions[1].sha1); | |
ac47a738 | 1436 | return 1; |
db5e523f SP |
1437 | } |
1438 | ||
b6f3481b SP |
1439 | static int tree_content_get( |
1440 | struct tree_entry *root, | |
1441 | const char *p, | |
1442 | struct tree_entry *leaf) | |
1443 | { | |
1444 | struct tree_content *t = root->tree; | |
1445 | const char *slash1; | |
1446 | unsigned int i, n; | |
1447 | struct tree_entry *e; | |
1448 | ||
1449 | slash1 = strchr(p, '/'); | |
1450 | if (slash1) | |
1451 | n = slash1 - p; | |
1452 | else | |
1453 | n = strlen(p); | |
1454 | ||
1455 | for (i = 0; i < t->entry_count; i++) { | |
1456 | e = t->entries[i]; | |
1457 | if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) { | |
1458 | if (!slash1) { | |
1459 | memcpy(leaf, e, sizeof(*leaf)); | |
1460 | if (e->tree && is_null_sha1(e->versions[1].sha1)) | |
1461 | leaf->tree = dup_tree_content(e->tree); | |
1462 | else | |
1463 | leaf->tree = NULL; | |
1464 | return 1; | |
1465 | } | |
1466 | if (!S_ISDIR(e->versions[1].mode)) | |
1467 | return 0; | |
1468 | if (!e->tree) | |
1469 | load_tree(e); | |
1470 | return tree_content_get(e, slash1 + 1, leaf); | |
1471 | } | |
1472 | } | |
1473 | return 0; | |
1474 | } | |
1475 | ||
7073e69e | 1476 | static int update_branch(struct branch *b) |
463acbe1 SP |
1477 | { |
1478 | static const char *msg = "fast-import"; | |
7073e69e SP |
1479 | struct ref_lock *lock; |
1480 | unsigned char old_sha1[20]; | |
1481 | ||
1482 | if (read_ref(b->name, old_sha1)) | |
1483 | hashclr(old_sha1); | |
68db31cc | 1484 | lock = lock_any_ref_for_update(b->name, old_sha1, 0); |
7073e69e SP |
1485 | if (!lock) |
1486 | return error("Unable to lock %s", b->name); | |
1487 | if (!force_update && !is_null_sha1(old_sha1)) { | |
1488 | struct commit *old_cmit, *new_cmit; | |
1489 | ||
1490 | old_cmit = lookup_commit_reference_gently(old_sha1, 0); | |
1491 | new_cmit = lookup_commit_reference_gently(b->sha1, 0); | |
1492 | if (!old_cmit || !new_cmit) { | |
1493 | unlock_ref(lock); | |
1494 | return error("Branch %s is missing commits.", b->name); | |
1495 | } | |
1496 | ||
4a164d48 | 1497 | if (!in_merge_bases(old_cmit, &new_cmit, 1)) { |
7073e69e | 1498 | unlock_ref(lock); |
46efd2d9 | 1499 | warning("Not updating %s" |
7073e69e SP |
1500 | " (new tip %s does not contain %s)", |
1501 | b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1)); | |
1502 | return -1; | |
1503 | } | |
1504 | } | |
1505 | if (write_ref_sha1(lock, b->sha1, msg) < 0) | |
1506 | return error("Unable to update %s", b->name); | |
1507 | return 0; | |
1508 | } | |
1509 | ||
1510 | static void dump_branches(void) | |
1511 | { | |
463acbe1 SP |
1512 | unsigned int i; |
1513 | struct branch *b; | |
463acbe1 SP |
1514 | |
1515 | for (i = 0; i < branch_table_sz; i++) { | |
7073e69e SP |
1516 | for (b = branch_table[i]; b; b = b->table_next_branch) |
1517 | failure |= update_branch(b); | |
463acbe1 SP |
1518 | } |
1519 | } | |
1520 | ||
fd99224e | 1521 | static void dump_tags(void) |
72303d44 SP |
1522 | { |
1523 | static const char *msg = "fast-import"; | |
1524 | struct tag *t; | |
1525 | struct ref_lock *lock; | |
7073e69e | 1526 | char ref_name[PATH_MAX]; |
72303d44 SP |
1527 | |
1528 | for (t = first_tag; t; t = t->next_tag) { | |
7073e69e SP |
1529 | sprintf(ref_name, "tags/%s", t->name); |
1530 | lock = lock_ref_sha1(ref_name, NULL); | |
72303d44 | 1531 | if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0) |
7073e69e | 1532 | failure |= error("Unable to update %s", ref_name); |
72303d44 SP |
1533 | } |
1534 | } | |
1535 | ||
a6a1a831 | 1536 | static void dump_marks_helper(FILE *f, |
0ea9f045 | 1537 | uintmax_t base, |
a6a1a831 SP |
1538 | struct mark_set *m) |
1539 | { | |
0ea9f045 | 1540 | uintmax_t k; |
a6a1a831 SP |
1541 | if (m->shift) { |
1542 | for (k = 0; k < 1024; k++) { | |
1543 | if (m->data.sets[k]) | |
1544 | dump_marks_helper(f, (base + k) << m->shift, | |
1545 | m->data.sets[k]); | |
1546 | } | |
1547 | } else { | |
1548 | for (k = 0; k < 1024; k++) { | |
1549 | if (m->data.marked[k]) | |
3efb1f34 | 1550 | fprintf(f, ":%" PRIuMAX " %s\n", base + k, |
a6a1a831 SP |
1551 | sha1_to_hex(m->data.marked[k]->sha1)); |
1552 | } | |
1553 | } | |
1554 | } | |
1555 | ||
fd99224e | 1556 | static void dump_marks(void) |
a6a1a831 | 1557 | { |
60b9004c SP |
1558 | static struct lock_file mark_lock; |
1559 | int mark_fd; | |
1560 | FILE *f; | |
1561 | ||
1562 | if (!mark_file) | |
1563 | return; | |
1564 | ||
1565 | mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0); | |
1566 | if (mark_fd < 0) { | |
1567 | failure |= error("Unable to write marks file %s: %s", | |
1568 | mark_file, strerror(errno)); | |
1569 | return; | |
a6a1a831 | 1570 | } |
60b9004c SP |
1571 | |
1572 | f = fdopen(mark_fd, "w"); | |
1573 | if (!f) { | |
1574 | rollback_lock_file(&mark_lock); | |
1575 | failure |= error("Unable to write marks file %s: %s", | |
1576 | mark_file, strerror(errno)); | |
1577 | return; | |
a6a1a831 | 1578 | } |
60b9004c SP |
1579 | |
1580 | dump_marks_helper(f, 0, marks); | |
1581 | fclose(f); | |
1582 | if (commit_lock_file(&mark_lock)) | |
1583 | failure |= error("Unable to write marks file %s: %s", | |
1584 | mark_file, strerror(errno)); | |
a6a1a831 SP |
1585 | } |
1586 | ||
e6c019d0 | 1587 | static int read_next_command(void) |
c44cdc7e | 1588 | { |
e6c019d0 PH |
1589 | static int stdin_eof = 0; |
1590 | ||
1591 | if (stdin_eof) { | |
1592 | unread_command_buf = 0; | |
1593 | return EOF; | |
1594 | } | |
1595 | ||
401d53fa | 1596 | do { |
904b1941 | 1597 | if (unread_command_buf) { |
1fdb649c | 1598 | unread_command_buf = 0; |
904b1941 SP |
1599 | } else { |
1600 | struct recent_command *rc; | |
1601 | ||
b449f4cf | 1602 | strbuf_detach(&command_buf); |
e6c019d0 PH |
1603 | stdin_eof = strbuf_getline(&command_buf, stdin, '\n'); |
1604 | if (stdin_eof) | |
1605 | return EOF; | |
904b1941 SP |
1606 | |
1607 | rc = rc_free; | |
1608 | if (rc) | |
1609 | rc_free = rc->next; | |
1610 | else { | |
1611 | rc = cmd_hist.next; | |
1612 | cmd_hist.next = rc->next; | |
1613 | cmd_hist.next->prev = &cmd_hist; | |
1614 | free(rc->buf); | |
1615 | } | |
1616 | ||
1617 | rc->buf = command_buf.buf; | |
1618 | rc->prev = cmd_tail; | |
1619 | rc->next = cmd_hist.prev; | |
1620 | rc->prev->next = rc; | |
1621 | cmd_tail = rc; | |
1622 | } | |
1623 | } while (command_buf.buf[0] == '#'); | |
e6c019d0 PH |
1624 | |
1625 | return 0; | |
c44cdc7e SP |
1626 | } |
1627 | ||
7e5dcea8 | 1628 | static void skip_optional_lf(void) |
2c570cde SP |
1629 | { |
1630 | int term_char = fgetc(stdin); | |
1631 | if (term_char != '\n' && term_char != EOF) | |
1632 | ungetc(term_char, stdin); | |
1633 | } | |
1634 | ||
fd99224e | 1635 | static void cmd_mark(void) |
c44cdc7e | 1636 | { |
599065a3 | 1637 | if (!prefixcmp(command_buf.buf, "mark :")) { |
0ea9f045 | 1638 | next_mark = strtoumax(command_buf.buf + 6, NULL, 10); |
c44cdc7e SP |
1639 | read_next_command(); |
1640 | } | |
1641 | else | |
d8397168 | 1642 | next_mark = 0; |
c44cdc7e SP |
1643 | } |
1644 | ||
e5b1444b | 1645 | static void *cmd_data (size_t *size) |
c44cdc7e | 1646 | { |
4a241d79 | 1647 | struct strbuf buffer; |
c44cdc7e | 1648 | |
f1696ee3 | 1649 | strbuf_init(&buffer, 0); |
599065a3 | 1650 | if (prefixcmp(command_buf.buf, "data ")) |
c44cdc7e SP |
1651 | die("Expected 'data n' command, found: %s", command_buf.buf); |
1652 | ||
599065a3 | 1653 | if (!prefixcmp(command_buf.buf + 5, "<<")) { |
3b4dce02 | 1654 | char *term = xstrdup(command_buf.buf + 5 + 2); |
4a241d79 PH |
1655 | size_t term_len = command_buf.len - 5 - 2; |
1656 | ||
3b4dce02 | 1657 | for (;;) { |
e6c019d0 | 1658 | if (strbuf_getline(&command_buf, stdin, '\n') == EOF) |
3b4dce02 SP |
1659 | die("EOF in data (terminator '%s' not found)", term); |
1660 | if (term_len == command_buf.len | |
1661 | && !strcmp(term, command_buf.buf)) | |
1662 | break; | |
4a241d79 PH |
1663 | strbuf_addbuf(&buffer, &command_buf); |
1664 | strbuf_addch(&buffer, '\n'); | |
3b4dce02 SP |
1665 | } |
1666 | free(term); | |
1667 | } | |
1668 | else { | |
4a241d79 PH |
1669 | size_t n = 0, length; |
1670 | ||
3b4dce02 | 1671 | length = strtoul(command_buf.buf + 5, NULL, 10); |
4a241d79 | 1672 | |
3b4dce02 | 1673 | while (n < length) { |
4a241d79 | 1674 | size_t s = strbuf_fread(&buffer, length - n, stdin); |
3b4dce02 | 1675 | if (!s && feof(stdin)) |
40db58b8 JS |
1676 | die("EOF in data (%lu bytes remaining)", |
1677 | (unsigned long)(length - n)); | |
3b4dce02 SP |
1678 | n += s; |
1679 | } | |
c44cdc7e SP |
1680 | } |
1681 | ||
2c570cde | 1682 | skip_optional_lf(); |
4a241d79 PH |
1683 | *size = buffer.len; |
1684 | return strbuf_detach(&buffer); | |
c44cdc7e SP |
1685 | } |
1686 | ||
63e0c8b3 SP |
1687 | static int validate_raw_date(const char *src, char *result, int maxlen) |
1688 | { | |
1689 | const char *orig_src = src; | |
1690 | char *endp, sign; | |
1691 | ||
1692 | strtoul(src, &endp, 10); | |
1693 | if (endp == src || *endp != ' ') | |
1694 | return -1; | |
1695 | ||
1696 | src = endp + 1; | |
1697 | if (*src != '-' && *src != '+') | |
1698 | return -1; | |
1699 | sign = *src; | |
1700 | ||
1701 | strtoul(src + 1, &endp, 10); | |
1702 | if (endp == src || *endp || (endp - orig_src) >= maxlen) | |
1703 | return -1; | |
1704 | ||
1705 | strcpy(result, orig_src); | |
1706 | return 0; | |
1707 | } | |
1708 | ||
1709 | static char *parse_ident(const char *buf) | |
1710 | { | |
1711 | const char *gt; | |
1712 | size_t name_len; | |
1713 | char *ident; | |
1714 | ||
1715 | gt = strrchr(buf, '>'); | |
1716 | if (!gt) | |
1717 | die("Missing > in ident string: %s", buf); | |
1718 | gt++; | |
1719 | if (*gt != ' ') | |
1720 | die("Missing space after > in ident string: %s", buf); | |
1721 | gt++; | |
1722 | name_len = gt - buf; | |
1723 | ident = xmalloc(name_len + 24); | |
1724 | strncpy(ident, buf, name_len); | |
1725 | ||
1726 | switch (whenspec) { | |
1727 | case WHENSPEC_RAW: | |
1728 | if (validate_raw_date(gt, ident + name_len, 24) < 0) | |
1729 | die("Invalid raw date \"%s\" in ident: %s", gt, buf); | |
1730 | break; | |
1731 | case WHENSPEC_RFC2822: | |
1732 | if (parse_date(gt, ident + name_len, 24) < 0) | |
1733 | die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf); | |
1734 | break; | |
1735 | case WHENSPEC_NOW: | |
1736 | if (strcmp("now", gt)) | |
1737 | die("Date in ident must be 'now': %s", buf); | |
1738 | datestamp(ident + name_len, 24); | |
1739 | break; | |
1740 | } | |
1741 | ||
1742 | return ident; | |
1743 | } | |
1744 | ||
fd99224e | 1745 | static void cmd_new_blob(void) |
6143f064 | 1746 | { |
d8397168 SP |
1747 | size_t l; |
1748 | void *d; | |
c44cdc7e SP |
1749 | |
1750 | read_next_command(); | |
1751 | cmd_mark(); | |
d8397168 | 1752 | d = cmd_data(&l); |
6143f064 | 1753 | |
d8397168 SP |
1754 | if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark)) |
1755 | free(d); | |
6143f064 SP |
1756 | } |
1757 | ||
fd99224e | 1758 | static void unload_one_branch(void) |
6bb5b329 | 1759 | { |
41e5257f SP |
1760 | while (cur_active_branches |
1761 | && cur_active_branches >= max_active_branches) { | |
6777a59f | 1762 | uintmax_t min_commit = ULONG_MAX; |
463acbe1 SP |
1763 | struct branch *e, *l = NULL, *p = NULL; |
1764 | ||
1765 | for (e = active_branches; e; e = e->active_next_branch) { | |
1766 | if (e->last_commit < min_commit) { | |
1767 | p = l; | |
1768 | min_commit = e->last_commit; | |
1769 | } | |
1770 | l = e; | |
1771 | } | |
1772 | ||
1773 | if (p) { | |
1774 | e = p->active_next_branch; | |
1775 | p->active_next_branch = e->active_next_branch; | |
1776 | } else { | |
1777 | e = active_branches; | |
1778 | active_branches = e->active_next_branch; | |
1779 | } | |
734c91f9 | 1780 | e->active = 0; |
463acbe1 SP |
1781 | e->active_next_branch = NULL; |
1782 | if (e->branch_tree.tree) { | |
afde8dd9 | 1783 | release_tree_content_recursive(e->branch_tree.tree); |
463acbe1 SP |
1784 | e->branch_tree.tree = NULL; |
1785 | } | |
1786 | cur_active_branches--; | |
6bb5b329 | 1787 | } |
6bb5b329 SP |
1788 | } |
1789 | ||
463acbe1 | 1790 | static void load_branch(struct branch *b) |
6bb5b329 | 1791 | { |
463acbe1 | 1792 | load_tree(&b->branch_tree); |
734c91f9 SP |
1793 | if (!b->active) { |
1794 | b->active = 1; | |
1795 | b->active_next_branch = active_branches; | |
1796 | active_branches = b; | |
1797 | cur_active_branches++; | |
1798 | branch_load_count++; | |
1799 | } | |
6bb5b329 SP |
1800 | } |
1801 | ||
463acbe1 | 1802 | static void file_change_m(struct branch *b) |
6bb5b329 | 1803 | { |
c44cdc7e SP |
1804 | const char *p = command_buf.buf + 2; |
1805 | char *p_uq; | |
1806 | const char *endp; | |
10e8d688 | 1807 | struct object_entry *oe = oe; |
463acbe1 | 1808 | unsigned char sha1[20]; |
10831c55 | 1809 | uint16_t mode, inline_data = 0; |
6bb5b329 | 1810 | |
c44cdc7e SP |
1811 | p = get_mode(p, &mode); |
1812 | if (!p) | |
1813 | die("Corrupt mode: %s", command_buf.buf); | |
1814 | switch (mode) { | |
1815 | case S_IFREG | 0644: | |
1816 | case S_IFREG | 0755: | |
ace4a9d1 | 1817 | case S_IFLNK: |
c44cdc7e SP |
1818 | case 0644: |
1819 | case 0755: | |
1820 | /* ok */ | |
1821 | break; | |
1822 | default: | |
1823 | die("Corrupt mode: %s", command_buf.buf); | |
1824 | } | |
1825 | ||
d8397168 SP |
1826 | if (*p == ':') { |
1827 | char *x; | |
0ea9f045 | 1828 | oe = find_mark(strtoumax(p + 1, &x, 10)); |
cacbdd0a | 1829 | hashcpy(sha1, oe->sha1); |
d8397168 | 1830 | p = x; |
599065a3 | 1831 | } else if (!prefixcmp(p, "inline")) { |
b715cfbb SP |
1832 | inline_data = 1; |
1833 | p += 6; | |
d8397168 SP |
1834 | } else { |
1835 | if (get_sha1_hex(p, sha1)) | |
1836 | die("Invalid SHA1: %s", command_buf.buf); | |
1837 | oe = find_object(sha1); | |
1838 | p += 40; | |
1839 | } | |
c44cdc7e SP |
1840 | if (*p++ != ' ') |
1841 | die("Missing space after SHA1: %s", command_buf.buf); | |
1842 | ||
1843 | p_uq = unquote_c_style(p, &endp); | |
1844 | if (p_uq) { | |
1845 | if (*endp) | |
1846 | die("Garbage after path in: %s", command_buf.buf); | |
1847 | p = p_uq; | |
1848 | } | |
6bb5b329 | 1849 | |
b715cfbb SP |
1850 | if (inline_data) { |
1851 | size_t l; | |
1852 | void *d; | |
1853 | if (!p_uq) | |
1854 | p = p_uq = xstrdup(p); | |
1855 | read_next_command(); | |
1856 | d = cmd_data(&l); | |
1857 | if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0)) | |
1858 | free(d); | |
1859 | } else if (oe) { | |
7111feed | 1860 | if (oe->type != OBJ_BLOB) |
c44cdc7e | 1861 | die("Not a blob (actually a %s): %s", |
df843662 | 1862 | command_buf.buf, typename(oe->type)); |
7111feed | 1863 | } else { |
21666f1a NP |
1864 | enum object_type type = sha1_object_info(sha1, NULL); |
1865 | if (type < 0) | |
c44cdc7e | 1866 | die("Blob not found: %s", command_buf.buf); |
21666f1a | 1867 | if (type != OBJ_BLOB) |
c44cdc7e | 1868 | die("Not a blob (actually a %s): %s", |
21666f1a | 1869 | typename(type), command_buf.buf); |
7111feed | 1870 | } |
6bb5b329 | 1871 | |
f39a946a | 1872 | tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL); |
e7d06a4b | 1873 | free(p_uq); |
463acbe1 | 1874 | } |
6bb5b329 | 1875 | |
463acbe1 SP |
1876 | static void file_change_d(struct branch *b) |
1877 | { | |
c44cdc7e SP |
1878 | const char *p = command_buf.buf + 2; |
1879 | char *p_uq; | |
1880 | const char *endp; | |
1881 | ||
1882 | p_uq = unquote_c_style(p, &endp); | |
1883 | if (p_uq) { | |
1884 | if (*endp) | |
1885 | die("Garbage after path in: %s", command_buf.buf); | |
1886 | p = p_uq; | |
1887 | } | |
f39a946a | 1888 | tree_content_remove(&b->branch_tree, p, NULL); |
e7d06a4b | 1889 | free(p_uq); |
6bb5b329 SP |
1890 | } |
1891 | ||
b6f3481b | 1892 | static void file_change_cr(struct branch *b, int rename) |
f39a946a SP |
1893 | { |
1894 | const char *s, *d; | |
1895 | char *s_uq, *d_uq; | |
1896 | const char *endp; | |
1897 | struct tree_entry leaf; | |
1898 | ||
1899 | s = command_buf.buf + 2; | |
1900 | s_uq = unquote_c_style(s, &endp); | |
1901 | if (s_uq) { | |
1902 | if (*endp != ' ') | |
1903 | die("Missing space after source: %s", command_buf.buf); | |
1904 | } | |
1905 | else { | |
1906 | endp = strchr(s, ' '); | |
1907 | if (!endp) | |
1908 | die("Missing space after source: %s", command_buf.buf); | |
1909 | s_uq = xmalloc(endp - s + 1); | |
1910 | memcpy(s_uq, s, endp - s); | |
1911 | s_uq[endp - s] = 0; | |
1912 | } | |
1913 | s = s_uq; | |
1914 | ||
1915 | endp++; | |
1916 | if (!*endp) | |
1917 | die("Missing dest: %s", command_buf.buf); | |
1918 | ||
1919 | d = endp; | |
1920 | d_uq = unquote_c_style(d, &endp); | |
1921 | if (d_uq) { | |
1922 | if (*endp) | |
1923 | die("Garbage after dest in: %s", command_buf.buf); | |
1924 | d = d_uq; | |
1925 | } | |
1926 | ||
1927 | memset(&leaf, 0, sizeof(leaf)); | |
b6f3481b SP |
1928 | if (rename) |
1929 | tree_content_remove(&b->branch_tree, s, &leaf); | |
1930 | else | |
1931 | tree_content_get(&b->branch_tree, s, &leaf); | |
f39a946a SP |
1932 | if (!leaf.versions[1].mode) |
1933 | die("Path %s not in branch", s); | |
1934 | tree_content_set(&b->branch_tree, d, | |
1935 | leaf.versions[1].sha1, | |
1936 | leaf.versions[1].mode, | |
1937 | leaf.tree); | |
1938 | ||
1939 | free(s_uq); | |
1940 | free(d_uq); | |
1941 | } | |
1942 | ||
825769a8 SP |
1943 | static void file_change_deleteall(struct branch *b) |
1944 | { | |
1945 | release_tree_content_recursive(b->branch_tree.tree); | |
1946 | hashclr(b->branch_tree.versions[0].sha1); | |
1947 | hashclr(b->branch_tree.versions[1].sha1); | |
1948 | load_tree(&b->branch_tree); | |
1949 | } | |
1950 | ||
654aaa37 SP |
1951 | static void cmd_from_commit(struct branch *b, char *buf, unsigned long size) |
1952 | { | |
1953 | if (!buf || size < 46) | |
1954 | die("Not a valid commit: %s", sha1_to_hex(b->sha1)); | |
1955 | if (memcmp("tree ", buf, 5) | |
1956 | || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1)) | |
1957 | die("The commit %s is corrupt", sha1_to_hex(b->sha1)); | |
1958 | hashcpy(b->branch_tree.versions[0].sha1, | |
1959 | b->branch_tree.versions[1].sha1); | |
1960 | } | |
1961 | ||
1962 | static void cmd_from_existing(struct branch *b) | |
1963 | { | |
1964 | if (is_null_sha1(b->sha1)) { | |
1965 | hashclr(b->branch_tree.versions[0].sha1); | |
1966 | hashclr(b->branch_tree.versions[1].sha1); | |
1967 | } else { | |
1968 | unsigned long size; | |
1969 | char *buf; | |
1970 | ||
1971 | buf = read_object_with_reference(b->sha1, | |
1972 | commit_type, &size, b->sha1); | |
1973 | cmd_from_commit(b, buf, size); | |
1974 | free(buf); | |
1975 | } | |
1976 | } | |
1977 | ||
1fdb649c | 1978 | static int cmd_from(struct branch *b) |
00e2b884 | 1979 | { |
6c3aac1c | 1980 | const char *from; |
00e2b884 SP |
1981 | struct branch *s; |
1982 | ||
599065a3 | 1983 | if (prefixcmp(command_buf.buf, "from ")) |
1fdb649c | 1984 | return 0; |
00e2b884 | 1985 | |
ea5e370a SP |
1986 | if (b->branch_tree.tree) { |
1987 | release_tree_content_recursive(b->branch_tree.tree); | |
1988 | b->branch_tree.tree = NULL; | |
1989 | } | |
00e2b884 SP |
1990 | |
1991 | from = strchr(command_buf.buf, ' ') + 1; | |
00e2b884 SP |
1992 | s = lookup_branch(from); |
1993 | if (b == s) | |
1994 | die("Can't create a branch from itself: %s", b->name); | |
1995 | else if (s) { | |
4cabf858 | 1996 | unsigned char *t = s->branch_tree.versions[1].sha1; |
445b8599 | 1997 | hashcpy(b->sha1, s->sha1); |
4cabf858 SP |
1998 | hashcpy(b->branch_tree.versions[0].sha1, t); |
1999 | hashcpy(b->branch_tree.versions[1].sha1, t); | |
00e2b884 | 2000 | } else if (*from == ':') { |
0ea9f045 | 2001 | uintmax_t idnum = strtoumax(from + 1, NULL, 10); |
00e2b884 | 2002 | struct object_entry *oe = find_mark(idnum); |
00e2b884 | 2003 | if (oe->type != OBJ_COMMIT) |
3efb1f34 | 2004 | die("Mark :%" PRIuMAX " not a commit", idnum); |
445b8599 | 2005 | hashcpy(b->sha1, oe->sha1); |
aac65ed1 | 2006 | if (oe->pack_id != MAX_PACK_ID) { |
00e2b884 | 2007 | unsigned long size; |
aac65ed1 SP |
2008 | char *buf = gfi_unpack_entry(oe, &size); |
2009 | cmd_from_commit(b, buf, size); | |
00e2b884 | 2010 | free(buf); |
aac65ed1 SP |
2011 | } else |
2012 | cmd_from_existing(b); | |
654aaa37 SP |
2013 | } else if (!get_sha1(from, b->sha1)) |
2014 | cmd_from_existing(b); | |
2015 | else | |
00e2b884 SP |
2016 | die("Invalid ref name or SHA1 expression: %s", from); |
2017 | ||
2018 | read_next_command(); | |
1fdb649c | 2019 | return 1; |
00e2b884 SP |
2020 | } |
2021 | ||
e5b1444b | 2022 | static struct hash_list *cmd_merge(unsigned int *count) |
62b6f483 | 2023 | { |
10e8d688 | 2024 | struct hash_list *list = NULL, *n, *e = e; |
6c3aac1c | 2025 | const char *from; |
62b6f483 SP |
2026 | struct branch *s; |
2027 | ||
2028 | *count = 0; | |
599065a3 | 2029 | while (!prefixcmp(command_buf.buf, "merge ")) { |
62b6f483 | 2030 | from = strchr(command_buf.buf, ' ') + 1; |
62b6f483 SP |
2031 | n = xmalloc(sizeof(*n)); |
2032 | s = lookup_branch(from); | |
2033 | if (s) | |
2034 | hashcpy(n->sha1, s->sha1); | |
2035 | else if (*from == ':') { | |
0ea9f045 | 2036 | uintmax_t idnum = strtoumax(from + 1, NULL, 10); |
62b6f483 SP |
2037 | struct object_entry *oe = find_mark(idnum); |
2038 | if (oe->type != OBJ_COMMIT) | |
3efb1f34 | 2039 | die("Mark :%" PRIuMAX " not a commit", idnum); |
62b6f483 | 2040 | hashcpy(n->sha1, oe->sha1); |
2f6dc35d SP |
2041 | } else if (!get_sha1(from, n->sha1)) { |
2042 | unsigned long size; | |
2043 | char *buf = read_object_with_reference(n->sha1, | |
6b4318e6 | 2044 | commit_type, &size, n->sha1); |
2f6dc35d SP |
2045 | if (!buf || size < 46) |
2046 | die("Not a valid commit: %s", from); | |
2047 | free(buf); | |
2048 | } else | |
62b6f483 SP |
2049 | die("Invalid ref name or SHA1 expression: %s", from); |
2050 | ||
2051 | n->next = NULL; | |
2052 | if (list) | |
2053 | e->next = n; | |
2054 | else | |
2055 | list = n; | |
2056 | e = n; | |
10e8d688 | 2057 | (*count)++; |
62b6f483 SP |
2058 | read_next_command(); |
2059 | } | |
2060 | return list; | |
2061 | } | |
2062 | ||
fd99224e | 2063 | static void cmd_new_commit(void) |
6bb5b329 | 2064 | { |
c44cdc7e SP |
2065 | struct branch *b; |
2066 | void *msg; | |
2067 | size_t msglen; | |
c44cdc7e SP |
2068 | char *sp; |
2069 | char *author = NULL; | |
2070 | char *committer = NULL; | |
62b6f483 SP |
2071 | struct hash_list *merge_list = NULL; |
2072 | unsigned int merge_count; | |
c44cdc7e SP |
2073 | |
2074 | /* Obtain the branch name from the rest of our command */ | |
2075 | sp = strchr(command_buf.buf, ' ') + 1; | |
c44cdc7e | 2076 | b = lookup_branch(sp); |
463acbe1 | 2077 | if (!b) |
00e2b884 | 2078 | b = new_branch(sp); |
c44cdc7e SP |
2079 | |
2080 | read_next_command(); | |
2081 | cmd_mark(); | |
599065a3 | 2082 | if (!prefixcmp(command_buf.buf, "author ")) { |
63e0c8b3 | 2083 | author = parse_ident(command_buf.buf + 7); |
c44cdc7e SP |
2084 | read_next_command(); |
2085 | } | |
599065a3 | 2086 | if (!prefixcmp(command_buf.buf, "committer ")) { |
63e0c8b3 | 2087 | committer = parse_ident(command_buf.buf + 10); |
c44cdc7e SP |
2088 | read_next_command(); |
2089 | } | |
2090 | if (!committer) | |
2091 | die("Expected committer but didn't get one"); | |
2092 | msg = cmd_data(&msglen); | |
02f3389d SP |
2093 | read_next_command(); |
2094 | cmd_from(b); | |
62b6f483 | 2095 | merge_list = cmd_merge(&merge_count); |
c44cdc7e SP |
2096 | |
2097 | /* ensure the branch is active/loaded */ | |
41e5257f | 2098 | if (!b->branch_tree.tree || !max_active_branches) { |
463acbe1 SP |
2099 | unload_one_branch(); |
2100 | load_branch(b); | |
2101 | } | |
6bb5b329 | 2102 | |
463acbe1 | 2103 | /* file_change* */ |
e6c019d0 | 2104 | while (command_buf.len > 0) { |
1fdb649c | 2105 | if (!prefixcmp(command_buf.buf, "M ")) |
463acbe1 | 2106 | file_change_m(b); |
599065a3 | 2107 | else if (!prefixcmp(command_buf.buf, "D ")) |
463acbe1 | 2108 | file_change_d(b); |
f39a946a | 2109 | else if (!prefixcmp(command_buf.buf, "R ")) |
b6f3481b SP |
2110 | file_change_cr(b, 1); |
2111 | else if (!prefixcmp(command_buf.buf, "C ")) | |
2112 | file_change_cr(b, 0); | |
825769a8 SP |
2113 | else if (!strcmp("deleteall", command_buf.buf)) |
2114 | file_change_deleteall(b); | |
1fdb649c SP |
2115 | else { |
2116 | unread_command_buf = 1; | |
2117 | break; | |
2118 | } | |
e6c019d0 PH |
2119 | if (read_next_command() == EOF) |
2120 | break; | |
6bb5b329 | 2121 | } |
6bb5b329 | 2122 | |
c44cdc7e | 2123 | /* build the tree and the commit */ |
463acbe1 | 2124 | store_tree(&b->branch_tree); |
8a8c55ea SP |
2125 | hashcpy(b->branch_tree.versions[0].sha1, |
2126 | b->branch_tree.versions[1].sha1); | |
63e0c8b3 | 2127 | size_dbuf(&new_data, 114 + msglen |
62b6f483 | 2128 | + merge_count * 49 |
c44cdc7e SP |
2129 | + (author |
2130 | ? strlen(author) + strlen(committer) | |
2131 | : 2 * strlen(committer))); | |
243f801d | 2132 | sp = new_data.buffer; |
4cabf858 SP |
2133 | sp += sprintf(sp, "tree %s\n", |
2134 | sha1_to_hex(b->branch_tree.versions[1].sha1)); | |
445b8599 | 2135 | if (!is_null_sha1(b->sha1)) |
c44cdc7e | 2136 | sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1)); |
62b6f483 SP |
2137 | while (merge_list) { |
2138 | struct hash_list *next = merge_list->next; | |
2139 | sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1)); | |
2140 | free(merge_list); | |
2141 | merge_list = next; | |
2142 | } | |
63e0c8b3 SP |
2143 | sp += sprintf(sp, "author %s\n", author ? author : committer); |
2144 | sp += sprintf(sp, "committer %s\n", committer); | |
2145 | *sp++ = '\n'; | |
c44cdc7e SP |
2146 | memcpy(sp, msg, msglen); |
2147 | sp += msglen; | |
e7d06a4b | 2148 | free(author); |
c44cdc7e SP |
2149 | free(committer); |
2150 | free(msg); | |
2151 | ||
69e74e74 | 2152 | if (!store_object(OBJ_COMMIT, |
243f801d | 2153 | new_data.buffer, sp - (char*)new_data.buffer, |
69e74e74 SP |
2154 | NULL, b->sha1, next_mark)) |
2155 | b->pack_id = pack_id; | |
463acbe1 | 2156 | b->last_commit = object_count_by_type[OBJ_COMMIT]; |
6bb5b329 SP |
2157 | } |
2158 | ||
fd99224e | 2159 | static void cmd_new_tag(void) |
72303d44 | 2160 | { |
72303d44 SP |
2161 | char *sp; |
2162 | const char *from; | |
2163 | char *tagger; | |
2164 | struct branch *s; | |
2165 | void *msg; | |
2166 | size_t msglen; | |
72303d44 | 2167 | struct tag *t; |
0ea9f045 | 2168 | uintmax_t from_mark = 0; |
72303d44 SP |
2169 | unsigned char sha1[20]; |
2170 | ||
2171 | /* Obtain the new tag name from the rest of our command */ | |
2172 | sp = strchr(command_buf.buf, ' ') + 1; | |
72303d44 SP |
2173 | t = pool_alloc(sizeof(struct tag)); |
2174 | t->next_tag = NULL; | |
2175 | t->name = pool_strdup(sp); | |
2176 | if (last_tag) | |
2177 | last_tag->next_tag = t; | |
2178 | else | |
2179 | first_tag = t; | |
2180 | last_tag = t; | |
72303d44 SP |
2181 | read_next_command(); |
2182 | ||
2183 | /* from ... */ | |
599065a3 | 2184 | if (prefixcmp(command_buf.buf, "from ")) |
72303d44 | 2185 | die("Expected from command, got %s", command_buf.buf); |
72303d44 | 2186 | from = strchr(command_buf.buf, ' ') + 1; |
72303d44 SP |
2187 | s = lookup_branch(from); |
2188 | if (s) { | |
445b8599 | 2189 | hashcpy(sha1, s->sha1); |
72303d44 | 2190 | } else if (*from == ':') { |
10e8d688 | 2191 | struct object_entry *oe; |
0ea9f045 | 2192 | from_mark = strtoumax(from + 1, NULL, 10); |
10e8d688 | 2193 | oe = find_mark(from_mark); |
72303d44 | 2194 | if (oe->type != OBJ_COMMIT) |
3efb1f34 | 2195 | die("Mark :%" PRIuMAX " not a commit", from_mark); |
445b8599 | 2196 | hashcpy(sha1, oe->sha1); |
72303d44 SP |
2197 | } else if (!get_sha1(from, sha1)) { |
2198 | unsigned long size; | |
2199 | char *buf; | |
2200 | ||
2201 | buf = read_object_with_reference(sha1, | |
df843662 | 2202 | commit_type, &size, sha1); |
72303d44 SP |
2203 | if (!buf || size < 46) |
2204 | die("Not a valid commit: %s", from); | |
2205 | free(buf); | |
2206 | } else | |
2207 | die("Invalid ref name or SHA1 expression: %s", from); | |
72303d44 SP |
2208 | read_next_command(); |
2209 | ||
2210 | /* tagger ... */ | |
599065a3 | 2211 | if (prefixcmp(command_buf.buf, "tagger ")) |
72303d44 | 2212 | die("Expected tagger command, got %s", command_buf.buf); |
63e0c8b3 | 2213 | tagger = parse_ident(command_buf.buf + 7); |
72303d44 SP |
2214 | |
2215 | /* tag payload/message */ | |
2216 | read_next_command(); | |
2217 | msg = cmd_data(&msglen); | |
2218 | ||
2219 | /* build the tag object */ | |
243f801d SP |
2220 | size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen); |
2221 | sp = new_data.buffer; | |
72303d44 | 2222 | sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1)); |
df843662 | 2223 | sp += sprintf(sp, "type %s\n", commit_type); |
72303d44 | 2224 | sp += sprintf(sp, "tag %s\n", t->name); |
63e0c8b3 SP |
2225 | sp += sprintf(sp, "tagger %s\n", tagger); |
2226 | *sp++ = '\n'; | |
72303d44 SP |
2227 | memcpy(sp, msg, msglen); |
2228 | sp += msglen; | |
2229 | free(tagger); | |
2230 | free(msg); | |
2231 | ||
69e74e74 SP |
2232 | if (store_object(OBJ_TAG, new_data.buffer, |
2233 | sp - (char*)new_data.buffer, | |
2234 | NULL, t->sha1, 0)) | |
2235 | t->pack_id = MAX_PACK_ID; | |
2236 | else | |
2237 | t->pack_id = pack_id; | |
72303d44 SP |
2238 | } |
2239 | ||
fd99224e | 2240 | static void cmd_reset_branch(void) |
5fced8dc SP |
2241 | { |
2242 | struct branch *b; | |
5fced8dc SP |
2243 | char *sp; |
2244 | ||
2245 | /* Obtain the branch name from the rest of our command */ | |
2246 | sp = strchr(command_buf.buf, ' ') + 1; | |
5fced8dc SP |
2247 | b = lookup_branch(sp); |
2248 | if (b) { | |
ea5e370a SP |
2249 | hashclr(b->sha1); |
2250 | hashclr(b->branch_tree.versions[0].sha1); | |
2251 | hashclr(b->branch_tree.versions[1].sha1); | |
5fced8dc SP |
2252 | if (b->branch_tree.tree) { |
2253 | release_tree_content_recursive(b->branch_tree.tree); | |
2254 | b->branch_tree.tree = NULL; | |
2255 | } | |
2256 | } | |
9938ffc5 SP |
2257 | else |
2258 | b = new_branch(sp); | |
9938ffc5 | 2259 | read_next_command(); |
b449f4cf | 2260 | if (!cmd_from(b) && command_buf.len > 0) |
1fdb649c | 2261 | unread_command_buf = 1; |
5fced8dc SP |
2262 | } |
2263 | ||
fd99224e | 2264 | static void cmd_checkpoint(void) |
7bfe6e26 | 2265 | { |
820b9310 SP |
2266 | if (object_count) { |
2267 | cycle_packfile(); | |
2268 | dump_branches(); | |
2269 | dump_tags(); | |
2270 | dump_marks(); | |
2271 | } | |
1fdb649c | 2272 | skip_optional_lf(); |
7bfe6e26 SP |
2273 | } |
2274 | ||
ac053c02 SP |
2275 | static void cmd_progress(void) |
2276 | { | |
b449f4cf | 2277 | fwrite(command_buf.buf, 1, command_buf.len, stdout); |
ac053c02 SP |
2278 | fputc('\n', stdout); |
2279 | fflush(stdout); | |
2280 | skip_optional_lf(); | |
2281 | } | |
2282 | ||
e8438420 SP |
2283 | static void import_marks(const char *input_file) |
2284 | { | |
2285 | char line[512]; | |
2286 | FILE *f = fopen(input_file, "r"); | |
2287 | if (!f) | |
2288 | die("cannot read %s: %s", input_file, strerror(errno)); | |
2289 | while (fgets(line, sizeof(line), f)) { | |
2290 | uintmax_t mark; | |
2291 | char *end; | |
2292 | unsigned char sha1[20]; | |
2293 | struct object_entry *e; | |
2294 | ||
2295 | end = strchr(line, '\n'); | |
2296 | if (line[0] != ':' || !end) | |
2297 | die("corrupt mark line: %s", line); | |
2298 | *end = 0; | |
2299 | mark = strtoumax(line + 1, &end, 10); | |
2300 | if (!mark || end == line + 1 | |
2301 | || *end != ' ' || get_sha1(end + 1, sha1)) | |
2302 | die("corrupt mark line: %s", line); | |
2303 | e = find_object(sha1); | |
2304 | if (!e) { | |
2305 | enum object_type type = sha1_object_info(sha1, NULL); | |
2306 | if (type < 0) | |
2307 | die("object not found: %s", sha1_to_hex(sha1)); | |
2308 | e = insert_object(sha1); | |
2309 | e->type = type; | |
2310 | e->pack_id = MAX_PACK_ID; | |
a5c1780a | 2311 | e->offset = 1; /* just not zero! */ |
e8438420 SP |
2312 | } |
2313 | insert_mark(mark, e); | |
2314 | } | |
2315 | fclose(f); | |
2316 | } | |
2317 | ||
d5c57b28 | 2318 | static const char fast_import_usage[] = |
63e0c8b3 | 2319 | "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]"; |
d5c57b28 | 2320 | |
8bcce301 SP |
2321 | int main(int argc, const char **argv) |
2322 | { | |
904b1941 | 2323 | unsigned int i, show_stats = 1; |
8bcce301 | 2324 | |
463acbe1 | 2325 | git_config(git_default_config); |
93e72d8d | 2326 | alloc_objects(object_entry_alloc); |
f1696ee3 | 2327 | strbuf_init(&command_buf, 0); |
93e72d8d SP |
2328 | atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*)); |
2329 | branch_table = xcalloc(branch_table_sz, sizeof(struct branch*)); | |
2330 | avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*)); | |
2331 | marks = pool_calloc(1, sizeof(struct mark_set)); | |
463acbe1 | 2332 | |
d5c57b28 SP |
2333 | for (i = 1; i < argc; i++) { |
2334 | const char *a = argv[i]; | |
2335 | ||
2336 | if (*a != '-' || !strcmp(a, "--")) | |
2337 | break; | |
cc44c765 | 2338 | else if (!prefixcmp(a, "--date-format=")) { |
63e0c8b3 SP |
2339 | const char *fmt = a + 14; |
2340 | if (!strcmp(fmt, "raw")) | |
2341 | whenspec = WHENSPEC_RAW; | |
2342 | else if (!strcmp(fmt, "rfc2822")) | |
2343 | whenspec = WHENSPEC_RFC2822; | |
2344 | else if (!strcmp(fmt, "now")) | |
2345 | whenspec = WHENSPEC_NOW; | |
2346 | else | |
2347 | die("unknown --date-format argument %s", fmt); | |
2348 | } | |
cc44c765 | 2349 | else if (!prefixcmp(a, "--max-pack-size=")) |
0ea9f045 | 2350 | max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024; |
cc44c765 | 2351 | else if (!prefixcmp(a, "--depth=")) |
d5c57b28 | 2352 | max_depth = strtoul(a + 8, NULL, 0); |
cc44c765 | 2353 | else if (!prefixcmp(a, "--active-branches=")) |
d5c57b28 | 2354 | max_active_branches = strtoul(a + 18, NULL, 0); |
e8438420 SP |
2355 | else if (!prefixcmp(a, "--import-marks=")) |
2356 | import_marks(a + 15); | |
cc44c765 | 2357 | else if (!prefixcmp(a, "--export-marks=")) |
a6a1a831 | 2358 | mark_file = a + 15; |
cc44c765 | 2359 | else if (!prefixcmp(a, "--export-pack-edges=")) { |
bdf1c06d SP |
2360 | if (pack_edges) |
2361 | fclose(pack_edges); | |
2362 | pack_edges = fopen(a + 20, "a"); | |
2363 | if (!pack_edges) | |
2364 | die("Cannot open %s: %s", a + 20, strerror(errno)); | |
2365 | } else if (!strcmp(a, "--force")) | |
7073e69e | 2366 | force_update = 1; |
c499d768 SP |
2367 | else if (!strcmp(a, "--quiet")) |
2368 | show_stats = 0; | |
2369 | else if (!strcmp(a, "--stats")) | |
2370 | show_stats = 1; | |
d5c57b28 SP |
2371 | else |
2372 | die("unknown option %s", a); | |
2373 | } | |
8455e484 | 2374 | if (i != argc) |
d5c57b28 | 2375 | usage(fast_import_usage); |
d5c57b28 | 2376 | |
904b1941 SP |
2377 | rc_free = pool_alloc(cmd_save * sizeof(*rc_free)); |
2378 | for (i = 0; i < (cmd_save - 1); i++) | |
2379 | rc_free[i].next = &rc_free[i + 1]; | |
2380 | rc_free[cmd_save - 1].next = NULL; | |
2381 | ||
a5c1780a | 2382 | prepare_packed_git(); |
f70b6534 | 2383 | start_packfile(); |
8acb3297 | 2384 | set_die_routine(die_nicely); |
e6c019d0 PH |
2385 | while (read_next_command() != EOF) { |
2386 | if (!strcmp("blob", command_buf.buf)) | |
c44cdc7e | 2387 | cmd_new_blob(); |
599065a3 | 2388 | else if (!prefixcmp(command_buf.buf, "commit ")) |
c44cdc7e | 2389 | cmd_new_commit(); |
599065a3 | 2390 | else if (!prefixcmp(command_buf.buf, "tag ")) |
72303d44 | 2391 | cmd_new_tag(); |
599065a3 | 2392 | else if (!prefixcmp(command_buf.buf, "reset ")) |
5fced8dc | 2393 | cmd_reset_branch(); |
7bfe6e26 SP |
2394 | else if (!strcmp("checkpoint", command_buf.buf)) |
2395 | cmd_checkpoint(); | |
ac053c02 SP |
2396 | else if (!prefixcmp(command_buf.buf, "progress ")) |
2397 | cmd_progress(); | |
c44cdc7e SP |
2398 | else |
2399 | die("Unsupported command: %s", command_buf.buf); | |
db5e523f | 2400 | } |
f70b6534 | 2401 | end_packfile(); |
c44cdc7e | 2402 | |
463acbe1 | 2403 | dump_branches(); |
72303d44 | 2404 | dump_tags(); |
8455e484 | 2405 | unkeep_all_packs(); |
a6a1a831 | 2406 | dump_marks(); |
8bcce301 | 2407 | |
bdf1c06d SP |
2408 | if (pack_edges) |
2409 | fclose(pack_edges); | |
2410 | ||
c499d768 SP |
2411 | if (show_stats) { |
2412 | uintmax_t total_count = 0, duplicate_count = 0; | |
2413 | for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++) | |
2414 | total_count += object_count_by_type[i]; | |
2415 | for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++) | |
2416 | duplicate_count += duplicate_count_by_type[i]; | |
2417 | ||
2418 | fprintf(stderr, "%s statistics:\n", argv[0]); | |
2419 | fprintf(stderr, "---------------------------------------------------------------------\n"); | |
3efb1f34 JR |
2420 | fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count); |
2421 | fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count); | |
2422 | fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]); | |
2423 | fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]); | |
2424 | fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]); | |
2425 | fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]); | |
c499d768 | 2426 | fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count); |
3efb1f34 | 2427 | fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count); |
c499d768 | 2428 | fprintf(stderr, " atoms: %10u\n", atom_cnt); |
3efb1f34 | 2429 | fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024); |
40db58b8 | 2430 | fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024)); |
3efb1f34 | 2431 | fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024); |
c499d768 SP |
2432 | fprintf(stderr, "---------------------------------------------------------------------\n"); |
2433 | pack_report(); | |
2434 | fprintf(stderr, "---------------------------------------------------------------------\n"); | |
2435 | fprintf(stderr, "\n"); | |
2436 | } | |
db5e523f | 2437 | |
7073e69e | 2438 | return failure ? 1 : 0; |
db5e523f | 2439 | } |