Skip to content

Commit

Permalink
Refactor fast-import branch creation from existing commit
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Shawn O. Pearce committed May 24, 2007
1 parent 20f546a commit 654aaa3
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 654aaa3

Please sign in to comment.