~ubuntu-branches/ubuntu/intrepid/git-core/intrepid-security

« back to all changes in this revision

Viewing changes to fast-import.c

  • Committer: Package Import Robot
  • Author(s): Gerrit Pape
  • Date: 2007-10-04 08:27:01 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20071004082701-rsd058ontoqz4i30
Tags: 1:1.5.3.4-1
new upstream point release (closes: #445188).

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
        | new_tag
9
9
        | reset_branch
10
10
        | checkpoint
 
11
        | progress
11
12
        ;
12
13
 
13
14
  new_blob ::= 'blob' lf
14
 
        mark?
 
15
    mark?
15
16
    file_content;
16
17
  file_content ::= data;
17
18
 
23
24
    ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
24
25
    ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
25
26
    file_change*
26
 
    lf;
 
27
    lf?;
27
28
  commit_msg ::= data;
28
29
 
29
 
  file_change ::= file_clr | file_del | file_obm | file_inm;
 
30
  file_change ::= file_clr
 
31
    | file_del
 
32
    | file_rnm
 
33
    | file_cpy
 
34
    | file_obm
 
35
    | file_inm;
30
36
  file_clr ::= 'deleteall' lf;
31
37
  file_del ::= 'D' sp path_str lf;
 
38
  file_rnm ::= 'R' sp path_str sp path_str lf;
 
39
  file_cpy ::= 'C' sp path_str sp path_str lf;
32
40
  file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
33
41
  file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
34
42
    data;
35
43
 
36
44
  new_tag ::= 'tag' sp tag_str lf
37
45
    'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
38
 
        'tagger' sp name '<' email '>' when lf
 
46
    'tagger' sp name '<' email '>' when lf
39
47
    tag_msg;
40
48
  tag_msg ::= data;
41
49
 
42
50
  reset_branch ::= 'reset' sp ref_str lf
43
51
    ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
44
 
    lf;
 
52
    lf?;
45
53
 
46
54
  checkpoint ::= 'checkpoint' lf
47
 
    lf;
 
55
    lf?;
 
56
 
 
57
  progress ::= 'progress' sp not_lf* lf
 
58
    lf?;
48
59
 
49
60
     # note: the first idnum in a stream should be 1 and subsequent
50
61
     # idnums should not have gaps between values as this will cause
51
62
     # the stream parser to reserve space for the gapped values.  An
52
 
         # idnum can be updated in the future to a new object by issuing
 
63
     # idnum can be updated in the future to a new object by issuing
53
64
     # a new mark directive with the old idnum.
54
 
         #
 
65
     #
55
66
  mark ::= 'mark' sp idnum lf;
56
67
  data ::= (delimited_data | exact_data)
57
 
    lf;
 
68
    lf?;
58
69
 
59
70
    # note: delim may be any string but must not contain lf.
60
71
    # data_line may contain any data but must not be exactly
61
72
    # delim.
62
73
  delimited_data ::= 'data' sp '<<' delim lf
63
74
    (data_line lf)*
64
 
        delim lf;
 
75
    delim lf;
65
76
 
66
77
     # note: declen indicates the length of binary_data in bytes.
67
78
     # declen does not include the lf preceeding the binary data.
71
82
 
72
83
     # note: quoted strings are C-style quoting supporting \c for
73
84
     # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
74
 
         # is the signed byte value in octal.  Note that the only
 
85
     # is the signed byte value in octal.  Note that the only
75
86
     # characters which must actually be escaped to protect the
76
87
     # stream formatting is: \, " and LF.  Otherwise these values
77
 
         # are UTF8.
 
88
     # are UTF8.
78
89
     #
79
90
  ref_str     ::= ref;
80
91
  sha1exp_str ::= sha1exp;
97
108
  lf ::= # ASCII newline (LF) character;
98
109
 
99
110
     # note: a colon (':') must precede the numerical value assigned to
100
 
         # an idnum.  This is to distinguish it from a ref or tag name as
 
111
     # an idnum.  This is to distinguish it from a ref or tag name as
101
112
     # GIT does not permit ':' in ref or tag strings.
102
 
         #
 
113
     #
103
114
  idnum   ::= ':' bigint;
104
115
  path    ::= # GIT style file path, e.g. "a/b/c";
105
116
  ref     ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
108
119
  hexsha1 ::= # SHA1 in hexadecimal format;
109
120
 
110
121
     # note: name and email are UTF8 strings, however name must not
111
 
         # contain '<' or lf and email must not contain any of the
 
122
     # contain '<' or lf and email must not contain any of the
112
123
     # following: '<', '>', lf.
113
 
         #
 
124
     #
114
125
  name  ::= # valid GIT author/committer name;
115
126
  email ::= # valid GIT author/committer email;
116
127
  ts    ::= # time since the epoch in seconds, ascii base10 notation;
117
128
  tz    ::= # GIT style timezone;
 
129
 
 
130
     # note: comments may appear anywhere in the input, except
 
131
     # within a data command.  Any form of the data command
 
132
     # always escapes the related input from comment processing.
 
133
     #
 
134
     # In case it is not clear, the '#' that starts the comment
 
135
     # must be the first character on that the line (an lf have
 
136
     # preceeded it).
 
137
     #
 
138
  comment ::= '#' not_lf* lf;
 
139
  not_lf  ::= # Any byte that is not ASCII newline (LF);
118
140
*/
119
141
 
120
142
#include "builtin.h"
247
269
        WHENSPEC_NOW,
248
270
} whenspec_type;
249
271
 
 
272
struct recent_command
 
