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>
This commit is contained in:
Shawn O. Pearce 2007-05-24 00:05:19 -04:00
parent 20f546a86c
commit 654aaa37ab

View file

@ -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();