git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH/WIP 02/11] notes-merge: use opendir/readdir instead of using read_directory()

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 25, 2011, 19:27 UTC
Message-ID
<7vzkgo3m9b.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1319438176-7304-3-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 6 quoted lines
> 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.

Technically isn't the directory structure this codepath looks at a working tree that has extract of a notes tree commit?

Looking at the result of the patch I do not have strong opinions either way, though. It isn't like we care about gitignore or attributes rules in the notes tree, so using read_directory() does feel like an overkill.

Show 84 quoted lines
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  notes-merge.c |   45 +++++++++++++++++++++++++++------------------
>  1 files changed, 27 insertions(+), 18 deletions(-)
>
> diff --git a/notes-merge.c b/notes-merge.c
> index e9e4199..80d64a2 100644
> --- a/notes-merge.c
> +++ b/notes-merge.c
> @@ -680,48 +680,57 @@ int notes_merge_commit(struct notes_merge_options *o,
>  	 * 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;
>  	const char *msg = strstr(partial_commit->buffer, "\n\n");
> +	int baselen;
>  
> -	OUTPUT(o, 3, "Committing notes in notes merge worktree at %.*s",
> -	       path_len - 1, path);
> +	strbuf_addstr(&path, git_path(NOTES_MERGE_WORKTREE));
> +	OUTPUT(o, 3, "Committing notes in notes merge worktree at %s", 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)) {
> -			OUTPUT(o, 3, "Skipping non-SHA1 entry '%s'", ent->name);
> +		if (is_dot_or_dotdot(e->d_name))
> +			continue;
> +
> +		if (strlen(e->d_name) != 40 || get_sha1_hex(e->d_name, obj_sha1)) {
> +			OUTPUT(o, 3, "Skipping non-SHA1 entry '%s%s'", 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);
>  		OUTPUT(o, 4, "Added resolved note for object %s: %s",
>  		       sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1));
> +		strbuf_setlen(&path, baselen);
>  	}
>  
>  	create_notes_commit(partial_tree, partial_commit->parents, msg,
>  			    result_sha1);
>  	OUTPUT(o, 4, "Finalized notes merge commit: %s",
>  	       sha1_to_hex(result_sha1));
> -	free(path);
> +	strbuf_release(&path);
> +	closedir(dir);
>  	return 0;
>  }
Previous: Nguyễn Thái Ngọc DuyNext: Nguyen Thai Ngoc Duy
Message 6 of 50 in “read_directory() rewrite to support struct pathspec”
  1. 00/11 read_directory() rewrite to support struct pathspecNguyễn Thái Ngọc Duy, Oct 24, 2011
  2. 01/11 Introduce "check-attr --excluded" as a replacement for "add --ignore-missing"Nguyễn Thái Ngọc Duy, Oct 24, 2011
  3. Junio C HamanoOct 27, 2011
  4. Nguyen Thai Ngoc DuyOct 28, 2011
  5. 02/11 notes-merge: use opendir/readdir instead of using read_directory()Nguyễn Thái Ngọc Duy, Oct 24, 2011
  6. Junio C HamanoOct 25, 2011
  7. Nguyen Thai Ngoc DuyOct 26, 2011
  8. Junio C HamanoOct 26, 2011
  9. Nguyen Thai Ngoc DuyOct 27, 2011
  10. Junio C HamanoOct 27, 2011
  11. Nguyen Thai Ngoc DuyOct 28, 2011
  12. 1/2 t3310: Add testcase demonstrating failure to --commit from within another dirJohan Herland, Mar 12, 2012
  13. 2/2 notes-merge: use opendir/readdir instead of using read_directory()Johan Herland, Mar 12, 2012
  14. Nguyen Thai Ngoc DuyMar 12, 2012
  15. fixup! t3310 on WindowsJohannes Sixt, Mar 14, 2012
  16. Johan HerlandMar 14, 2012
  17. Johannes SixtMar 14, 2012
  18. David BremnerMar 14, 2012
  19. Johan HerlandMar 14, 2012
  20. Junio C HamanoMar 14, 2012
  21. 3/2 notes-merge: Don't remove .git/NOTES_MERGE_WORKTREE; it may be the user's cwdJohan Herland, Mar 14, 2012
  22. Junio C HamanoMar 15, 2012
  23. Junio C HamanoMar 15, 2012
  24. Johan HerlandMar 15, 2012
  25. Re* [PATCH 3/2] notes-merge: Don't remove .git/NOTES_MERGE_WORKTREE; it may be the user's cwdJunio C Hamano, Mar 15, 2012
  26. Junio C HamanoMar 15, 2012
  27. Johannes SixtMar 15, 2012
  28. 03/11 t5403: avoid doing "git add foo/bar" where foo/.git existsNguyễn Thái Ngọc Duy, Oct 24, 2011
  29. Junio C HamanoOct 25, 2011
  30. Nguyen Thai Ngoc DuyOct 26, 2011
  31. Junio C HamanoOct 26, 2011
  32. Nguyen Thai Ngoc DuyOct 27, 2011
  33. Junio C HamanoOct 27, 2011
  34. Nguyen Thai Ngoc DuyOct 30, 2011
  35. Junio C HamanoOct 30, 2011
  36. Nguyen Thai Ngoc DuyOct 30, 2011
  37. Junio C HamanoOct 30, 2011
  38. 04/11 tree-walk.c: do not leak internal structure in tree_entry_len()Nguyễn Thái Ngọc Duy, Oct 24, 2011
  39. Junio C HamanoOct 25, 2011
  40. 05/11 symbolize return values of tree_entry_interesting()Nguyễn Thái Ngọc Duy, Oct 24, 2011
  41. Junio C HamanoOct 25, 2011
  42. Junio C HamanoOct 27, 2011
  43. Nguyen Thai Ngoc DuyOct 30, 2011
  44. 06/11 read_directory_recursive: reduce one indentation levelNguyễn Thái Ngọc Duy, Oct 24, 2011
  45. 07/11 tree_entry_interesting: make use of local pointer "item"Nguyễn Thái Ngọc Duy, Oct 24, 2011
  46. 08/11 tree-walk: mark useful pathspecsNguyễn Thái Ngọc Duy, Oct 24, 2011
  47. 09/11 tree_entry_interesting: differentiate partial vs full matchNguyễn Thái Ngọc Duy, Oct 24, 2011
  48. 10/11 read-dir: stop using path_simplify code in favor of tree_entry_interesting()Nguyễn Thái Ngọc Duy, Oct 24, 2011
  49. 11/11 dir.c: remove dead code after read_directory() rewriteNguyễn Thái Ngọc Duy, Oct 24, 2011
  50. Junio C HamanoOct 24, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.