273
{
 
274
        struct recent_command *prev;
 
275
        struct recent_command *next;
 
276
        char *buf;
 
277
};
 
278
 
250
279
/* Configured limits on output */
251
280
static unsigned long max_depth = 10;
252
281
static off_t max_packsize = (1LL << 32) - 1;
312
341
/* Input stream parsing */
313
342
static whenspec_type whenspec = WHENSPEC_RAW;
314
343
static struct strbuf command_buf;
 
344
static int unread_command_buf;
 
345
static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
 
346
static struct recent_command *cmd_tail = &cmd_hist;
 
347
static struct recent_command *rc_free;
 
348
static unsigned int cmd_save = 100;
315
349
static uintmax_t next_mark;
316
350
static struct dbuf new_data;
317
351
 
 
352
static void write_branch_report(FILE *rpt, struct branch *b)
 
353
{
 
354
        fprintf(rpt, "%s:\n", b->name);
 
355
 
 
356
        fprintf(rpt, "  status      :");
 
357
        if (b->active)
 
358
                fputs(" active", rpt);
 
359
        if (b->branch_tree.tree)
 
360
                fputs(" loaded", rpt);
 
361
        if (is_null_sha1(b->branch_tree.versions[1].sha1))
 
362
                fputs(" dirty", rpt);
 
363
        fputc('\n', rpt);
 
364
 
 
365
        fprintf(rpt, "  tip commit  : %s\n", sha1_to_hex(b->sha1));
 
366
        fprintf(rpt, "  old tree    : %s\n", sha1_to_hex(b->branch_tree.versions[0].sha1));
 
367
        fprintf(rpt, "  cur tree    : %s\n", sha1_to_hex(b->branch_tree.versions[1].sha1));
 
368
        fprintf(rpt, "  commit clock: %" PRIuMAX "\n", b->last_commit);
 
369
 
 
370
        fputs("  last pack   : ", rpt);
 
371
        if (b->pack_id < MAX_PACK_ID)
 
372
                fprintf(rpt, "%u", b->pack_id);
 
373
        fputc('\n', rpt);
 
374
 
 
375
        fputc('\n', rpt);
 
376
}
 
377
 
 
378
static void write_crash_report(const char *err)
 
379
{
 
380
        char *loc = git_path("fast_import_crash_%d", getpid());
 
381
        FILE *rpt = fopen(loc, "w");
 
382
        struct branch *b;
 
383
        unsigned long lu;
 
384
        struct recent_command *rc;
 
385
 
 
386
        if (!rpt) {
 
387
                error("can't write crash report %s: %s", loc, strerror(errno));
 
388
                return;
 
389
        }
 
390
 
 
391
        fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
 
392
 
 
393
        fprintf(rpt, "fast-import crash report:\n");
 
394
        fprintf(rpt, "    fast-import process: %d\n", getpid());
 
395
        fprintf(rpt, "    parent process     : %d\n", getppid());
 
396
        fprintf(rpt, "    at %s\n", show_date(time(NULL), 0, DATE_LOCAL));
 
397
        fputc('\n', rpt);
 
398
 
 
399
        fputs("fatal: ", rpt);
 
400
        fputs(err, rpt);
 
401
        fputc('\n', rpt);
 
402
 
 
403
        fputc('\n', rpt);
 
404
        fputs("Most Recent Commands Before Crash\n", rpt);
 
405
        fputs("---------------------------------\n", rpt);
 
406
        for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
 
407
                if (rc->next == &cmd_hist)
 
408
                        fputs("* ", rpt);
 
409
                else
 
410
                        fputs("  ", rpt);
 
411
                fputs(rc->buf, rpt);
 
412
                fputc('\n', rpt);
 
413
        }
 
