git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH/RFC] get_sha1: prefer 40-hex ref name over 40-hex SHA-1
@ 2013-05-01  3:01 Nguyễn Thái Ngọc Duy
  2013-05-01  3:03 ` Eric Sunshine
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2013-05-01  3:01 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

The current behavior is inconsistent when passing SHA-1 to get_sha1.
If it's a short sha-1, refs take precedence. "git rev-parse 1234" will
resolve refs/heads/1234 if exists even if there is an unambiguous
SHA-1 starting with 1234. However if it's full SHA-1, the SHA-1 takes
precedence and refs with the same name are ignored.

The former makes more sense than the latter. This patch makes git
check for 40-hex ref names before consider it SHA-1. In future, we may
want to warn ambiguity between refs and SHA-1 (for both full and short
SHA-1).

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Some gray area where my "makes more sense" may not actually be common
 sense.

 sha1_name.c                         | 13 +++++++++++--
 t/t1512-rev-parse-disambiguation.sh | 15 +++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/sha1_name.c b/sha1_name.c
index 3820f28..faf10b4 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -436,11 +436,20 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
 {
 	static const char *warn_msg = "refname '%.*s' is ambiguous.";
 	char *real_ref = NULL;
-	int refs_found = 0;
+	int refs_found;
 	int at, reflog_len;
 
-	if (len == 40 && !get_sha1_hex(str, sha1))
+	if (len == 40 && !get_sha1_hex(str, sha1)) {
+		unsigned char ref_sha1[20];
+		refs_found = dwim_ref(str, len, ref_sha1, &real_ref);
+		if (refs_found > 0) {
+			if (warn_ambiguous_refs && refs_found > 1)
+				warning(warn_msg, len, str);
+			hashcpy(sha1, ref_sha1);
+		}
+		free(real_ref);
 		return 0;
+	}
 
 	/* basic@{time or number or -number} format to query ref-log */
 	reflog_len = at = 0;
diff --git a/t/t1512-rev-parse-disambiguation.sh b/t/t1512-rev-parse-disambiguation.sh
index 6b3d797..97ff8ac 100755
--- a/t/t1512-rev-parse-disambiguation.sh
+++ b/t/t1512-rev-parse-disambiguation.sh
@@ -261,4 +261,19 @@ test_expect_success 'rev-parse --disambiguate' '
 	test "$(sed -e "s/^\(.........\).*/\1/" actual | sort -u)" = 000000000
 '
 
+test_expect_success 'rev-parse 20-hex ref' '
+	REF=`git rev-parse HEAD` &&
+	VAL=`echo| git commit-tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904` &&
+	git update-ref refs/heads/$REF $VAL &&
+	test `git rev-parse $REF` = $VAL
+'
+
+test_expect_success 'rev-parse ambiguous 20-hex ref' '
+	REF=`git rev-parse HEAD` &&
+	VAL=`echo| git commit-tree -p HEAD 4b825dc642cb6eb9a060e54bf8d69288fbee4904` &&
+	git update-ref refs/tags/$REF $VAL &&
+	test `git rev-parse $REF 2>err` = $VAL &&
+	grep "refname.*ambiguous" err
+'
+
 test_done
-- 
1.8.2.83.gc99314b

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

end of thread, other threads:[~2013-05-29 12:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-01  3:01 [PATCH/RFC] get_sha1: prefer 40-hex ref name over 40-hex SHA-1 Nguyễn Thái Ngọc Duy
2013-05-01  3:03 ` Eric Sunshine
2013-05-01 18:43 ` Jonathan Nieder
2013-05-02 10:09   ` Duy Nguyen
2013-05-04  3:45   ` [PATCH] get_sha1: improve ambiguity warning regarding SHA-1 and ref names Nguyễn Thái Ngọc Duy
2013-05-07 16:09     ` Junio C Hamano
2013-05-29 12:12     ` [PATCH v2] get_sha1: warn about full or short object names that look like refs Nguyễn Thái Ngọc Duy
2013-05-02  8:55 ` [PATCH/RFC] get_sha1: prefer 40-hex ref name over 40-hex SHA-1 Thomas Rast

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