Merge branch 'jk/no-more-self-assignment'

This started as a topic to reduce "type var = var" self assignment
tricks that were used to squelch "variable used uninitialized perhaps?"
warning from some compilers, but resulted in rewriting logic with
a version that is simpler and easier to understand for humans.

* jk/no-more-self-assignment:
  match-trees: simplify score_trees() using tree_entry()
  submodule: clarify logic in show_submodule_summary
This commit is contained in:
Junio C Hamano 2013-04-02 15:09:35 -07:00
commit 5fb7b899fb
2 changed files with 34 additions and 47 deletions

View File

@ -47,6 +47,13 @@ static int score_matches(unsigned mode1, unsigned mode2, const char *path)
return score; return score;
} }
static int base_name_entries_compare(const struct name_entry *a,
const struct name_entry *b)
{
return base_name_compare(a->path, tree_entry_len(a), a->mode,
b->path, tree_entry_len(b), b->mode);
}
/* /*
* Inspect two trees, and give a score that tells how similar they are. * Inspect two trees, and give a score that tells how similar they are.
*/ */
@ -71,54 +78,35 @@ static int score_trees(const unsigned char *hash1, const unsigned char *hash2)
if (type != OBJ_TREE) if (type != OBJ_TREE)
die("%s is not a tree", sha1_to_hex(hash2)); die("%s is not a tree", sha1_to_hex(hash2));
init_tree_desc(&two, two_buf, size); init_tree_desc(&two, two_buf, size);
while (one.size | two.size) { for (;;) {
const unsigned char *elem1 = elem1; struct name_entry e1, e2;
const unsigned char *elem2 = elem2; int got_entry_from_one = tree_entry(&one, &e1);
const char *path1 = path1; int got_entry_from_two = tree_entry(&two, &e2);
const char *path2 = path2;
unsigned mode1 = mode1;
unsigned mode2 = mode2;
int cmp; int cmp;
if (one.size) if (got_entry_from_one && got_entry_from_two)
elem1 = tree_entry_extract(&one, &path1, &mode1); cmp = base_name_entries_compare(&e1, &e2);
if (two.size) else if (got_entry_from_one)
elem2 = tree_entry_extract(&two, &path2, &mode2);
if (!one.size) {
/* two has more entries */
score += score_missing(mode2, path2);
update_tree_entry(&two);
continue;
}
if (!two.size) {
/* two lacks this entry */ /* two lacks this entry */
score += score_missing(mode1, path1); cmp = -1;
update_tree_entry(&one); else if (got_entry_from_two)
continue; /* two has more entries */
} cmp = 1;
cmp = base_name_compare(path1, strlen(path1), mode1, else
path2, strlen(path2), mode2); break;
if (cmp < 0) {
if (cmp < 0)
/* path1 does not appear in two */ /* path1 does not appear in two */
score += score_missing(mode1, path1); score += score_missing(e1.mode, e1.path);
update_tree_entry(&one); else if (cmp > 0)
continue;
}
else if (cmp > 0) {
/* path2 does not appear in one */ /* path2 does not appear in one */
score += score_missing(mode2, path2); score += score_missing(e2.mode, e2.path);
update_tree_entry(&two); else if (hashcmp(e1.sha1, e2.sha1))
continue;
}
else if (hashcmp(elem1, elem2))
/* they are different */ /* they are different */
score += score_differs(mode1, mode2, path1); score += score_differs(e1.mode, e2.mode, e1.path);
else else
/* same subtree or blob */ /* same subtree or blob */
score += score_matches(mode1, mode2, path1); score += score_matches(e1.mode, e2.mode, e1.path);
update_tree_entry(&one);
update_tree_entry(&two);
} }
free(one_buf); free(one_buf);
free(two_buf); free(two_buf);

View File

@ -261,7 +261,7 @@ void show_submodule_summary(FILE *f, const char *path,
const char *del, const char *add, const char *reset) const char *del, const char *add, const char *reset)
{ {
struct rev_info rev; struct rev_info rev;
struct commit *left = left, *right = right; struct commit *left = NULL, *right = NULL;
const char *message = NULL; const char *message = NULL;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
int fast_forward = 0, fast_backward = 0; int fast_forward = 0, fast_backward = 0;
@ -275,10 +275,8 @@ void show_submodule_summary(FILE *f, const char *path,
else if (!(left = lookup_commit_reference(one)) || else if (!(left = lookup_commit_reference(one)) ||
!(right = lookup_commit_reference(two))) !(right = lookup_commit_reference(two)))
message = "(commits not present)"; message = "(commits not present)";
else if (prepare_submodule_summary(&rev, path, left, right,
if (!message && &fast_forward, &fast_backward))
prepare_submodule_summary(&rev, path, left, right,
&fast_forward, &fast_backward))
message = "(revision walker failed)"; message = "(revision walker failed)";
if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
@ -302,11 +300,12 @@ void show_submodule_summary(FILE *f, const char *path,
strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset); strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
fwrite(sb.buf, sb.len, 1, f); fwrite(sb.buf, sb.len, 1, f);
if (!message) { if (!message) /* only NULL if we succeeded in setting up the walk */
print_submodule_summary(&rev, f, del, add, reset); print_submodule_summary(&rev, f, del, add, reset);
if (left)
clear_commit_marks(left, ~0); clear_commit_marks(left, ~0);
if (right)
clear_commit_marks(right, ~0); clear_commit_marks(right, ~0);
}
strbuf_release(&sb); strbuf_release(&sb);
} }