414
 
 
415
        fputc('\n', rpt);
 
416
        fputs("Active Branch LRU\n", rpt);
 
417
        fputs("-----------------\n", rpt);
 
418
        fprintf(rpt, "    active_branches = %lu cur, %lu max\n",
 
419
                cur_active_branches,
 
420
                max_active_branches);
 
421
        fputc('\n', rpt);
 
422
        fputs("  pos  clock name\n", rpt);
 
423
        fputs("  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
 
424
        for (b = active_branches, lu = 0; b; b = b->active_next_branch)
 
425
                fprintf(rpt, "  %2lu) %6" PRIuMAX" %s\n",
 
426
                        ++lu, b->last_commit, b->name);
 
427
 
 
428
        fputc('\n', rpt);
 
429
        fputs("Inactive Branches\n", rpt);
 
430
        fputs("-----------------\n", rpt);
 
431
        for (lu = 0; lu < branch_table_sz; lu++) {
 
432
                for (b = branch_table[lu]; b; b = b->table_next_branch)
 
433
                        write_branch_report(rpt, b);
 
434
        }
 
435
 
 
436
        fputc('\n', rpt);
 
437
        fputs("-------------------\n", rpt);
 
438
        fputs("END OF CRASH REPORT\n", rpt);
 
439
        fclose(rpt);
 
440
}
 
441
 
 
442
static NORETURN void die_nicely(const char *err, va_list params)
 
443
{
 
444
        static int zombie;
 
445
        char message[2 * PATH_MAX];
 
446
 
 
447
        vsnprintf(message, sizeof(message), err, params);
 
448
        fputs("fatal: ", stderr);
 
449
        fputs(message, stderr);
 
450
        fputc('\n', stderr);
 
451
 
 
452
        if (!zombie) {
 
453
                zombie = 1;
 
454
                write_crash_report(message);
 
455
        }
 
456
        exit(128);
 
457
}
318
458
 
319
459
static void alloc_objects(unsigned int cnt)
320
460
{
517
657
 
518
658
        if (b)
519
659
                die("Invalid attempt to create duplicate branch: %s", name);
520
 
        if (check_ref_format(name))
 
660
        switch (check_ref_format(name)) {
 
661
        case  0: break; /* its valid */
 
662
        case -2: break; /* valid, but too few '/', allow anyway */
 
663
        default:
521
664
                die("Branch name doesn't conform to GIT standards: %s", name);
 
665
        }
522
666
 
523
667
        b = pool_calloc(1, sizeof(struct branch));
524
668
        b->name = pool_strdup(name);
622
766
        avail_tree_entry = e;
623
767
}
624
768
 
 
769
static struct tree_content *dup_tree_content(struct tree_content *s)
 
770
{
 
771
        struct tree_content *d;
 
772
        struct tree_entry *a, *b;
 
773
        unsigned int i;
 
774
 
 
775
        if (!s)
 
776
                return NULL;
 
777
        d = new_tree_content(s->entry_count);
 
778
        for (i = 0; i < s->entry_count; i++) {
 
779
                a = s->entries[i];
 
780
                b = new_tree_entry();
 
781
                memcpy(b, a, sizeof(*a));
 
782
                if (a->tree && is_null_sha1(b->versions[1].sha1))
 
783
                        b->tree = dup_tree_content(a->tree);
 
784
                else
 
785
                        b->tree = NULL;
 
786
                d->entries[i] = b;
 
787
        }
 
788
        d->entry_count = s->entry_count;
 
789
        d->delta_depth = s->delta_depth;
 
790
 
 
791
        return d;
 
792
}
 
793
 
