git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] diff: --no-index should ignore the worktree
@ 2025-08-10  0:20 Junio C Hamano
  2025-08-12 14:37 ` opensource
  2025-08-15 22:01 ` [PATCH] diff: simplify --no-index taking advantage of the fact that prefix==NULL Junio C Hamano
  0 siblings, 2 replies; 4+ messages in thread
From: Junio C Hamano @ 2025-08-10  0:20 UTC (permalink / raw)
  To: git; +Cc: Ramsay Jones, Gregoire Geis

The act of giving "--no-index" tells Git to pretend that the current
directory is not under control of any Git index or repository, so
even when you happen to be in a Git controlled working tree, where
in that working tree should not matter.

But the start-up sequence tries to discover the top of the working
tree and chdir(2)'s there, even before Git passes control to the
subcommand being run.  When diff_no_index() starts running, it
starts at a wrong (from the end-user's point of view who thinks "git
diff --no-index" is merely a better version of GNU diff) directory,
and the original directory the user started the command is at
"prefix".

Because the paths given from argv[] have already been adjusted to
account for this path shuffling by prepending the prefix, and
showing the resulting path by stripping the prefix, the effect of
these nonsense operations (nonsense in the context of "--no-index",
that is) is usually not observable.

Except for special cases like "-", where it is not preprocessed by
prepending the prefix.

Instead of papering over by adding more special cases only to cater
to the no-index codepath in the generic code, drive the diff
machinery more faithfully to what is going on.  If the user started
"git diff --no-index" in directory X/Y/Z in a working tree
controlled by Git, and the start up sequence of Git chdir(2)'ed up
to directory X and left Y/Z in the prefix, revert the effect of the
start up sequence by chdir'ing back to Y/Z and emptying the prefix.

Reported-by: Gregoire Geis <opensource@gregoirege.is>
Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/diff.c           | 15 +++++++++++++++
 t/t4053-diff-no-index.sh | 17 +++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/builtin/diff.c b/builtin/diff.c
index 9a89e25a98..0b23c41456 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -487,6 +487,21 @@ int cmd_diff(int argc,
 
 	init_diff_ui_defaults();
 	repo_config(the_repository, git_diff_ui_config, NULL);
+
+	/*
+	 * If we are ignoring the fact that our current directory may
+	 * be part of a working tree controlled by a Git repository to
+	 * pretend to be a "better GNU diff", we should undo the
+	 * effect of the setup code that did a chdir() to the top of
+	 * the working tree.  Where we came from is recorded in the
+	 * prefix.
+	 */
+	if (no_index && prefix) {
+		if (chdir(prefix))
+			die(_("cannot come back to cwd"));
+		prefix = NULL;
+	}
+
 	prefix = precompose_argv_prefix(argc, argv, prefix);
 
 	repo_init_revisions(the_repository, &rev, prefix);
diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
index 01db9243ab..44b4b13f5d 100755
--- a/t/t4053-diff-no-index.sh
+++ b/t/t4053-diff-no-index.sh
@@ -26,6 +26,23 @@ test_expect_success 'git diff --no-index directories' '
 	test_line_count = 14 cnt
 '
 
+test_expect_success 'git diff --no-index with -' '
+	cat >expect <<-\EOF &&
+	diff --git a/- b/-
+	new file mode 100644
+	--- /dev/null
+	+++ b/-
+	@@ -0,0 +1 @@
+	+frotz
+	EOF
+	(
+		cd a &&
+		echo frotz |
+		test_expect_code 1 git diff --no-index /dev/null - >../actual
+	) &&
+	test_cmp expect actual
+'
+
 test_expect_success 'git diff --no-index relative path outside repo' '
 	(
 		cd repo &&

Range-diff against v1:
1:  456a265746 ! 1:  4166ccb163 diff: --no-index should ignore the worktree
    @@ builtin/diff.c: int cmd_diff(int argc,
     +	 * prefix.
     +	 */
     +	if (no_index && prefix) {
    -+		chdir(prefix);
    ++		if (chdir(prefix))
    ++			die(_("cannot come back to cwd"));
     +		prefix = NULL;
     +	}
     +
-- 
2.51.0-rc1-130-g831cc762e6


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] diff: --no-index should ignore the worktree
  2025-08-10  0:20 [PATCH v2] diff: --no-index should ignore the worktree Junio C Hamano
@ 2025-08-12 14:37 ` opensource
  2025-08-12 15:07   ` Junio C Hamano
  2025-08-15 22:01 ` [PATCH] diff: simplify --no-index taking advantage of the fact that prefix==NULL Junio C Hamano
  1 sibling, 1 reply; 4+ messages in thread
From: opensource @ 2025-08-12 14:37 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Ramsay Jones

Hey,

I don't know how the review process goes at this point as you took
over the patch. The handling of the chdir failure in v2 LGTM. I ran
the t*-diff*.sh tests again and tried a couple of commands, and
everything seems to work.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] diff: --no-index should ignore the worktree
  2025-08-12 14:37 ` opensource
@ 2025-08-12 15:07   ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2025-08-12 15:07 UTC (permalink / raw)
  To: opensource; +Cc: git, Ramsay Jones

opensource@gregoirege.is writes:

> I don't know how the review process goes at this point as you took
> over the patch. The handling of the chdir failure in v2 LGTM. I ran
> the t*-diff*.sh tests again and tried a couple of commands, and
> everything seems to work.

Thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] diff: simplify --no-index taking advantage of the fact that prefix==NULL
  2025-08-10  0:20 [PATCH v2] diff: --no-index should ignore the worktree Junio C Hamano
  2025-08-12 14:37 ` opensource
@ 2025-08-15 22:01 ` Junio C Hamano
  1 sibling, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2025-08-15 22:01 UTC (permalink / raw)
  To: git; +Cc: Ramsay Jones, Gregoire Geis

Since the caller of the diff_no_index() function now undoes the
effect of setup.c:setup_git_directory_gently(), i.e. figuring out
where in the working tree the cwd of the process is, recording the
cwd relative to that top directory in the prefix, and chdir()ing up
to the top directory, we are now guaranteed to always have NULL in
the prefix.  When started outside a repository, prefix would already
be NULL.

Take advantage of this fact to simplify the implementation.

 - The code that conditionally calls prefix_filename(), except for
   "-" given from the command line, can go away, as there is nothing
   to prefix.

 - The temporary variable that points at the memory allocated by
   prefix_filename() for later freeing can go; so can the loop to
   free them.

 - As the prefix_filename() loop that had to special case "-" is
   gone, we do not rewrite "-" to another constant address that has
   the same string, file_from_standard_input[].  Teach get_mode()
   function to strcmp() between the path(s) and "-" directly for
   simplicity.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 diff-no-index.c | 40 ++++++++++++++--------------------------
 1 file changed, 14 insertions(+), 26 deletions(-)

diff --git a/diff-no-index.c b/diff-no-index.c
index 88ae4cee56..8bdd0021b1 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -71,9 +71,8 @@ static int read_directory_contents(const char *path, struct string_list *list,
 }
 
 /*
- * This should be "(standard input)" or something, but it will
- * probably expose many more breakages in the way no-index code
- * is bolted onto the diff callchain.
+ * stdin should be spelled as "-"; if you have path that is "-",
+ * spell it as "./-".
  */
 static const char file_from_standard_input[] = "-";
 
@@ -97,7 +96,7 @@ static int get_mode(const char *path, int *mode, enum special *special)
 	} else if (!strcasecmp(path, "nul")) {
 		*mode = 0;
 #endif
-	} else if (path == file_from_standard_input) {
+	} else if (!strcmp(path, file_from_standard_input)) {
 		*mode = create_ce_mode(0666);
 		*special = SPECIAL_STDIN;
 	} else if (lstat(path, &st)) {
@@ -305,18 +304,18 @@ static int fixup_paths(const char **path, struct strbuf *replacement)
 	unsigned int isdir0 = 0, isdir1 = 0;
 	unsigned int ispipe0 = 0, ispipe1 = 0;
 
-	if (path[0] != file_from_standard_input && !stat(path[0], &st)) {
+	if (strcmp(path[0], file_from_standard_input) && !stat(path[0], &st)) {
 		isdir0 = S_ISDIR(st.st_mode);
 		ispipe0 = S_ISFIFO(st.st_mode);
 	}
 
-	if (path[1] != file_from_standard_input && !stat(path[1], &st)) {
+	if (strcmp(path[1], file_from_standard_input) && !stat(path[1], &st)) {
 		isdir1 = S_ISDIR(st.st_mode);
 		ispipe1 = S_ISFIFO(st.st_mode);
 	}
 
-	if ((path[0] == file_from_standard_input && isdir1) ||
-	    (isdir0 && path[1] == file_from_standard_input))
+	if ((isdir1 && !strcmp(path[0], file_from_standard_input)) ||
+	    (isdir0 && !strcmp(path[1], file_from_standard_input)))
 		die(_("cannot compare stdin to a directory"));
 
 	if ((isdir0 && ispipe1) || (ispipe0 && isdir1))
@@ -349,9 +348,7 @@ int diff_no_index(struct rev_info *revs, const struct git_hash_algo *algop,
 	int i, no_index, skip1 = 0, skip2 = 0;
 	int ret = 1;
 	const char *paths[2];
-	char *to_free[ARRAY_SIZE(paths)] = { 0 };
 	struct strbuf replacement = STRBUF_INIT;
-	const char *prefix = revs->prefix;
 	struct option no_index_options[] = {
 		OPT_BOOL_F(0, "no-index", &no_index, "",
 			   PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
@@ -359,8 +356,11 @@ int diff_no_index(struct rev_info *revs, const struct git_hash_algo *algop,
 	};
 	struct option *options;
 
+	if (revs->prefix)
+		BUG("caller of diff_no_index() forgot to undo setup");
+
 	options = add_diff_options(no_index_options, &revs->diffopt);
-	argc = parse_options(argc, argv, revs->prefix, options,
+	argc = parse_options(argc, argv, NULL, options,
 			     diff_no_index_usage, 0);
 	if (argc < 2) {
 		if (implicit_no_index)
@@ -368,18 +368,8 @@ int diff_no_index(struct rev_info *revs, const struct git_hash_algo *algop,
 				  "compare two paths outside a working tree"));
 		usage_with_options(diff_no_index_usage, options);
 	}
-	for (i = 0; i < 2; i++) {
-		const char *p = argv[i];
-		if (!strcmp(p, "-"))
-			/*
-			 * stdin should be spelled as "-"; if you have
-			 * path that is "-", spell it as "./-".
-			 */
-			p = file_from_standard_input;
-		else if (prefix)
-			p = to_free[i] = prefix_filename(prefix, p);
-		paths[i] = p;
-	}
+	for (i = 0; i < 2; i++)
+		paths[i] = argv[i];
 
 	if (fixup_paths(paths, &replacement)) {
 		parse_pathspec(&pathspec, PATHSPEC_FROMTOP | PATHSPEC_ATTR,
@@ -406,7 +396,7 @@ int diff_no_index(struct rev_info *revs, const struct git_hash_algo *algop,
 	revs->diffopt.flags.no_index = 1;
 
 	revs->diffopt.flags.relative_name = 1;
-	revs->diffopt.prefix = prefix;
+	revs->diffopt.prefix = NULL;
 
 	revs->max_count = -2;
 	diff_setup_done(&revs->diffopt);
@@ -428,8 +418,6 @@ int diff_no_index(struct rev_info *revs, const struct git_hash_algo *algop,
 	ret = diff_result_code(revs);
 
 out:
-	for (i = 0; i < ARRAY_SIZE(to_free); i++)
-		free(to_free[i]);
 	strbuf_release(&replacement);
 	if (ps)
 		clear_pathspec(ps);
-- 
2.51.0-rc2-158-gf97fc618fa


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-08-15 22:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-10  0:20 [PATCH v2] diff: --no-index should ignore the worktree Junio C Hamano
2025-08-12 14:37 ` opensource
2025-08-12 15:07   ` Junio C Hamano
2025-08-15 22:01 ` [PATCH] diff: simplify --no-index taking advantage of the fact that prefix==NULL Junio C Hamano

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