From 654aaa37ab5c70650bdd16d57b56c2d0f9aa43cf Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Thu, 24 May 2007 00:05:19 -0400 Subject: 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 diff --git a/fast-import.c b/fast-import.c index f308db7..f5107df 100644 --- a/fast-import.c +++ b/fast-import.c @@ -1657,6 +1657,33 @@ static void file_change_deleteall(struct branch *b) load_tree(&b->branch_tree); } +static void cmd_from_commit(struct branch *b, char *buf, unsigned long size) +{ + if (!buf || size < 46) + die("Not a valid commit: %s", sha1_to_hex(b->sha1)); + if (memcmp("tree ", buf, 5) + || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1)) + die("The commit %s is corrupt", sha1_to_hex(b->sha1)); + hashcpy(b->branch_tree.versions[0].sha1, + b->branch_tree.versions[1].sha1); +} + +static void cmd_from_existing(struct branch *b) +{ + if (is_null_sha1(b->sha1)) { + hashclr(b->branch_tree.versions[0].sha1); + hashclr(b->branch_tree.versions[1].sha1); + } else { + unsigned long size; + char *buf; + + buf = read_object_with_reference(b->sha1, + commit_type, &size, b->sha1); + cmd_from_commit(b, buf, size); + free(buf); + } +} + static void cmd_from(struct branch *b) { const char *from; @@ -1688,34 +1715,11 @@ static void cmd_from(struct branch *b) die("Mark :%" PRIuMAX " not a commit", idnum); hashcpy(b->sha1, oe->sha1); buf = gfi_unpack_entry(oe, &size); - if (!buf || size < 46) - die("Not a valid commit: %s", from); - if (memcmp("tree ", buf, 5) - || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1)) - die("The commit %s is corrupt", sha1_to_hex(b->sha1)); + cmd_from_commit(b, buf, size); free(buf); - hashcpy(b->branch_tree.versions[0].sha1, - b->branch_tree.versions[1].sha1); - } else if (!get_sha1(from, b->sha1)) { - if (is_null_sha1(b->sha1)) { - hashclr(b->branch_tree.versions[0].sha1); - hashclr(b->branch_tree.versions[1].sha1); - } else { - unsigned long size; - char *buf; - - buf = read_object_with_reference(b->sha1, - commit_type, &size, b->sha1); - if (!buf || size < 46) - die("Not a valid commit: %s", from); - if (memcmp("tree ", buf, 5) - || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1)) - die("The commit %s is corrupt", sha1_to_hex(b->sha1)); - free(buf); - hashcpy(b->branch_tree.versions[0].sha1, - b->branch_tree.versions[1].sha1); - } - } else + } else if (!get_sha1(from, b->sha1)) + cmd_from_existing(b); + else die("Invalid ref name or SHA1 expression: %s", from); read_next_command(); -- cgit v0.10.2-6-g49f6