625
794
static void start_packfile(void)
626
795
{
627
796
        static char tmpfile[PATH_MAX];
631
800
 
632
801
        snprintf(tmpfile, sizeof(tmpfile),
633
802
                "%s/tmp_pack_XXXXXX", get_object_directory());
634
 
        pack_fd = mkstemp(tmpfile);
635
 
        if (pack_fd < 0)
636
 
                die("Can't create %s: %s", tmpfile, strerror(errno));
 
803
        pack_fd = xmkstemp(tmpfile);
637
804
        p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
638
805
        strcpy(p->pack_name, tmpfile);
639
806
        p->pack_fd = pack_fd;
695
862
 
696
863
        snprintf(tmpfile, sizeof(tmpfile),
697
864
                "%s/tmp_idx_XXXXXX", get_object_directory());
698
 
        idx_fd = mkstemp(tmpfile);
699
 
        if (idx_fd < 0)
700
 
                die("Can't create %s: %s", tmpfile, strerror(errno));
 
865
        idx_fd = xmkstemp(tmpfile);
701
866
        f = sha1fd(idx_fd, tmpfile);
702
867
        sha1write(f, array, 256 * sizeof(int));
703
868
        SHA1_Init(&ctx);
1154
1319
        struct tree_entry *root,
1155
1320
        const char *p,
1156
1321
        const unsigned char *sha1,
1157
 
        const uint16_t mode)
 
1322
        const uint16_t mode,
 
1323
        struct tree_content *subtree)
1158
1324
{
1159
1325
        struct tree_content *t = root->tree;
1160
1326
        const char *slash1;
1168
1334
                n = strlen(p);
1169
1335
        if (!n)
1170
1336
                die("Empty path component found in input");
 
1337
        if (!slash1 && !S_ISDIR(mode) && subtree)
 
1338
                die("Non-directories cannot have subtrees");
1171
1339
 
1172
1340
        for (i = 0; i < t->entry_count; i++) {
1173
1341
                e = t->entries[i];
1174
1342
                if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1175
1343
                        if (!slash1) {
1176
 
                                if (e->versions[1].mode == mode
 
1344
                                if (!S_ISDIR(mode)
 
1345
                                                && e->versions[1].mode == mode
1177
1346
                                                && !hashcmp(e->versions[1].sha1, sha1))
1178
1347
                                        return 0;
1179
1348
                                e->versions[1].mode = mode;
1180
1349
                                hashcpy(e->versions[1].sha1, sha1);
1181
 
                                if (e->tree) {
 
1350
                                if (e->tree)
1182
1351
                                        release_tree_content_recursive(e->tree);
1183
 
                                        e->tree = NULL;
1184
 
                                }
 
1352
                                e->tree = subtree;
1185
1353
                                hashclr(root->versions[1].sha1);
1186
1354
                                return 1;
1187
1355
                        }
1191
1359
                        }
1192
1360
                        if (!e->tree)
1193
1361
                                load_tree(e);
1194
 
                        if (tree_content_set(e, slash1 + 1, sha1, mode)) {
 
1362
                        if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
1195
1363
                                hashclr(root->versions[1].sha1);
1196
1364
                                return 1;
1197
1365
                        }
1209
1377
        if (slash1) {
1210
1378
                e->tree = new_tree_content(8);
1211
1379
                e->versions[1].mode = S_IFDIR;
1212
 
                tree_content_set(e, slash1 + 1, sha1, mode);
 
1380
                tree_content_set(e, slash1 + 1, sha1, mode, subtree);
1213
1381
        } else {
1214
 
                e->tree = NULL;
 
1382
                e->tree = subtree;
1215
1383
                e->versions[1].mode = mode;
1216
1384
                hashcpy(e->versions[1].sha1, sha1);
1217
1385
        }
1219
1387
        return 1;
1220
1388
}
1221
1389
 
1222
 
static int tree_content_remove(struct tree_entry *root, const char *p)
 
1390
static int tree_content_remove(
 
1391
        struct tree_entry *root,
 
1392
        const char *p,
 
1393
        struct tree_entry *backup_leaf)
1223
1394
{
1224
1395
        struct tree_content *t = root->tree;
1225
1396
        const char *slash1;
1239
1410
                                goto del_entry;
1240
1411
                        if (!e->tree)
1241
1412
                                load_tree(e);
1242
 
                        if (tree_content_remove(e, slash1 + 1)) {
 
1413
                        if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
1243
1414
                                for (n = 0; n < e->tree->entry_count; n++) {
1244
1415
                                        if (e->tree->entries[n]->versions[1].mode) {
1245
1416
                                                hashclr(root->versions[1].sha1);
1246
1417
                                                return 1;
1247
1418
                                        }
1248
1419
                                }
 
1420
                                backup_leaf = NULL;
1249
1421
                                goto del_entry;
1250
1422
                        }
1251
1423
                        return 0;
1254
1426
        return 0;
1255
1427
 
1256
1428
del_entry:
1257
 
        if (e->tree) {
 
1429
        if (backup_leaf)
 
1430
                memcpy(backup_leaf, e, sizeof(*backup_leaf));
 
1431
        else if (e->tree)
1258
1432
                release_tree_content_recursive(e->tree);
1259
 
                e->tree = NULL;
1260
 
        }
 
1433
        e->tree = NULL;
1261
1434
        e->versions[1].mode = 0;
1262
1435
        hashclr(e->versions[1].sha1);
1263
1436
        hashclr(root->versions[1].sha1);
1264
1437
        return 1;
1265
1438
}
1266
1439
 
 
1440
static int tree_content_get(
 
1441
        struct tree_entry *root,
 
1442
        const char *p,
 
1443
        struct tree_entry *leaf)
 
1444
{
 
1445
        struct tree_content *t = root->tree;
 
1446
        const char *slash1;
 
1447
        unsigned int i, n;
 
1448
        struct tree_entry *e;
 
1449
 
 
1450
        slash1 = strchr(p, '/');
 
1451
        if (slash1)
 
1452
                n = slash1 - p;
 
1453
        else
 
1454
                n = strlen(p);
 
1455
 
 
1456
        for (i = 0; i < t->entry_count; i++) {
 
1457
                e = t->entries[i];
 
1458
                if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
 
1459
                        if (!slash1) {
 
1460
                                memcpy(leaf, e, sizeof(*leaf));
 
1461
                                if (e->tree && is_null_sha1(e->versions[1].sha1))
 
1462
                                        leaf->tree = dup_tree_content(e->tree);
 
1463
                                else
 
1464
                                        leaf->tree = NULL;
 
1465
                                return 1;
 
1466
                        }
 
1467
                        if (!S_ISDIR(e->versions[1].mode))
 
1468
                                return 0;
 
1469
                        if (!e->tree)
 
1470
                                load_tree(e);
 
1471
                        return tree_content_get(e, slash1 + 1, leaf);
 
1472
                }
 
1473
        }
 
1474
        return 0;
 
1475
}
 
