@matheusbsbs / matheus-bagliano / commits / 654aaa37ab
Fork 0

Refactor fast-import branch creation from existing commit

To resolve a corner case uncovered by Simon Hausmann I need to reuse the logic for the SHA-1 expression version of the 'from ' command within the mark version of the 'from ' command. This change doesn't alter any functionality, but is merely breaking the common code out to a function that I can reuse. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed May 24, 2007 at 00:05 UTC 654aaa37ab5c70650bdd16d57b56c2d0f9aa43cf
1 file changed +31 -27
fast-import.c
+31 -27
@@ -1657,6 +1657,33 @@ static void file_change_deleteall(struct branch *b)
1657 load_tree(&b->branch_tree);
1658 }
1659
1660 +static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
1661 +{
1662 + if (!buf || size < 46)
1663 + die("Not a valid commit: %s", sha1_to_hex(b->sha1));
1664 + if (memcmp("tree ", buf, 5)
1665 + || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1666 + die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1667 + hashcpy(b->branch_tree.versions[0].sha1,
1668 + b->branch_tree.versions[1].sha1);
1669 +}
1670 +
1671 +static void cmd_from_existing(struct branch *b)
1672 +{
1673 + if (is_null_sha1(b->sha1)) {
1674 + hashclr(b->branch_tree.versions[0].sha1);
1675 + hashclr(b->branch_tree.versions[1].sha1);
1676 + } else {
1677 + unsigned long size;
1678 + char *buf;
1679 +
1680 + buf = read_object_with_reference(b->sha1,
1681 + commit_type, &size, b->sha1);
1682 + cmd_from_commit(b, buf, size);
1683 + free(buf);
1684 + }
1685 +}
1686 +
1687 static void cmd_from(struct branch *b)
1688 {
1689 const char *from;
@@ -1688,34 +1715,11 @@ static void cmd_from(struct branch *b)
1715 die("Mark :%" PRIuMAX " not a commit", idnum);
1716 hashcpy(b->sha1, oe->sha1);
1717 buf = gfi_unpack_entry(oe, &size);
1691 - if (!buf || size < 46)
1692 - die("Not a valid commit: %s", from);
1693 - if (memcmp("tree ", buf, 5)
1694 - || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1695 - die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1718 + cmd_from_commit(b, buf, size);
1719 free(buf);
1697 - hashcpy(b->branch_tree.versions[0].sha1,
1698 - b->branch_tree.versions[1].sha1);
1699 - } else if (!get_sha1(from, b->sha1)) {
1700 - if (is_null_sha1(b->sha1)) {
1701 - hashclr(b->branch_tree.versions[0].sha1);
1702 - hashclr(b->branch_tree.versions[1].sha1);
1703 - } else {
1704 - unsigned long size;
1705 - char *buf;
1706 -
1707 - buf = read_object_with_reference(b->sha1,
1708 - commit_type, &size, b->sha1);
1709 - if (!buf || size < 46)
1710 - die("Not a valid commit: %s", from);
1711 - if (memcmp("tree ", buf, 5)
1712 - || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1713 - die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1714 - free(buf);
1715 - hashcpy(b->branch_tree.versions[0].sha1,
1716 - b->branch_tree.versions[1].sha1);
1717 - }
1718 - } else
1720 + } else if (!get_sha1(from, b->sha1))
1721 + cmd_from_existing(b);
1722 + else
1723 die("Invalid ref name or SHA1 expression: %s", from);
1724
1725 read_next_command();