Coverage Report

Created: 2023-11-19 07:08

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