1476
 
1267
1477
static int update_branch(struct branch *b)
1268
1478
{
1269
1479
        static const char *msg = "fast-import";
1272
1482
 
1273
1483
        if (read_ref(b->name, old_sha1))
1274
1484
                hashclr(old_sha1);
1275
 
        lock = lock_any_ref_for_update(b->name, old_sha1);
 
1485
        lock = lock_any_ref_for_update(b->name, old_sha1, 0);
1276
1486
        if (!lock)
1277
1487
                return error("Unable to lock %s", b->name);
1278
1488
        if (!force_update && !is_null_sha1(old_sha1)) {
1377
1587
 
1378
1588
static void read_next_command(void)
1379
1589
{
1380
 
        read_line(&command_buf, stdin, '\n');
 
1590
        do {
 
1591
                if (unread_command_buf) {
 
1592
                        unread_command_buf = 0;
 
1593
                        if (command_buf.eof)
 
1594
                                return;
 
1595
                } else {
 
1596
                        struct recent_command *rc;
 
1597
 
 
1598
                        command_buf.buf = NULL;
 
1599
                        read_line(&command_buf, stdin, '\n');
 
1600
                        if (command_buf.eof)
 
1601
                                return;
 
1602
 
 
1603
                        rc = rc_free;
 
1604
                        if (rc)
 
1605
                                rc_free = rc->next;
 
1606
                        else {
 
1607
                                rc = cmd_hist.next;
 
1608
                                cmd_hist.next = rc->next;
 
1609
                                cmd_hist.next->prev = &cmd_hist;
 
1610
                                free(rc->buf);
 
1611
                        }
 
1612
 
 
1613
                        rc->buf = command_buf.buf;
 
1614
                        rc->prev = cmd_tail;
 
1615
                        rc->next = cmd_hist.prev;
 
1616
                        rc->prev->next = rc;
 
1617
                        cmd_tail = rc;
 
1618
                }
 
1619
        } while (command_buf.buf[0] == '#');
 
1620
}
 
1621
 
 
1622
static void skip_optional_lf(void)
 
1623
{
 
1624
        int term_char = fgetc(stdin);
 
1625
        if (term_char != '\n' && term_char != EOF)
 
1626
                ungetc(term_char, stdin);
1381
1627
}
1382
1628
 
1383
1629
static void cmd_mark(void)
1403
1649
                size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1404
1650
                length = 0;
1405
1651
                buffer = xmalloc(sz);
 
1652
                command_buf.buf = NULL;
1406
1653
                for (;;) {
1407
 
                        read_next_command();
 
1654
                        read_line(&command_buf, stdin, '\n');
1408
1655
                        if (command_buf.eof)
1409
1656
                                die("EOF in data (terminator '%s' not found)", term);
1410
1657
                        if (term_len == command_buf.len
1411
1658
                                && !strcmp(term, command_buf.buf))
1412
1659
                                break;
1413
 
                        if (sz < (length + command_buf.len)) {
1414
 
                                sz = sz * 3 / 2 + 16;
1415
 
                                if (sz < (length + command_buf.len))
1416
 
                                        sz = length + command_buf.len;
1417
 
                                buffer = xrealloc(buffer, sz);
1418
 
                        }
 
1660
                        ALLOC_GROW(buffer, length + command_buf.len, sz);
1419
1661
                        memcpy(buffer + length,
1420
1662
                                command_buf.buf,
1421
1663
                                command_buf.len - 1);
1437
1679
                }
1438
1680
        }
1439
1681
 
1440
 
        if (fgetc(stdin) != '\n')
1441
 
                die("An lf did not trail the binary data as expected.");
1442
 
 
 
1682
        skip_optional_lf();
1443
1683
        *size = length;
1444
1684
        return buffer;
1445
1685
}
1629
1869
                            typename(type), command_buf.buf);
