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:
parent
20f546a86c
commit
654aaa37ab
@ -1657,6 +1657,33 @@ static void file_change_deleteall(struct branch *b)
|
|||||||
load_tree(&b->branch_tree);
|
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)
|
static void cmd_from(struct branch *b)
|
||||||
{
|
{
|
||||||
const char *from;
|
const char *from;
|
||||||
@ -1688,34 +1715,11 @@ static void cmd_from(struct branch *b)
|
|||||||
die("Mark :%" PRIuMAX " not a commit", idnum);
|
die("Mark :%" PRIuMAX " not a commit", idnum);
|
||||||
hashcpy(b->sha1, oe->sha1);
|
hashcpy(b->sha1, oe->sha1);
|
||||||
buf = gfi_unpack_entry(oe, &size);
|
buf = gfi_unpack_entry(oe, &size);
|
||||||
if (!buf || size < 46)
|
cmd_from_commit(b, buf, size);
|
||||||
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);
|
free(buf);
|
||||||
hashcpy(b->branch_tree.versions[0].sha1,
|
} else if (!get_sha1(from, b->sha1))
|
||||||
b->branch_tree.versions[1].sha1);
|
cmd_from_existing(b);
|
||||||
} else if (!get_sha1(from, b->sha1)) {
|
else
|
||||||
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
|
|
||||||
die("Invalid ref name or SHA1 expression: %s", from);
|
die("Invalid ref name or SHA1 expression: %s", from);
|
||||||
|
|
||||||
read_next_command();
|
read_next_command();
|
||||||
|
Loading…
Reference in New Issue
Block a user