@matheusbsbs / matheus-bagliano / commits / 00e2b8842c
Fork 0

Remove branch creation command from fast-import.

Jon Smirl was finding it difficult to alter cvs2svn to generate branch commands prior to the first commit of the same branch. This change moves the 'from' command to be an optional parameter of the 'commit' command, thereby allowing a new branch to be defined at the moment it gets used to create the first commit on that branch. This change makes it impossible to create a branch with no commits on it as at least one commit is needed to register the branch. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Aug 24, 2006 at 18:45 UTC 00e2b8842c58e451fcf8038287c8420423bab50a
1 file changed +71 -98
fast-import.c
+71 -98
@@ -4,7 +4,6 @@ Format of STDIN stream:
4 stream ::= cmd*;
5
6 cmd ::= new_blob
7 - | new_branch
7 | new_commit
8 | new_tag
9 ;
@@ -14,15 +13,12 @@ Format of STDIN stream:
13 file_content;
14 file_content ::= data;
15
17 - new_branch ::= 'branch' sp ref_str lf
18 - ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
19 - lf;
20 -
16 new_commit ::= 'commit' sp ref_str lf
22 - mark?
23 - ('author' sp name '<' email '>' ts tz lf)?
24 - 'committer' sp name '<' email '>' ts tz lf
25 - commit_msg
17 + ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
18 + mark?
19 + ('author' sp name '<' email '>' ts tz lf)?
20 + 'committer' sp name '<' email '>' ts tz lf
21 + commit_msg
22 file_change*
23 lf;
24 commit_msg ::= data;
@@ -831,7 +827,7 @@ static void load_tree(struct tree_entry *root)
827 } else {
828 char type[20];
829 buf = read_sha1_file(root->sha1, type, &size);
834 - if (!buf || !strcmp(type, tree_type))
830 + if (!buf || strcmp(type, tree_type))
831 die("Can't load tree %s", sha1_to_hex(root->sha1));
832 }
833
@@ -1299,6 +1295,69 @@ static void file_change_d(struct branch *b)
1295 free(p_uq);
1296 }
1297
1298 +static void cmd_from(struct branch *b)
1299 +{
1300 + const char *from, *endp;
1301 + char *str_uq;
1302 + struct branch *s;
1303 +
1304 + if (strncmp("from ", command_buf.buf, 5))
1305 + return;
1306 +
1307 + if (b->last_commit)
1308 + die("Can't reinitailize branch %s", b->name);
1309 +
1310 + from = strchr(command_buf.buf, ' ') + 1;
1311 + str_uq = unquote_c_style(from, &endp);
1312 + if (str_uq) {
1313 + if (*endp)
1314 + die("Garbage after string in: %s", command_buf.buf);
1315 + from = str_uq;
1316 + }
1317 +
1318 + s = lookup_branch(from);
1319 + if (b == s)
1320 + die("Can't create a branch from itself: %s", b->name);
1321 + else if (s) {
1322 + memcpy(b->sha1, s->sha1, 20);
1323 + memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20);
1324 + } else if (*from == ':') {
1325 + unsigned long idnum = strtoul(from + 1, NULL, 10);
1326 + struct object_entry *oe = find_mark(idnum);
1327 + unsigned long size;
1328 + char *buf;
1329 + if (oe->type != OBJ_COMMIT)
1330 + die("Mark :%lu not a commit", idnum);
1331 + memcpy(b->sha1, oe->sha1, 20);
1332 + buf = unpack_entry(oe->offset, &size);
1333 + if (!buf || size < 46)
1334 + die("Not a valid commit: %s", from);
1335 + if (memcmp("tree ", buf, 5)
1336 + || get_sha1_hex(buf + 5, b->branch_tree.sha1))
1337 + die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1338 + free(buf);
1339 + } else if (!get_sha1(from, b->sha1)) {
1340 + if (!memcmp(b->sha1, null_sha1, 20))
1341 + memcpy(b->branch_tree.sha1, null_sha1, 20);
1342 + else {
1343 + unsigned long size;
1344 + char *buf;
1345 +
1346 + buf = read_object_with_reference(b->sha1,
1347 + type_names[OBJ_COMMIT], &size, b->sha1);
1348 + if (!buf || size < 46)
1349 + die("Not a valid commit: %s", from);
1350 + if (memcmp("tree ", buf, 5)
1351 + || get_sha1_hex(buf + 5, b->branch_tree.sha1))
1352 + die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1353 + free(buf);
1354 + }
1355 + } else
1356 + die("Invalid ref name or SHA1 expression: %s", from);
1357 +
1358 + read_next_command();
1359 +}
1360 +
1361 static void cmd_new_commit()
1362 {
1363 struct branch *b;
@@ -1321,11 +1380,12 @@ static void cmd_new_commit()
1380 }
1381 b = lookup_branch(sp);
1382 if (!b)
1324 - die("Branch not declared: %s", sp);
1383 + b = new_branch(sp);
1384 if (str_uq)
1385 free(str_uq);
1386
1387 read_next_command();
1388 + cmd_from(b);
1389 cmd_mark();
1390 if (!strncmp("author ", command_buf.buf, 7)) {
1391 author = strdup(command_buf.buf);
@@ -1385,91 +1445,6 @@ static void cmd_new_commit()
1445 b->last_commit = object_count_by_type[OBJ_COMMIT];
1446 }
1447
1388 -static void cmd_new_branch()
1389 -{
1390 - struct branch *b;
1391 - char *str_uq;
1392 - const char *endp;
1393 - char *sp;
1394 -
1395 - /* Obtain the new branch name from the rest of our command */
1396 - sp = strchr(command_buf.buf, ' ') + 1;
1397 - str_uq = unquote_c_style(sp, &endp);
1398 - if (str_uq) {
1399 - if (*endp)
1400 - die("Garbage after ref in: %s", command_buf.buf);
1401 - sp = str_uq;
1402 - }
1403 - b = new_branch(sp);
1404 - if (str_uq)
1405 - free(str_uq);
1406 - read_next_command();
1407 -
1408 - /* from ... */
1409 - if (!strncmp("from ", command_buf.buf, 5)) {
1410 - const char *from;
1411 - struct branch *s;
1412 -
1413 - from = strchr(command_buf.buf, ' ') + 1;
1414 - str_uq = unquote_c_style(from, &endp);
1415 - if (str_uq) {
1416 - if (*endp)
1417 - die("Garbage after string in: %s", command_buf.buf);
1418 - from = str_uq;
1419 - }
1420 -
1421 - s = lookup_branch(from);
1422 - if (b == s)
1423 - die("Can't create a branch from itself: %s", b->name);
1424 - else if (s) {
1425 - memcpy(b->sha1, s->sha1, 20);
1426 - memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20);
1427 - } else if (*from == ':') {
1428 - unsigned long idnum = strtoul(from + 1, NULL, 10);
1429 - struct object_entry *oe = find_mark(idnum);
1430 - unsigned long size;
1431 - char *buf;
1432 - if (oe->type != OBJ_COMMIT)
1433 - die("Mark :%lu not a commit", idnum);
1434 - memcpy(b->sha1, oe->sha1, 20);
1435 - buf = unpack_entry(oe->offset, &size);
1436 - if (!buf || size < 46)
1437 - die("Not a valid commit: %s", from);
1438 - if (memcmp("tree ", buf, 5)
1439 - || get_sha1_hex(buf + 5, b->branch_tree.sha1))
1440 - die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1441 - free(buf);
1442 - } else if (!get_sha1(from, b->sha1)) {
1443 - if (!memcmp(b->sha1, null_sha1, 20))
1444 - memcpy(b->branch_tree.sha1, null_sha1, 20);
1445 - else {
1446 - unsigned long size;
1447 - char *buf;
1448 -
1449 - buf = read_object_with_reference(b->sha1,
1450 - type_names[OBJ_COMMIT], &size, b->sha1);
1451 - if (!buf || size < 46)
1452 - die("Not a valid commit: %s", from);
1453 - if (memcmp("tree ", buf, 5)
1454 - || get_sha1_hex(buf + 5, b->branch_tree.sha1))
1455 - die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1456 - free(buf);
1457 - }
1458 - } else
1459 - die("Invalid ref name or SHA1 expression: %s", from);
1460 -
1461 - if (str_uq)
1462 - free(str_uq);
1463 - read_next_command();
1464 - } else {
1465 - memcpy(b->sha1, null_sha1, 20);
1466 - memcpy(b->branch_tree.sha1, null_sha1, 20);
1467 - }
1468 -
1469 - if (command_buf.eof || command_buf.len > 1)
1470 - die("An lf did not terminate the branch command as expected.");
1471 -}
1472 -
1448 static void cmd_new_tag()
1449 {
1450 char *str_uq;
@@ -1623,8 +1598,6 @@ int main(int argc, const char **argv)
1598 break;
1599 else if (!strcmp("blob", command_buf.buf))
1600 cmd_new_blob();
1626 - else if (!strncmp("branch ", command_buf.buf, 7))
1627 - cmd_new_branch();
1601 else if (!strncmp("commit ", command_buf.buf, 7))
1602 cmd_new_commit();
1603 else if (!strncmp("tag ", command_buf.buf, 4))