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

Re: [PATCH] diff: do not reuse_worktree_file for submodules

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 18, 2014, 21:01 UTC
Message-ID
<xmqqy518cezh.fsf@gitster.dls.corp.google.com>
In-Reply-To
<d08b7e5a36ee13226d1ad56a731016762ae89938.1392569505.git.tr@thomasrast.ch>
Thomas Rast <tr@thomasrast.ch> writes:
Show 29 quoted lines
> The GIT_EXTERNAL_DIFF calling code attempts to reuse existing worktree
> files for the worktree side of diffs, for performance reasons.
> However, that code also tries to do the same with submodules.  This
> results in calls to $GIT_EXTERNAL_DIFF where the old-file is a file of
> the form "Submodule commit $sha1", but the new-file is a directory in
> the worktree.
>
> Fix it by never reusing a worktree "file" in the submodule case.
>
> Reported-by: Grégory Pakosz <gregory.pakosz@gmail.com>
> Signed-off-by: Thomas Rast <tr@thomasrast.ch>
> ---
>  diff.c                   |  5 +++--
>  t/t4020-diff-external.sh | 30 +++++++++++++++++++++++++++++-
>  2 files changed, 32 insertions(+), 3 deletions(-)
>
> diff --git a/diff.c b/diff.c
> index 7c59bfe..e9a8874 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -2845,8 +2845,9 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
>  		remove_tempfile_installed = 1;
>  	}
>  
> -	if (!one->sha1_valid ||
> -	    reuse_worktree_file(name, one->sha1, 1)) {
> +	if (!S_ISGITLINK(one->mode) &&
> +	    (!one->sha1_valid ||
> +	     reuse_worktree_file(name, one->sha1, 1))) {

I agree with the goal/end result, but I have to wonder if the reuse_worktree_file() be the helper function that ought to encapsulate such a logic?

Instead of feeding it an object name and a path, if we passed a diff_filespec to the helper, it would have access to the mode as well. It would result in a more intrusive change, so I'd prefer to see your patch applied first and then build such a refactor on top, perhaps like the attached.

 diff.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/diff.c b/diff.c
index a96992a..74eec80 100644
--- a/diff.c
+++ b/diff.c
@@ -2582,11 +2582,13 @@ void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
  * the work tree has that object contents, return true, so that
  * prepare_temp_file() does not have to inflate and extract.
  */
-static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
+static int reuse_worktree_file(const struct diff_filespec *spec, int want_file)
 {
 	const struct cache_entry *ce;
 	struct stat st;
 	int pos, len;
+	const char *name = spec->path;
+	const unsigned char *sha1 = spec->sha1;
 
 	/*
 	 * We do not read the cache ourselves here, because the
@@ -2698,7 +2700,7 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
 		return diff_populate_gitlink(s, size_only);
 
 	if (!s->sha1_valid ||
-	    reuse_worktree_file(s->path, s->sha1, 0)) {
+	    reuse_worktree_file(s, 0)) {
 		struct strbuf buf = STRBUF_INIT;
 		struct stat st;
 		int fd;
@@ -2844,17 +2846,17 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
 
 	if (!S_ISGITLINK(one->mode) &&
 	    (!one->sha1_valid ||
-	     reuse_worktree_file(name, one->sha1, 1))) {
+	     reuse_worktree_file(one, 1))) {
 		struct stat st;
-		if (lstat(name, &st) < 0) {
+		if (lstat(one->path, &st) < 0) {
 			if (errno == ENOENT)
 				goto not_a_valid_file;
-			die_errno("stat(%s)", name);
+			die_errno("stat(%s)", one->path);
 		}
 		if (S_ISLNK(st.st_mode)) {
 			struct strbuf sb = STRBUF_INIT;
-			if (strbuf_readlink(&sb, name, st.st_size) < 0)
-				die_errno("readlink(%s)", name);
+			if (strbuf_readlink(&sb, one->path, st.st_size) < 0)
+				die_errno("readlink(%s)", one->path);
 			prep_temp_blob(name, temp, sb.buf, sb.len,
 				       (one->sha1_valid ?
 					one->sha1 : null_sha1),
@@ -2864,7 +2866,7 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
 		}
 		else {
 			/* we can borrow from the file in the work tree */
-			temp->name = name;
+			temp->name = one->path;
 			if (!one->sha1_valid)
 				strcpy(temp->hex, sha1_to_hex(null_sha1));
 			else
Previous: Thomas RastNext: Thomas Rast
Message 3 of 6 in “git diff, external diff tool, and submodules”
  1. Grégory PakoszFeb 15, 2014
  2. diff: do not reuse_worktree_file for submodulesThomas Rast, Feb 16, 2014
  3. Junio C HamanoFeb 18, 2014
  4. Thomas RastFeb 22, 2014
  5. Thomas RastFeb 23, 2014
  6. Junio C HamanoFeb 24, 2014

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.