Skip to contentSkip to conversation

Detailed diagnosis when parsing an object name fails.

PATCH In review [email protected] 5 participants 9 messages Jun 15, 2016 – Aug 14, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Detailed diagnosis when parsing an object name fails.

From: Matthieu Moy <[email protected]>

The previous error message was the same in many situations (unknown revision or path not in the working tree). We try to help the user as much as possible to understand the error, especially with the sha1:filename notation. In this case, we say whether the sha1 or the filename is problematic, and diagnose the confusion between relative-to-root and relative-to-$PWD confusion precisely.

The 6 new error messages are tested.

---
Changes since v1:

* Fixed a segfault with

+ if (!prefix)
+ prefix = "";

* Added testcases.

 cache.h                        |    6 ++-
 setup.c                        |   15 +++++-
 sha1_name.c                    |   95 ++++++++++++++++++++++++++++++++++++++--
 t/t1506-rev-parse-diagnosis.sh |   67 ++++++++++++++++++++++++++++
 4 files changed, 176 insertions(+), 7 deletions(-)
 create mode 100755 t/t1506-rev-parse-diagnosis.sh
cache.h +5 -1
index 0e69384..5c8cb5f 100644
@@ -708,7 +708,11 @@ static inline unsigned int hexval(unsigned char c)
 #define DEFAULT_ABBREV 7
 
 extern int get_sha1(const char *str, unsigned char *sha1);
-extern int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode);
+static inline get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
+{
+	return get_sha1_with_mode_1(str, sha1, mode, 0, NULL);
+}
+extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int fatal, const char *prefix);
 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
 extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
 extern int read_ref(const char *filename, unsigned char *sha1);
setup.c +13 -2
index f67250b..3094e8b 100644
@@ -74,6 +74,18 @@ int check_filename(const char *prefix, const char *arg)
 	die_errno("failed to stat '%s'", arg);
 }
 