1630
1870
        }
1631
1871
 
1632
 
        tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
 
1872
        tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
1633
1873
        free(p_uq);
1634
1874
}
1635
1875
 
1645
1885
                        die("Garbage after path in: %s", command_buf.buf);
1646
1886
                p = p_uq;
1647
1887
        }
1648
 
        tree_content_remove(&b->branch_tree, p);
 
1888
        tree_content_remove(&b->branch_tree, p, NULL);
1649
1889
        free(p_uq);
1650
1890
}
1651
1891
 
 
1892
static void file_change_cr(struct branch *b, int rename)
 
1893
{
 
1894
        const char *s, *d;
 
1895
        char *s_uq, *d_uq;
 
1896
        const char *endp;
 
1897
        struct tree_entry leaf;
 
1898
 
 
1899
        s = command_buf.buf + 2;
 
1900
        s_uq = unquote_c_style(s, &endp);
 
1901
        if (s_uq) {
 
1902
                if (*endp != ' ')
 
1903
                        die("Missing space after source: %s", command_buf.buf);
 
1904
        }
 
1905
        else {
 
1906
                endp = strchr(s, ' ');
 
1907
                if (!endp)
 
1908
                        die("Missing space after source: %s", command_buf.buf);
 
1909
                s_uq = xmalloc(endp - s + 1);
 
1910
                memcpy(s_uq, s, endp - s);
 
1911
                s_uq[endp - s] = 0;
 
1912
        }
 
1913
        s = s_uq;
 
1914
 
 
1915
        endp++;
 
1916
        if (!*endp)
 
1917
                die("Missing dest: %s", command_buf.buf);
 
1918
 
 
1919
        d = endp;
 
1920
        d_uq = unquote_c_style(d, &endp);
 
1921
        if (d_uq) {
 
1922
                if (*endp)
 
1923
                        die("Garbage after dest in: %s", command_buf.buf);
 
1924
                d = d_uq;
 
1925
        }
 
1926
 
 
1927
        memset(&leaf, 0, sizeof(leaf));
 
1928
        if (rename)
 
1929
                tree_content_remove(&b->branch_tree, s, &leaf);
 
1930
        else
 
1931
                tree_content_get(&b->branch_tree, s, &leaf);
 
1932
        if (!leaf.versions[1].mode)
 
1933
                die("Path %s not in branch", s);
 
1934
        tree_content_set(&b->branch_tree, d,
 
1935
                leaf.versions[1].sha1,
 
1936
                leaf.versions[1].mode,
 
1937
                leaf.tree);
 
1938
 
 
1939
        free(s_uq);
 
1940
        free(d_uq);
 
1941
}
 
1942
 
