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

From: Junio C Hamano <[email-masked]>
Date: Tue, 18 Feb 2014 13:01:22 -0800

Thomas Rast <[email-masked]> writes:

> 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 <[email-masked]>
> Signed-off-by: Thomas Rast <[email-masked]>
> ---
>  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