+static void NORETURN die_verify_filename(const char *prefix, const char *arg)
+{
+	unsigned char sha1[20];
+	unsigned mode;
+	/* try a detailed diagnostic ... */
+	get_sha1_with_mode_1(arg, sha1, &mode, 1, prefix);
+	/* ... or fall back the most general message. */
+	die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
+	    "Use '--' to separate paths from revisions", arg);
+
+}
+
 /*
  * Verify a filename that we got as an argument for a pathspec
  * entry. Note that a filename that begins with "-" never verifies
@@ -87,8 +99,7 @@ void verify_filename(const char *prefix, const char *arg)
 		die("bad flag '%s' used after filename", arg);
 	if (check_filename(prefix, arg))
 		return;
-	die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
-	    "Use '--' to separate paths from revisions", arg);
+	die_verify_filename(prefix, arg);
 }
 
 /*
sha1_name.c +91 -4

113 lines not on this page · raw message

t/t1506-rev-parse-diagnosis.sh new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
1.6.6.rc0.256.g6060
#1 lore raw
Quoted 5 lines · [email protected] writes:
cache.h +0 -0
index 0e69384..5c8cb5f 100644

cache.h static inline unsigned int hexval(unsigned char c) · line 715

Quoted 9 lines
cache.h +5 -1
@@ -708,7 +708,11 @@ static inline unsigned int hexval(unsigned char c)
 #define DEFAULT_ABBREV 7
 
 extern int get_sha1(const char *str, unsigned char *sha1);
-extern int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode);
+static inline get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
+{
+	return get_sha1_with_mode_1(str, sha1, mode, 0, NULL);
+}
+extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int fatal, const char *prefix);

Do I understand correctly that "fatal" here is the same as "!gently" elsewhere in the API?

Quoted 19 lines
sha1_name.c +10 -1
index 44bb62d..030e2ac 100644
@@ -804,7 +804,77 @@ int get_sha1(const char *name, unsigned char *sha1)
 	return get_sha1_with_mode(name, sha1, &unused);
 }
 
-int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
+static void diagnose_invalid_sha1_path(const char *prefix,
+				       const char *filename,
+				       const char *tree_sha1,
+				       const char *object_name)
+{
+	struct stat st;
+	unsigned char sha1[20];
+	unsigned mode;
+
+	if (!prefix)

sha1_name.c int get_sha1(const char *name, unsigned char *sha1) · line 826

Quoted 10 lines
sha1_name.c +10 -0
+		prefix = "";
+
+	if (!lstat(filename, &st))
+		die("Path '%s' exists on disk, but not in '%s'.",
+		    filename, object_name);
+	if (errno == ENOENT || errno == ENOTDIR) {
+		char *fullname = malloc(strlen(filename)
+					     + strlen(prefix) + 1);
+		strcpy(fullname, prefix);
+		strcat(fullname, filename);

What if malloc fails here (and elsewhere in your patch)?

Quoted 32 lines
sha1_name.c +32 -0
+		if (!get_tree_entry(tree_sha1, fullname,
+				    sha1, &mode)) {
+			die("Path '%s' exists, but not '%s'.\n"
+			    "Did you mean '%s:%s'?",
+			    fullname,
+			    filename,
+			    object_name,
+			    fullname);
+		}
+		die("Path '%s' does not exist in '%s'",
+		    filename, object_name);
+	}
+}
+
+static void diagnose_invalid_index_path(int stage,
+					const char *prefix,
+					const char *filename)
+{
+	struct stat st;
+
+	if (!prefix)
+		prefix = "";
+
+	if (!lstat(filename, &st))
+		die("Path '%s' exists on disk, but not in the index.", filename);
+	if (errno == ENOENT || errno == ENOTDIR) {
+		struct cache_entry *ce;
+		int pos;
+		int namelen = strlen(filename) + strlen(prefix);
+		char *fullname = malloc(namelen + 1);
+		strcpy(fullname, prefix);
+		strcat(fullname, filename);

sha1_name.c int get_sha1(const char *name, unsigned char *sha1) · line 869

Quoted 10 lines
sha1_name.c +10 -0
+		pos = cache_name_pos(fullname, namelen);
+		if (pos < 0)
+			pos = -pos - 1;
+		ce = active_cache[pos];
+		if (ce_namelen(ce) == namelen &&
+		    !memcmp(ce->name, fullname, namelen))
+			die("Path '%s' is in the index, but not '%s'.\n"
+			    "Did you mean ':%d:%s'?",
+			    fullname, filename,
+			    stage, fullname);

What happens if the user asked for ":2:Makefile" while in directory "t/", and there is ":1:t/Makefile" but not ":2:t/Makefile" in the index?

What should happen if the user asked for ":2:t/Makefile" in such a case?

Quoted 10 lines
sha1_name.c +2 -0
@@ -850,6 +920,8 @@ int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
 			}
 			pos++;
 		}
+		if (fatal)
+			diagnose_invalid_index_path(stage, prefix, cp);
 		return -1;
 	}
 	for (cp = name, bracket_depth = 0; *cp; cp++) {

sha1_name.c int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode) · line 939

Quoted 9 lines
sha1_name.c +3 -3
@@ -862,9 +934,24 @@ int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
 	}
 	if (*cp == ':') {
 		unsigned char tree_sha1[20];
-		if (!get_sha1_1(name, cp-name, tree_sha1))
-			return get_tree_entry(tree_sha1, cp+1, sha1,
-					      mode);
+		char *object_name;
+		if (fatal) {
+			object_name = malloc(cp-name+1);

Where is this freed?

Instead of doing a leaky allocation, it may make sense to pass the tree object name as <const char *, size_t> pair, and print it with "%.*s" in the error reporting codepath. After all, object_name is used only for that purpose in diagnose_invalid_sha1_path(), no?

Quoted 48 lines
sha1_name.c +15 -0
+			strncpy(object_name, name, cp-name);
+			object_name[cp-name] = '\0';
+		}
+		if (!get_sha1_1(name, cp-name, tree_sha1)) {
+			const char *filename = cp+1;
+			ret = get_tree_entry(tree_sha1, filename, sha1, mode);
+			if (fatal)
+				diagnose_invalid_sha1_path(prefix, filename,
+							   tree_sha1, object_name);
+
+			return ret;
+		} else {
+			if (fatal)
+				die("Invalid object name '%s'.", object_name);
+		}
 	}
 	return ret;
 }
t/t1506-rev-parse-diagnosis.sh new file +24 -0
new file mode 100755
index 0000000..8112d56
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+test_description='test git rev-parse diagnosis for invalid argument'
+
+exec </dev/null
+
+. ./test-lib.sh
+
+HASH_file=
+
+test_expect_success 'set up basic repo' '
+	echo one > file.txt &&
+	mkdir subdir &&
+	echo two > subdir/file.txt &&
+	echo three > subdir/file2.txt &&
+	git add . &&
+	git commit -m init &&
+	echo four > index-only.txt &&
+	git add index-only.txt &&
+	echo five > disk-only.txt
+'
+
+test_expect_success 'correct file objects' '
+	HASH_file=$(git rev-parse HEAD:file.txt) &&

t/t1506-rev-parse-diagnosis.sh line 34

Quoted 10 lines
t/t1506-rev-parse-diagnosis.sh new file +10 -0
+	git rev-parse HEAD:subdir/file.txt &&
+	git rev-parse :index-only.txt &&
+	cd subdir &&
+	git rev-parse HEAD:file.txt &&
+	git rev-parse HEAD:subdir/file2.txt &&
+	test $HASH_file = $(git rev-parse HEAD:file.txt) &&
+	test $HASH_file = $(git rev-parse :file.txt) &&
+	test $HASH_file = $(git rev-parse :0:file.txt) &&
+	cd ..
+'

Please make it a habit of not doing "cd" without forcing a subprocess using (). If 'rev-parse HEAD:file.txt' fails after "cd subdir", the next test will start running from that directory.

t/t1506-rev-parse-diagnosis.sh line 38

Quoted 3 lines
t/t1506-rev-parse-diagnosis.sh new file +3 -0
+test_expect_success 'incorrect revision id' '
+	test_must_fail git rev-parse foobar:file.txt 2>&1 |
+		grep "Invalid object name '"'"'foobar'"'"'." &&

It always is better to write this in separate steps, because exit status of the upstream of pipe is discarded by the shell.

If you expect an error exit and want to make sure a particular error message is given, do this:

	test_must_fail git rev-parse foobar:file.txt 2>error &&
        grep "Invalid ..." error 

If you expect an error exit and want to make sure an incorrect error message is not produced, do this:

	test_must_fail git rev-parse foobar:file.txt 2>error &&
        ! grep "Invalid ..." error 
#2 lore raw
Quoted 1 line · Junio C Hamano

[email protected] writes:

(sorry for the bad email, my fingers tried to anwser "yes" when git-send-email asked me to confirm my address)

Quoted 7 lines
Quoted 7 lines
cache.h +0 -0
index 0e69384..5c8cb5f 100644
@@ -708,7 +708,11 @@ static inline unsigned int hexval(unsigned char c)
 #define DEFAULT_ABBREV 7
 
Quoted 10 lines

cache.h static inline unsigned int hexval(unsigned char c) · line 715

Quoted 7 lines
cache.h +5 -1
 extern int get_sha1(const char *str, unsigned char *sha1);
-extern int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode);
+static inline get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
+{
+	return get_sha1_with_mode_1(str, sha1, mode, 0, NULL);
+}
+extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int fatal, const char *prefix);

Do I understand correctly that "fatal" here is the same as "!gently" elsewhere in the API?

It seems it is. I renamed it.

Quoted 7 lines

cache.h static inline unsigned int hexval(unsigned char c)

Quoted 5 lines
cache.h +5 -0
+	if (errno == ENOENT || errno == ENOTDIR) {
+		char *fullname = malloc(strlen(filename)
+					     + strlen(prefix) + 1);
+		strcpy(fullname, prefix);
+		strcat(fullname, filename);

What if malloc fails here (and elsewhere in your patch)?

Should have been an xmalloc. Fixed.

Quoted 20 lines
Quoted 20 lines
cache.h +20 -0
+static void diagnose_invalid_index_path(int stage,
+					const char *prefix,
+					const char *filename)
+{
+	struct stat st;
+
+	if (!prefix)
+		prefix = "";
+
+	if (!lstat(filename, &st))
+		die("Path '%s' exists on disk, but not in the index.", filename);
+	if (errno == ENOENT || errno == ENOTDIR) {
+		struct cache_entry *ce;
+		int pos;
+		int namelen = strlen(filename) + strlen(prefix);
+		char *fullname = malloc(namelen + 1);
+		strcpy(fullname, prefix);
+		strcat(fullname, filename);
+		pos = cache_name_pos(fullname, namelen);
+		if (pos < 0)
Quoted 8 lines

cache.h

Quoted 8 lines
cache.h +8 -0
+			pos = -pos - 1;
+		ce = active_cache[pos];
+		if (ce_namelen(ce) == namelen &&
+		    !memcmp(ce->name, fullname, namelen))
+			die("Path '%s' is in the index, but not '%s'.\n"
+			    "Did you mean ':%d:%s'?",
+			    fullname, filename,
+			    stage, fullname);
                            ^^^^^

This one should have been ce_stage(ce), otherwise, we suggest :0:t/Makefile as you probably guessed:

Quoted 5 lines

What happens if the user asked for ":2:Makefile" while in directory "t/", and there is ":1:t/Makefile" but not ":2:t/Makefile" in the index?

What should happen if the user asked for ":2:t/Makefile" in such a case?

I originally didn't handle the case "it's in the index, but not at the stage you requested" (since most users asking :N:blah will be fluent enough in Git not to need a special case of error messages). But I finally implemented this test.

Quoted 4 lines
Quoted 4 lines
cache.h +0 -0
@@ -862,9 +934,24 @@ int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
 	}
 	if (*cp == ':') {
 		unsigned char tree_sha1[20];
Quoted 8 lines

cache.h int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode) · line 939

Quoted 6 lines
cache.h +3 -3
-		if (!get_sha1_1(name, cp-name, tree_sha1))
-			return get_tree_entry(tree_sha1, cp+1, sha1,
-					      mode);
+		char *object_name;
+		if (fatal) {
+			object_name = malloc(cp-name+1);

Where is this freed?

Nowhere. In the present state, it's not serious since all codepath going through this malloc reach a die() right after. But a free() doesn't harm, I've added it.

Quoted 4 lines

Instead of doing a leaky allocation, it may make sense to pass the tree object name as <const char *, size_t> pair, and print it with "%.*s" in the error reporting codepath. After all, object_name is used only for that purpose in diagnose_invalid_sha1_path(), no?

matter of taste, but I prefer doing one "complicated" thing (copying the string) once, and avoid having to deal with two variables instead of one later.

Quoted 7 lines
Quoted 7 lines
cache.h +7 -0
+test_expect_success 'correct file objects' '
+	HASH_file=$(git rev-parse HEAD:file.txt) &&
+	git rev-parse HEAD:subdir/file.txt &&
+	git rev-parse :index-only.txt &&
+	cd subdir &&
+	git rev-parse HEAD:file.txt &&
+	git rev-parse HEAD:subdir/file2.txt &&
Quoted 8 lines

cache.h int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode) · line 951

Quoted 5 lines
cache.h +5 -0
+	test $HASH_file = $(git rev-parse HEAD:file.txt) &&
+	test $HASH_file = $(git rev-parse :file.txt) &&
+	test $HASH_file = $(git rev-parse :0:file.txt) &&
+	cd ..
+'

Please make it a habit of not doing "cd" without forcing a subprocess using ().

Fixed.

Quoted 5 lines

cache.h int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode) · line 953

Quoted 2 lines
cache.h +2 -0
+	test_must_fail git rev-parse foobar:file.txt 2>&1 |
+		grep "Invalid object name '"'"'foobar'"'"'." &&

It always is better to write this in separate steps, because exit status of the upstream of pipe is discarded by the shell.

Fixed.

I'll resubmit soon.

--
Matthieu Moy
http://www-verimag.imag.fr/~moy/

#3 lore raw

PATCH v2 1/1 Detailed diagnosis when parsing an object name fails.

The previous error message was the same in many situations (unknown revision or path not in the working tree). We try to help the user as much as possible to understand the error, especially with the sha1:filename notation. In this case, we say whether the sha1 or the filename is problematic, and diagnose the confusion between relative-to-root and relative-to-$PWD confusion precisely.

The 7 new error messages are tested.

---
This should address Junio's comments (BTW, I forgot to say "thanks for the review" :-) ).

 cache.h                        |    6 ++-
 setup.c                        |   15 +++++-
 sha1_name.c                    |  115 ++++++++++++++++++++++++++++++++++++++--
 t/t1506-rev-parse-diagnosis.sh |   69 ++++++++++++++++++++++++
 4 files changed, 198 insertions(+), 7 deletions(-)
 create mode 100755 t/t1506-rev-parse-diagnosis.sh
cache.h +5 -1
index 0e69384..563862c 100644
@@ -708,7 +708,11 @@ static inline unsigned int hexval(unsigned char c)
 #define DEFAULT_ABBREV 7
 
 extern int get_sha1(const char *str, unsigned char *sha1);
-extern int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode);
+static inline get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
+{
+	return get_sha1_with_mode_1(str, sha1, mode, 1, NULL);
+}
+extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int gently, const char *prefix);
 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
 extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
 extern int read_ref(const char *filename, unsigned char *sha1);
setup.c +13 -2
index f67250b..5792eb7 100644
@@ -74,6 +74,18 @@ int check_filename(const char *prefix, const char *arg)
 	die_errno("failed to stat '%s'", arg);
 }
 
+static void NORETURN die_verify_filename(const char *prefix, const char *arg)
+{
+	unsigned char sha1[20];
+	unsigned mode;
+	/* try a detailed diagnostic ... */
+	get_sha1_with_mode_1(arg, sha1, &mode, 0, prefix);
+	/* ... or fall back the most general message. */
+	die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
+	    "Use '--' to separate paths from revisions", arg);
+
+}
+
 /*
  * Verify a filename that we got as an argument for a pathspec
  * entry. Note that a filename that begins with "-" never verifies
@@ -87,8 +99,7 @@ void verify_filename(const char *prefix, const char *arg)
 		die("bad flag '%s' used after filename", arg);
 	if (check_filename(prefix, arg))
 		return;
-	die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
-	    "Use '--' to separate paths from revisions", arg);
+	die_verify_filename(prefix, arg);
 }
 
 /*
sha1_name.c +111 -4

133 lines not on this page · raw message

t/t1506-rev-parse-diagnosis.sh new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
1.6.6.rc0.355.gcb53a.dirty
#4 lore raw
In reply to Matthieu Moy

Matthieu Moy <[email protected]> writes:

The second round still had a few compilation warnings (turned to errors in my environment) so I fixed them up somewhat. I stopped before getting a clean compile, though---you will still get:

sha1_name.c: In function 'get_sha1_with_mode_1':
sha1_name.c:956: error: 'object_name' may be used uninitialized in this function

even with this fix-up.

Quoted 6 lines
Quoted 4 lines

Instead of doing a leaky allocation, it may make sense to pass the tree object name as <const char *, size_t> pair, and print it with "%.*s" in the error reporting codepath. After all, object_name is used only for that purpose in diagnose_invalid_sha1_path(), no?

matter of taste,...

I thought generally it is accepted that I had a better taste on this list? ;-)

-- squashed --

 cache.h     |    4 ++--
 sha1_name.c |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
cache.h +2 -2
index 952bd51..3f9ee86 100644
@@ -702,11 +702,11 @@ static inline unsigned int hexval(unsigned char c)
 #define DEFAULT_ABBREV 7
 
 extern int get_sha1(const char *str, unsigned char *sha1);
-static inline get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
+extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int gently, const char *prefix);
+static inline int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
 {
 	return get_sha1_with_mode_1(str, sha1, mode, 1, NULL);
 }
-extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int gently, const char *prefix);
 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
 extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
 extern int read_ref(const char *filename, unsigned char *sha1);
sha1_name.c +1 -1
index 1bc09ac..025244a 100644
@@ -807,7 +807,7 @@ int get_sha1(const char *name, unsigned char *sha1)
 /* Must be called only when object_name:filename doesn't exist. */
 static void diagnose_invalid_sha1_path(const char *prefix,
 				       const char *filename,
-				       const char *tree_sha1,
+				       const unsigned char *tree_sha1,
 				       const char *object_name)
 {
 	struct stat st;
#5 lore raw
Quoted 4 lines · Junio C Hamano

Matthieu Moy <[email protected]> writes:

The second round still had a few compilation warnings (turned to errors in my environment) so I fixed them up somewhat.

Thanks. For some reasons, I had CFLAGS=-g and I didn't see the warnings :-(

Quoted 5 lines

I stopped before getting a
clean compile, though---you will still get:

sha1_name.c: In function 'get_sha1_with_mode_1':
sha1_name.c:956: error: 'object_name' may be used uninitialized in this function

This one is strange. I don't git it with gcc 4.4, even with -Wall -Werror. And indeed, object_name is initialized when !gently, which is precisely when we use it.

I did

- char *object_name;
+ char *object_name = NULL;

to make sure the warning goes away on your machine, but I don't understand what's going on.

Quoted 2 lines
 cache.h     |    4 ++--
 sha1_name.c |    2 +-

Squashed into v3, together with initialisation of object_name, and two s/int/unsigned/ to please gcc -Wextra.

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#6 lore raw

PATCH v3 1/1 Detailed diagnosis when parsing an object name fails.

The previous error message was the same in many situations (unknown revision or path not in the working tree). We try to help the user as much as possible to understand the error, especially with the sha1:filename notation. In this case, we say whether the sha1 or the filename is problematic, and diagnose the confusion between relative-to-root and relative-to-$PWD confusion precisely.

The 7 new error messages are tested.

---
 cache.h                        |    6 ++-
 setup.c                        |   15 +++++-
 sha1_name.c                    |  115 ++++++++++++++++++++++++++++++++++++++--
 t/t1506-rev-parse-diagnosis.sh |   69 ++++++++++++++++++++++++
 4 files changed, 198 insertions(+), 7 deletions(-)
 create mode 100755 t/t1506-rev-parse-diagnosis.sh
cache.h +5 -1
index 0e69384..c122bfa 100644
@@ -708,7 +708,11 @@ static inline unsigned int hexval(unsigned char c)
 #define DEFAULT_ABBREV 7
 
 extern int get_sha1(const char *str, unsigned char *sha1);
-extern int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode);
+extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int gently, const char *prefix);
+static inline int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
+{
+	return get_sha1_with_mode_1(str, sha1, mode, 1, NULL);
+}
 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
 extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
 extern int read_ref(const char *filename, unsigned char *sha1);
setup.c +13 -2
index f67250b..5792eb7 100644
@@ -74,6 +74,18 @@ int check_filename(const char *prefix, const char *arg)
 	die_errno("failed to stat '%s'", arg);
 }
 
+static void NORETURN die_verify_filename(const char *prefix, const char *arg)
+{
+	unsigned char sha1[20];
+	unsigned mode;
+	/* try a detailed diagnostic ... */
+	get_sha1_with_mode_1(arg, sha1, &mode, 0, prefix);
+	/* ... or fall back the most general message. */
+	die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
+	    "Use '--' to separate paths from revisions", arg);
+
+}
+
 /*
  * Verify a filename that we got as an argument for a pathspec
  * entry. Note that a filename that begins with "-" never verifies
@@ -87,8 +99,7 @@ void verify_filename(const char *prefix, const char *arg)
 		die("bad flag '%s' used after filename", arg);
 	if (check_filename(prefix, arg))
 		return;
-	die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
-	    "Use '--' to separate paths from revisions", arg);
+	die_verify_filename(prefix, arg);
 }
 
 /*
sha1_name.c +111 -4

133 lines not on this page · raw message

t/t1506-rev-parse-diagnosis.sh new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
1.6.6.rc0.355.gcb53a.dirty
#7 lore raw

Heya,

Quoted 6 lines · Matthieu Moy

The previous error message was the same in many situations (unknown revision or path not in the working tree). We try to help the user as much as possible to understand the error, especially with the sha1:filename notation. In this case, we say whether the sha1 or the filename is problematic, and diagnose the confusion between relative-to-root and relative-to-$PWD confusion precisely.

Thank you, I just got helped by your patch, and was very pleasantly surprised to see git being so unusually helpful! We'll make a user-friendly scm out of git yet :).

Signature · 3 lines
Cheers,

Sverre Rabbelier
#8 lore raw
In reply to Matthieu Moy
Quoted 6 lines · Matthieu Moy
Quoted 4 lines
Quoted 1 line

+extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int fatal, const char *prefix);

Do I understand correctly that "fatal" here is the same as "!gently" elsewhere in the API?

It seems it is. I renamed it.

This was a pure question, not a suggestion to change it (we do name a function do_foo_gently() when there is do_foo() that does the same but reports errors more noisily, though). I found the name "fatal" a bit confusing as I at first couldn't tell if the caller was telling the function that it already detected a "fatal" error (and telling the function to report the fatalness) and didn't realize that the caller is instead saying "if you find an error, treat it as a fatal one" until I read it again.

I am Ok with the new "gently" name with the negative semantics as well (I see no need to change it back to "error_is_fatal").

Thanks.