git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: "Dennis Kaarsemaker" <dennis@kaarsemaker.net>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 05/16] diff: handle --no-index prefixes consistently
Date: Mon, 12 Sep 2016 20:23:32 -0700	[thread overview]
Message-ID: <20160913032331.ozo34zfavzoeszj4@sigill.intra.peff.net> (raw)
In-Reply-To: <20160913032242.coyuhyhn6uklewuk@sigill.intra.peff.net>

If we see an explicit "git diff --no-index ../foo ../bar",
then we do not set up the git repository at all (we already
know we are in --no-index mode, so do not have to check "are
we in a repository?"), and hence have no "prefix" within the
repository. A patch generated by this command will have the
filenames "a/../foo" and "b/../bar", no matter which
directory we are in with respect to any repository.

However, in the implicit case, where we notice that the
files are outside the repository, we will have chdir()'d to
the top-level of the repository. We then feed the prefix
back to the diff machinery. As a result, running the same
diff from a subdirectory will result in paths that look like
"a/subdir/../../foo".

Besides being unnecessarily long, this may also be confusing
to the user: they don't care about the subdir or the
repository at all; it's just where they happened to be when
running the command. We should treat this the same as the
explicit --no-index case.

One way to address this would be to chdir() back to the
original path before running our diff. However, that's a bit
hacky, as we would also need to adjust $GIT_DIR, which could
be a relative path from our top-level.

Instead, we can reuse the diff machinery's RELATIVE_NAME
option, which automatically strips off the prefix. Note that
this _also_ restricts the diff to this relative prefix, but
that's OK for our purposes: we queue our own diff pairs
manually, and do not rely on that part of the diff code.

Signed-off-by: Jeff King <peff@peff.net>
---
 diff-no-index.c          |  3 +++
 t/t4053-diff-no-index.sh | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/diff-no-index.c b/diff-no-index.c
index 1f8999b..f420786 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -281,6 +281,9 @@ void diff_no_index(struct rev_info *revs,
 
 	DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
 
+	DIFF_OPT_SET(&revs->diffopt, RELATIVE_NAME);
+	revs->diffopt.prefix = prefix;
+
 	revs->max_count = -2;
 	diff_setup_done(&revs->diffopt);
 
diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
index 6eb8321..e60c951 100755
--- a/t/t4053-diff-no-index.sh
+++ b/t/t4053-diff-no-index.sh
@@ -89,4 +89,22 @@ test_expect_success 'turning a file into a directory' '
 	)
 '
 
+test_expect_success 'diff from repo subdir shows real paths (explicit)' '
+	echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
+	test_expect_code 1 \
+		git -C repo/sub \
+		diff --no-index ../../non/git/a ../../non/git/b >actual &&
+	head -n 1 <actual >actual.head &&
+	test_cmp expect actual.head
+'
+
+test_expect_success 'diff from repo subdir shows real paths (implicit)' '
+	echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
+	test_expect_code 1 \
+		git -C repo/sub \
+		diff ../../non/git/a ../../non/git/b >actual &&
+	head -n 1 <actual >actual.head &&
+	test_cmp expect actual.head
+'
+
 test_done
-- 
2.10.0.230.g6f8d04b


  parent reply	other threads:[~2016-09-13  3:23 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-13  3:22 [PATCH 0/16] fix config-reading in non-repos Jeff King
2016-09-13  3:23 ` [PATCH 01/16] t1007: factor out repeated setup Jeff King
2016-09-13 21:42   ` Stefan Beller
2016-09-13  3:23 ` [PATCH 02/16] hash-object: always try to set up the git repository Jeff King
2016-09-13  3:23 ` [PATCH 03/16] patch-id: use RUN_SETUP_GENTLY Jeff King
2016-09-13  3:23 ` [PATCH 04/16] diff: skip implicit no-index check when given --no-index Jeff King
2016-09-13  3:23 ` Jeff King [this message]
2016-09-13  3:23 ` [PATCH 06/16] diff: always try to set up the repository Jeff King
2016-09-13 22:00   ` Stefan Beller
2016-09-13 22:22     ` Jeff King
2016-09-13  3:23 ` [PATCH 07/16] pager: remove obsolete comment Jeff King
2016-09-13  3:23 ` [PATCH 08/16] pager: stop loading git_default_config() Jeff King
2016-09-13  3:23 ` [PATCH 09/16] pager: make pager_program a file-local static Jeff King
2016-09-13  3:23 ` [PATCH 10/16] pager: use callbacks instead of configset Jeff King
2016-09-13  3:23 ` [PATCH 11/16] pager: handle early config Jeff King
2016-09-13  3:24 ` [PATCH 12/16] t1302: use "git -C" Jeff King
2016-09-13  3:24 ` [PATCH 13/16] test-config: setup git directory Jeff King
2016-09-13  3:24 ` [PATCH 14/16] config: only read .git/config from configured repos Jeff King
2016-09-13  3:24 ` [PATCH 15/16] init: expand comments explaining config trickery Jeff King
2016-09-13  3:24 ` [PATCH 16/16] init: reset cached config when entering new repo Jeff King
2016-09-13 22:18   ` Stefan Beller
2016-09-14 10:55 ` [PATCH 0/16] fix config-reading in non-repos Dennis Kaarsemaker
2016-09-14 15:31   ` Jeff King

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20160913032331.ozo34zfavzoeszj4@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=dennis@kaarsemaker.net \
    --cc=git@vger.kernel.org \
    --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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).