Git development
 help / color / mirror / Atom feed
From: Jack Bates <bk874k@nottheoilrig.com>
To: git@vger.kernel.org
Cc: Jeff King <peff@peff.net>, Jack Bates <jack@nottheoilrig.com>
Subject: [PATCH] diff: fix up SHA-1 abbreviations outside of repository
Date: Sun,  4 Dec 2016 12:47:47 -0700	[thread overview]
Message-ID: <20161204194747.7100-1-jack@nottheoilrig.com> (raw)

The three cases where "git diff" operates outside of a repository are 1)
when we run it outside of a repository, 2) when one of the files we're
comparing is outside of the repository we're in, and 3) the --no-index
option. Commit 4f03666 ("diff: handle sha1 abbreviations outside of
repository", 2016-10-20) only worked in the first case.
---
 builtin/diff.c                  |  4 +++-
 t/t4063-diff-no-index-abbrev.sh | 50 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100755 t/t4063-diff-no-index-abbrev.sh

diff --git a/builtin/diff.c b/builtin/diff.c
index 7f91f6d..ec7c432 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -342,9 +342,11 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 		       "--no-index" : "[--no-index]");
 
 	}
-	if (no_index)
+	if (no_index) {
 		/* If this is a no-index diff, just run it and exit there. */
+		startup_info->have_repository = 0;
 		diff_no_index(&rev, argc, argv);
+	}
 
 	/* Otherwise, we are doing the usual "git" diff */
 	rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
diff --git a/t/t4063-diff-no-index-abbrev.sh b/t/t4063-diff-no-index-abbrev.sh
new file mode 100755
index 0000000..d1d6302
--- /dev/null
+++ b/t/t4063-diff-no-index-abbrev.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+test_description='don'\'' peek into .git when operating outside of a repository
+
+When abbreviating SHA-1s, if another object in the repository has the
+same abbreviation, we normally lengthen the abbreviation until it'\''s
+unique. Commit 4f03666 ("diff: handle sha1 abbreviations outside of
+repository", 2016-10-20) addressed the case of abbreviating SHA-1s
+outside the context of a repository. In that case we shouldn'\''t peek
+into a .git directory to make an abbreviation unique.
+
+To check that we don'\''t, create an blob with a SHA-1 that starts with
+0000. (Outside of a repository, SHA-1s are all zeros.) Then make an
+abbreviation and check that Git doesn'\''t lengthen it.
+
+The three cases where "git diff" operates outside of a repository are
+1) when we run it outside of a repository, 2) when one of the files
+we'\''re comparing is outside of the repository we'\''re in,
+and 3) the --no-index option.
+'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	echo 1 >a &&
+	echo 2 >b &&
+	git init repo &&
+	(
+		cd repo &&
+
+		# Create a blob
+		# 00002e907f44c3881822c473d8842405cfd96362
+		echo 119132 >collision &&
+		git add collision
+	)
+'
+
+cat >expect <<EOF
+:100644 100644 0000... 0000... M	../a
+EOF
+
+test_expect_success 'don'\''t peek into .git when operating outside of a repository' '
+	(
+		cd repo &&
+		test_must_fail git diff --raw --abbrev=4 ../a ../b >actual &&
+		test_cmp ../expect actual
+	)
+'
+
+test_done
-- 
2.10.2

             reply	other threads:[~2016-12-04 19:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-04 19:47 Jack Bates [this message]
2016-12-05  6:55 ` [PATCH] diff: fix up SHA-1 abbreviations outside of repository Jeff King
2016-12-05  7:19 ` Junio C Hamano
2016-12-05  7:26   ` Jeff King
2016-12-05 17:45     ` Jack Bates
2016-12-05 22:43     ` Junio C Hamano

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

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

  git send-email \
    --in-reply-to=20161204194747.7100-1-jack@nottheoilrig.com \
    --to=bk874k@nottheoilrig.com \
    --cc=git@vger.kernel.org \
    --cc=jack@nottheoilrig.com \
    --cc=peff@peff.net \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox