fast-import: properly fanout notes when tree is imported

In typical uses of fast-import, trees are inherited from a parent commit. In that case, the tree_entry for the branch looks like: .versions[1].sha1 = $some_sha1 .tree = <tree structure loaded from $some_sha1> However, when trees are imported, rather than inherited, that is not the case. One can import a tree with a filemodify command, replacing the root tree object. e.g. "M 040000 $some_sha1 \n" In this case, the tree_entry for the branch looks like: .versions[1].sha1 = $some_sha1 .tree = NULL When adding new notes with the notemodify command, do_change_note_fanout is called to get a notes count, and to do so, it loops over the tree_entry->tree, but doesn't do anything when the tree is NULL. In the latter case above, it means do_change_note_fanout thinks the tree contains no notes, and new notes are added with no fanout. Interestingly, do_change_note_fanout does check whether subdirectories have a NULL .tree, in which case it uses load_tree(). Which means the right behaviour happens when using the filemodify command to import subdirectories. This change makes do_change_note_fanount call load_tree() whenever the tree_entry it is given has no tree loaded, making all cases handled equally. Signed-off-by: Mike Hommey <mh@glandium.org> Reviewed-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Mike Hommey committed Dec 21, 2016 at 06:04 UTC 405d7f4af64ece9a49c48b1b0c43b161b2dd94d5
2 files changed +47 -3
fast-import.c
+5 -3
@@ -2220,13 +2220,17 @@ static uintmax_t do_change_note_fanout(
2220 char *fullpath, unsigned int fullpath_len,
2221 unsigned char fanout)
2222 {
2223 - struct tree_content *t = root->tree;
2223 + struct tree_content *t;
2224 struct tree_entry *e, leaf;
2225 unsigned int i, tmp_hex_sha1_len, tmp_fullpath_len;
2226 uintmax_t num_notes = 0;
2227 unsigned char sha1[20];
2228 char realpath[60];
2229
2230 + if (!root->tree)
2231 + load_tree(root);
2232 + t = root->tree;
2233 +
2234 for (i = 0; t && i < t->entry_count; i++) {
2235 e = t->entries[i];
2236 tmp_hex_sha1_len = hex_sha1_len + e->name->str_len;
@@ -2278,8 +2282,6 @@ static uintmax_t do_change_note_fanout(
2282 leaf.tree);
2283 } else if (S_ISDIR(e->versions[1].mode)) {
2284 /* This is a subdir that may contain note entries */
2281 - if (!e->tree)
2282 - load_tree(e);
2285 num_notes += do_change_note_fanout(orig_root, e,
2286 hex_sha1, tmp_hex_sha1_len,
2287 fullpath, tmp_fullpath_len, fanout);
t/t9301-fast-import-notes.sh
+42
@@ -483,6 +483,48 @@ test_expect_success 'verify that lots of notes trigger a fanout scheme' '
483
484 '
485
486 +# Create another notes tree from the one above
487 +SP=" "
488 +cat >>input <<INPUT_END
489 +commit refs/heads/other_commits
490 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
491 +data <<COMMIT
492 +commit #$(($num_commit + 1))
493 +COMMIT
494 +
495 +from refs/heads/many_commits
496 +M 644 inline file
497 +data <<EOF
498 +file contents in commit #$(($num_commit + 1))
499 +EOF
500 +
501 +commit refs/notes/other_notes
502 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
503 +data <<COMMIT
504 +committing one more note on a tree imported from a previous notes tree
505 +COMMIT
506 +
507 +M 040000 $(git log --no-walk --format=%T refs/notes/many_notes)$SP
508 +N inline :$(($num_commit + 1))
509 +data <<EOF
510 +note for commit #$(($num_commit + 1))
511 +EOF
512 +INPUT_END
513 +
514 +test_expect_success 'verify that importing a notes tree respects the fanout scheme' '
515 + git fast-import <input &&
516 +
517 + # None of the entries in the top-level notes tree should be a full SHA1
518 + git ls-tree --name-only refs/notes/other_notes |
519 + while read path
520 + do
521 + if test $(expr length "$path") -ge 40
522 + then
523 + return 1
524 + fi
525 + done
526 +'
527 +
528 cat >>expect_non-note1 << EOF
529 This is not a note, but rather a regular file residing in a notes tree
530 EOF