notes-merge: use opendir/readdir instead of using read_directory()

notes_merge_commit() only needs to list all entries (non-recursively) under a directory, which can be easily accomplished with opendir/readdir and would be more lightweight than read_directory(). read_directory() is designed to list paths inside a working directory. Using it outside of its scope may lead to undesired effects. Apparently, one of the undesired effects of read_directory() is that it doesn't deal with being given absolute paths. This creates problems for notes_merge_commit() when git_path() returns an absolute path, which happens when the current working directory is in a subdirectory of the .git directory. Originally-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Updated-by: Johan Herland <johan@herland.net> Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Mar 12, 2012 at 15:57 UTC a0be62c100897573ef1575ec0d5e8b215e9dcafe
2 files changed +31 -21
notes-merge.c
+30 -20
@@ -687,51 +687,60 @@ int notes_merge_commit(struct notes_merge_options *o,
687 {
688 /*
689 * Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
690 - * found notes to 'partial_tree'. Write the updates notes tree to
690 + * found notes to 'partial_tree'. Write the updated notes tree to
691 * the DB, and commit the resulting tree object while reusing the
692 * commit message and parents from 'partial_commit'.
693 * Finally store the new commit object SHA1 into 'result_sha1'.
694 */
695 - struct dir_struct dir;
696 - char *path = xstrdup(git_path(NOTES_MERGE_WORKTREE "/"));
697 - int path_len = strlen(path), i;
695 + DIR *dir;
696 + struct dirent *e;
697 + struct strbuf path = STRBUF_INIT;
698 char *msg = strstr(partial_commit->buffer, "\n\n");
699 struct strbuf sb_msg = STRBUF_INIT;
700 + int baselen;
701
702 + strbuf_addstr(&path, git_path(NOTES_MERGE_WORKTREE));
703 if (o->verbosity >= 3)
702 - printf("Committing notes in notes merge worktree at %.*s\n",
703 - path_len - 1, path);
704 + printf("Committing notes in notes merge worktree at %s\n",
705 + path.buf);
706
707 if (!msg || msg[2] == '\0')
708 die("partial notes commit has empty message");
709 msg += 2;
710
709 - memset(&dir, 0, sizeof(dir));
710 - read_directory(&dir, path, path_len, NULL);
711 - for (i = 0; i < dir.nr; i++) {
712 - struct dir_entry *ent = dir.entries[i];
711 + dir = opendir(path.buf);
712 + if (!dir)
713 + die_errno("could not open %s", path.buf);
714 +
715 + strbuf_addch(&path, '/');
716 + baselen = path.len;
717 + while ((e = readdir(dir)) != NULL) {
718 struct stat st;
714 - const char *relpath = ent->name + path_len;
719 unsigned char obj_sha1[20], blob_sha1[20];
720
717 - if (ent->len - path_len != 40 || get_sha1_hex(relpath, obj_sha1)) {
721 + if (is_dot_or_dotdot(e->d_name))
722 + continue;
723 +
724 + if (strlen(e->d_name) != 40 || get_sha1_hex(e->d_name, obj_sha1)) {
725 if (o->verbosity >= 3)
719 - printf("Skipping non-SHA1 entry '%s'\n",
720 - ent->name);
726 + printf("Skipping non-SHA1 entry '%s%s'\n",
727 + path.buf, e->d_name);
728 continue;
729 }
730
731 + strbuf_addstr(&path, e->d_name);
732 /* write file as blob, and add to partial_tree */
725 - if (stat(ent->name, &st))
726 - die_errno("Failed to stat '%s'", ent->name);
727 - if (index_path(blob_sha1, ent->name, &st, HASH_WRITE_OBJECT))
728 - die("Failed to write blob object from '%s'", ent->name);
733 + if (stat(path.buf, &st))
734 + die_errno("Failed to stat '%s'", path.buf);
735 + if (index_path(blob_sha1, path.buf, &st, HASH_WRITE_OBJECT))
736 + die("Failed to write blob object from '%s'", path.buf);
737 if (add_note(partial_tree, obj_sha1, blob_sha1, NULL))
738 die("Failed to add resolved note '%s' to notes tree",
731 - ent->name);
739 + path.buf);
740 if (o->verbosity >= 4)
741 printf("Added resolved note for object %s: %s\n",
742 sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1));
743 + strbuf_setlen(&path, baselen);
744 }
745
746 strbuf_attach(&sb_msg, msg, strlen(msg), strlen(msg) + 1);
@@ -740,7 +749,8 @@ int notes_merge_commit(struct notes_merge_options *o,
749 if (o->verbosity >= 4)
750 printf("Finalized notes merge commit: %s\n",
751 sha1_to_hex(result_sha1));
743 - free(path);
752 + strbuf_release(&path);
753 + closedir(dir);
754 return 0;
755 }
756
t/t3310-notes-merge-manual-resolve.sh
+1 -1
@@ -558,7 +558,7 @@ foo
558 bar
559 EOF
560
561 -test_expect_failure 'switch cwd before committing notes merge' '
561 +test_expect_success 'switch cwd before committing notes merge' '
562 git notes add -m foo HEAD &&
563 git notes --ref=other add -m bar HEAD &&
564 test_must_fail git notes merge refs/notes/other &&