git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Herland <johan@herland.net>
To: git@vger.kernel.org
Cc: Johan Herland <johan@herland.net>,
	gitster@pobox.com, david@tethera.net, pclouds@gmail.com
Subject: [PATCH 2/2] notes-merge: use opendir/readdir instead of using read_directory()
Date: Mon, 12 Mar 2012 15:47:27 +0100	[thread overview]
Message-ID: <1331563647-1909-2-git-send-email-johan@herland.net> (raw)
In-Reply-To: <1331563647-1909-1-git-send-email-johan@herland.net>

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>
---

This is a resurrection of pclouds' patch 2/11 in a patch series sent
last October for rewriting read_directory(). This patch doesn't
actually touch read_directory(), but instead rewrites
notes_merge_commit() to use opendir()/readdir() instead of
read_directory(). Since the usage of read_directory() is what caused
the bug that David found (in the previous patch), this rewrite happens
to fix that bug as well.


Have fun! :)

...Johan

 notes-merge.c                         |   50 ++++++++++++++++++++-------------
 t/t3310-notes-merge-manual-resolve.sh |    2 +-
 2 files changed, 31 insertions(+), 21 deletions(-)

diff --git a/notes-merge.c b/notes-merge.c
index fb0832f..3a16af2 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -687,51 +687,60 @@ int notes_merge_commit(struct notes_merge_options *o,
 {
 	/*
 	 * Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
-	 * found notes to 'partial_tree'. Write the updates notes tree to
+	 * found notes to 'partial_tree'. Write the updated notes tree to
 	 * the DB, and commit the resulting tree object while reusing the
 	 * commit message and parents from 'partial_commit'.
 	 * Finally store the new commit object SHA1 into 'result_sha1'.
 	 */
-	struct dir_struct dir;
-	char *path = xstrdup(git_path(NOTES_MERGE_WORKTREE "/"));
-	int path_len = strlen(path), i;
+	DIR *dir;
+	struct dirent *e;
+	struct strbuf path = STRBUF_INIT;
 	char *msg = strstr(partial_commit->buffer, "\n\n");
 	struct strbuf sb_msg = STRBUF_INIT;
+	int baselen;

+	strbuf_addstr(&path, git_path(NOTES_MERGE_WORKTREE));
 	if (o->verbosity >= 3)
-		printf("Committing notes in notes merge worktree at %.*s\n",
-			path_len - 1, path);
+		printf("Committing notes in notes merge worktree at %s\n",
+			path.buf);

 	if (!msg || msg[2] == '\0')
 		die("partial notes commit has empty message");
 	msg += 2;

-	memset(&dir, 0, sizeof(dir));
-	read_directory(&dir, path, path_len, NULL);
-	for (i = 0; i < dir.nr; i++) {
-		struct dir_entry *ent = dir.entries[i];
+	dir = opendir(path.buf);
+	if (!dir)
+		die_errno("could not open %s", path.buf);
+
+	strbuf_addch(&path, '/');
+	baselen = path.len;
+	while ((e = readdir(dir)) != NULL) {
 		struct stat st;
-		const char *relpath = ent->name + path_len;
 		unsigned char obj_sha1[20], blob_sha1[20];

-		if (ent->len - path_len != 40 || get_sha1_hex(relpath, obj_sha1)) {
+		if (is_dot_or_dotdot(e->d_name))
+			continue;
+
+		if (strlen(e->d_name) != 40 || get_sha1_hex(e->d_name, obj_sha1)) {
 			if (o->verbosity >= 3)
-				printf("Skipping non-SHA1 entry '%s'\n",
-								ent->name);
+				printf("Skipping non-SHA1 entry '%s%s'\n",
+					path.buf, e->d_name);
 			continue;
 		}

+		strbuf_addstr(&path, e->d_name);
 		/* write file as blob, and add to partial_tree */
-		if (stat(ent->name, &st))
-			die_errno("Failed to stat '%s'", ent->name);
-		if (index_path(blob_sha1, ent->name, &st, HASH_WRITE_OBJECT))
-			die("Failed to write blob object from '%s'", ent->name);
+		if (stat(path.buf, &st))
+			die_errno("Failed to stat '%s'", path.buf);
+		if (index_path(blob_sha1, path.buf, &st, HASH_WRITE_OBJECT))
+			die("Failed to write blob object from '%s'", path.buf);
 		if (add_note(partial_tree, obj_sha1, blob_sha1, NULL))
 			die("Failed to add resolved note '%s' to notes tree",
-			    ent->name);
+			    path.buf);
 		if (o->verbosity >= 4)
 			printf("Added resolved note for object %s: %s\n",
 				sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1));
+		strbuf_setlen(&path, baselen);
 	}

 	strbuf_attach(&sb_msg, msg, strlen(msg), strlen(msg) + 1);
@@ -740,7 +749,8 @@ int notes_merge_commit(struct notes_merge_options *o,
 	if (o->verbosity >= 4)
 		printf("Finalized notes merge commit: %s\n",
 			sha1_to_hex(result_sha1));
-	free(path);
+	strbuf_release(&path);
+	closedir(dir);
 	return 0;
 }

diff --git a/t/t3310-notes-merge-manual-resolve.sh b/t/t3310-notes-merge-manual-resolve.sh
index 0c531c3..d6d6ac6 100755
--- a/t/t3310-notes-merge-manual-resolve.sh
+++ b/t/t3310-notes-merge-manual-resolve.sh
@@ -558,7 +558,7 @@ foo
 bar
 EOF

-test_expect_failure 'switch cwd before committing notes merge' '
+test_expect_success 'switch cwd before committing notes merge' '
 	git notes add -m foo HEAD &&
 	git notes --ref=other add -m bar HEAD &&
 	test_must_fail git notes merge refs/notes/other &&
--
1.7.9.2

  reply	other threads:[~2012-03-12 14:48 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-24  6:36 [PATCH/WIP 00/11] read_directory() rewrite to support struct pathspec Nguyễn Thái Ngọc Duy
2011-10-24  6:36 ` [PATCH/WIP 01/11] Introduce "check-attr --excluded" as a replacement for "add --ignore-missing" Nguyễn Thái Ngọc Duy
2011-10-27 18:08   ` Junio C Hamano
2011-10-28 20:51     ` Nguyen Thai Ngoc Duy
2011-10-24  6:36 ` [PATCH/WIP 02/11] notes-merge: use opendir/readdir instead of using read_directory() Nguyễn Thái Ngọc Duy
2011-10-25 19:27   ` Junio C Hamano
2011-10-26  0:08     ` Nguyen Thai Ngoc Duy
2011-10-26 17:37       ` Junio C Hamano
2011-10-27  7:51         ` Nguyen Thai Ngoc Duy
2011-10-27 17:23           ` Junio C Hamano
2011-10-28 20:47             ` Nguyen Thai Ngoc Duy
2012-03-12 14:47   ` [PATCH 1/2] t3310: Add testcase demonstrating failure to --commit from within another dir Johan Herland
2012-03-12 14:47     ` Johan Herland [this message]
2012-03-12 14:53       ` [PATCH 2/2] notes-merge: use opendir/readdir instead of using read_directory() Nguyen Thai Ngoc Duy
2012-03-14  8:39       ` [PATCH jh/notes-merge-in-git-dir-worktree] fixup! t3310 on Windows Johannes Sixt
2012-03-14 11:39         ` Johan Herland
2012-03-14 11:59           ` Johannes Sixt
2012-03-14 12:20             ` David Bremner
2012-03-14 12:56             ` Johan Herland
2012-03-14 17:44               ` Junio C Hamano
2012-03-14 23:55                 ` [PATCH 3/2] notes-merge: Don't remove .git/NOTES_MERGE_WORKTREE; it may be the user's cwd Johan Herland
2012-03-15  7:02                   ` Junio C Hamano
2012-03-15  7:16                     ` Junio C Hamano
2012-03-15  7:39                       ` Johan Herland
2012-03-15  8:04                         ` Re* " Junio C Hamano
2012-03-15  8:12                         ` Junio C Hamano
2012-03-15  8:12                   ` Johannes Sixt
2011-10-24  6:36 ` [PATCH/WIP 03/11] t5403: avoid doing "git add foo/bar" where foo/.git exists Nguyễn Thái Ngọc Duy
2011-10-25 19:19   ` Junio C Hamano
2011-10-26  0:18     ` Nguyen Thai Ngoc Duy
2011-10-26 17:26       ` Junio C Hamano
2011-10-27  8:06         ` Nguyen Thai Ngoc Duy
2011-10-27 17:41           ` Junio C Hamano
2011-10-30  5:55             ` Nguyen Thai Ngoc Duy
2011-10-30  7:08               ` Junio C Hamano
2011-10-30  9:55                 ` Nguyen Thai Ngoc Duy
2011-10-30 23:47                   ` Junio C Hamano
2011-10-24  6:36 ` [PATCH/WIP 04/11] tree-walk.c: do not leak internal structure in tree_entry_len() Nguyễn Thái Ngọc Duy
2011-10-25 19:20   ` Junio C Hamano
2011-10-24  6:36 ` [PATCH/WIP 05/11] symbolize return values of tree_entry_interesting() Nguyễn Thái Ngọc Duy
2011-10-25 19:24   ` Junio C Hamano
2011-10-27 18:36   ` Junio C Hamano
2011-10-30  9:17     ` Nguyen Thai Ngoc Duy
2011-10-24  6:36 ` [PATCH/WIP 06/11] read_directory_recursive: reduce one indentation level Nguyễn Thái Ngọc Duy
2011-10-24  6:36 ` [PATCH/WIP 07/11] tree_entry_interesting: make use of local pointer "item" Nguyễn Thái Ngọc Duy
2011-10-24  6:36 ` [PATCH/WIP 08/11] tree-walk: mark useful pathspecs Nguyễn Thái Ngọc Duy
2011-10-24  6:36 ` [PATCH/WIP 09/11] tree_entry_interesting: differentiate partial vs full match Nguyễn Thái Ngọc Duy
2011-10-24  6:36 ` [PATCH/WIP 10/11] read-dir: stop using path_simplify code in favor of tree_entry_interesting() Nguyễn Thái Ngọc Duy
2011-10-24  6:36 ` [PATCH/WIP 11/11] dir.c: remove dead code after read_directory() rewrite Nguyễn Thái Ngọc Duy
2011-10-24 17:10 ` [PATCH/WIP 00/11] read_directory() rewrite to support struct pathspec Junio C Hamano
  -- strict thread matches above, loose matches on Subject: below --
2012-03-11 17:17 problem with merging notes David Bremner
2012-03-12 14:57 ` [PATCH 1/2] t3310: Add testcase demonstrating failure to --commit from within another dir Johan Herland
2012-03-12 14:57   ` [PATCH 2/2] notes-merge: use opendir/readdir instead of using read_directory() Johan Herland

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1331563647-1909-2-git-send-email-johan@herland.net \
    --to=johan@herland.net \
    --cc=david@tethera.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).