1652
1943
static void file_change_deleteall(struct branch *b)
1653
1944
{
1654
1945
        release_tree_content_recursive(b->branch_tree.tree);
1684
1975
        }
1685
1976
}
1686
1977
 
1687
 
static void cmd_from(struct branch *b)
 
1978
static int cmd_from(struct branch *b)
1688
1979
{
1689
1980
        const char *from;
1690
1981
        struct branch *s;
1691
1982
 
1692
1983
        if (prefixcmp(command_buf.buf, "from "))
1693
 
                return;
 
1984
                return 0;
1694
1985
 
1695
1986
        if (b->branch_tree.tree) {
1696
1987
                release_tree_content_recursive(b->branch_tree.tree);
1725
2016
                die("Invalid ref name or SHA1 expression: %s", from);
1726
2017
 
1727
2018
        read_next_command();
 
2019
        return 1;
1728
2020
}
1729
2021
 
1730
2022
static struct hash_list *cmd_merge(unsigned int *count)
1809
2101
        }
1810
2102
 
1811
2103
        /* file_change* */
1812
 
        for (;;) {
1813
 
                if (1 == command_buf.len)
1814
 
                        break;
1815
 
                else if (!prefixcmp(command_buf.buf, "M "))
 
2104
        while (!command_buf.eof && command_buf.len > 1) {
 
2105
                if (!prefixcmp(command_buf.buf, "M "))
1816
2106
                        file_change_m(b);
1817
2107
                else if (!prefixcmp(command_buf.buf, "D "))
1818
2108
                        file_change_d(b);
 
2109
                else if (!prefixcmp(command_buf.buf, "R "))
 
2110
                        file_change_cr(b, 1);
 
2111
                else if (!prefixcmp(command_buf.buf, "C "))
 
2112
                        file_change_cr(b, 0);
1819
2113
                else if (!strcmp("deleteall", command_buf.buf))
1820
2114
                        file_change_deleteall(b);
1821
 
                else
1822
 
                        die("Unsupported file_change: %s", command_buf.buf);
 
2115
                else {
 
2116
                        unread_command_buf = 1;
 
2117
                        break;
 
2118
                }
1823
2119
                read_next_command();
1824
2120
        }
1825
2121
 
1960
2256
        else
1961
2257
                b = new_branch(sp);
1962
2258
        read_next_command();
1963
 
        cmd_from(b);
 
2259
        if (!cmd_from(b) && command_buf.len > 1)
 
2260
                unread_command_buf = 1;
1964
2261
}
1965
2262
 
1966
2263
static void cmd_checkpoint(void)
1971
2268
                dump_tags();
1972
2269
                dump_marks();
1973
2270
        }
1974
 
        read_next_command();
 
2271
        skip_optional_lf();
 
2272
}
 
2273
 
 
2274
static void cmd_progress(void)
 
2275
{
 
2276
        fwrite(command_buf.buf, 1, command_buf.len - 1, stdout);
 
2277
        fputc('\n', stdout);
 
2278
        fflush(stdout);
 
2279
        skip_optional_lf();
1975
2280
}
1976
2281
 
1977
2282
static void import_marks(const char *input_file)
2014
2319
 
2015
2320
int main(int argc, const char **argv)
2016
2321
{
2017
 
        int i, show_stats = 1;
 
2322
        unsigned int i, show_stats = 1;
2018
2323
 
2019
2324
        git_config(git_default_config);
2020
2325
        alloc_objects(object_entry_alloc);
2068
2373
        if (i != argc)
2069
2374
                usage(fast_import_usage);
2070
2375
 
 
2376
        rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
 
2377
        for (i = 0; i < (cmd_save - 1); i++)
 
2378
                rc_free[i].next = &rc_free[i + 1];
 
2379
        rc_free[cmd_save - 1].next = NULL;
 
2380
 
2071
2381
        prepare_packed_git();
2072
2382
        start_packfile();
 
2383
        set_die_routine(die_nicely);
2073
2384
        for (;;) {
2074
2385
                read_next_command();
2075
2386
                if (command_buf.eof)
2084
2395
                        cmd_reset_branch();
2085
2396
                else if (!strcmp("checkpoint", command_buf.buf))
2086
2397
                        cmd_checkpoint();
 
2398
                else if (!prefixcmp(command_buf.buf, "progress "))
 
2399
                        cmd_progress();
2087
2400
                else
2088
2401
                        die("Unsupported command: %s", command_buf.buf);
2089
2402
        }