Skip to contentSkip to conversation

cat-file ambiguity prints "dangling"

PATCH Superseded Eric Wong 3 participants 8 messages Jan 18, 2019 – Jan 30, 2019 Documentation

lore
Share & tools
lore

Eric Wong

author
#0 lore raw

Hi David,

Perhaps I'm confused, the cat-file manpage seems to indicate "dangling" only gets printed if I use "--follow-symlinks".

However, I'm not using "--follow-symlinks" and I get "dangling" when passing ambiguous object IDs:

Trying to check the ambiguous "dead" against git.git, I get:

	$ echo dead | git cat-file --batch-check 2>/dev/null
	dangling 4
	dead

Is the above intended?

stderr shows the ambiguity and gives me hints, which is expected.

Thanks.

#1 lore raw

It appears that get_oid_with_context calls into get_short_oid for that case, and get_short_oid returns SHORT_NAME_AMBIGUOUS, which is -2. We treat that as DANGLING_SYMLINK, which also seems to have the value -2. So, it's an ambiguity in ambiguity resolution.

Fix attached.

Quoted 14 lines · Eric Wong

Hi David,

Perhaps I'm confused, the cat-file manpage seems to indicate "dangling" only gets printed if I use "--follow-symlinks".

However, I'm not using "--follow-symlinks" and I get "dangling" when passing ambiguous object IDs:

Trying to check the ambiguous "dead" against git.git, I get:

	$ echo dead | git cat-file --batch-check 2>/dev/null
	dangling 4
	dead
Quoted 6 lines

Is the above intended?

stderr shows the ambiguity and gives me hints, which is expected.

Thanks.

From 319a4c76f1939a004775a3440d504e14f53074f3 Mon Sep 17 00:00:00 2001
From: David Turner <[email protected]>
Date: Thu, 17 Jan 2019 23:19:43 -0500
Subject: [PATCH] Do not print 'dangling' for cat-file in case of ambiguity

The return values -1 and -2 from get_oid could mean two different things, depending on whether they were from an enum returned by get_tree_entry_follow_symlinks, or from a different code path. This caused 'dangling' to be printed from a git cat-file in the case of an ambiguous (-2) result.

Unify the results of get_oid* and get_tree_entry_follow_symlinks to be one common type, with unambiguous values.

---
 builtin/cat-file.c |  5 +++-
 cache.h            | 20 +++++++++++++++-
 sha1-name.c        | 58 ++++++++++++++++++++++++----------------------
 tree-walk.c        |  4 ++--
 tree-walk.h        | 18 +-------------
 5 files changed, 56 insertions(+), 49 deletions(-)
builtin/cat-file.c +4 -1
index 2ca56fd086..f2344b199f 100644
@@ -380,7 +380,7 @@ static void batch_one_object(const char *obj_name,
 {
 	struct object_context ctx;
 	int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
-	enum follow_symlinks_result result;
+	enum get_oid_result result;
 
 	result = get_oid_with_context(obj_name, flags, &data->oid, &ctx);
 	if (result != FOUND) {
@@ -388,6 +388,9 @@ static void batch_one_object(const char *obj_name,
 		case MISSING_OBJECT:
 			printf("%s missing\n", obj_name);
 			break;
+		case SHORT_NAME_AMBIGUOUS:
+			/* We've already printed the ambiguous message */
+			break;
 		case DANGLING_SYMLINK:
 			printf("dangling %"PRIuMAX"\n%s\n",
 			       (uintmax_t)strlen(obj_name), obj_name);
cache.h +19 -1
index 49713cc5a5..70652e99dc 100644
@@ -1332,6 +1332,24 @@ struct object_context {
 	GET_OID_TREE | GET_OID_TREEISH | \
 	GET_OID_BLOB)
 
+enum get_oid_result {
+	FOUND = 0,
+	MISSING_OBJECT = -1, /* The requested object is missing */
+	SHORT_NAME_AMBIGUOUS = -2,
+	/* The following only apply when symlinks are followed */
+	DANGLING_SYMLINK = -4, /*
+				* The initial symlink is there, but
+				* (transitively) points to a missing
+				* in-tree file
+				*/
+	SYMLINK_LOOP = -5,
+	NOT_DIR = -6, /*
+		       * Somewhere along the symlink chain, a path is
+		       * requested which contains a file as a
+		       * non-final element.
+		       */
+};
+
 extern int get_oid(const char *str, struct object_id *oid);
 extern int get_oid_commit(const char *str, struct object_id *oid);
 extern int get_oid_committish(const char *str, struct object_id *oid);
@@ -1339,7 +1357,7 @@ extern int get_oid_tree(const char *str, struct object_id *oid);
 extern int get_oid_treeish(const char *str, struct object_id *oid);
 extern int get_oid_blob(const char *str, struct object_id *oid);
 extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
-extern int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc);
+extern enum get_oid_result get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc);
 
 
 typedef int each_abbrev_fn(const struct object_id *oid, void *);
sha1-name.c +30 -28

145 lines not on this page · raw message

tree-walk.c +2 -2
index 08210a4109..fb7959b21d 100644
@@ -579,10 +579,10 @@ int get_tree_entry(const struct object_id *tree_oid, const char *name, struct ob
  * with the sha1 of the found object, and *mode will hold the mode of
  * the object.
  *
- * See the code for enum follow_symlink_result for a description of
+ * See the code for enum get_oid_result for a description of
  * the return values.
  */
-enum follow_symlinks_result get_tree_entry_follow_symlinks(struct object_id *tree_oid, const char *name, struct object_id *result, struct strbuf *result_path, unsigned *mode)
+enum get_oid_result get_tree_entry_follow_symlinks(struct object_id *tree_oid, const char *name, struct object_id *result, struct strbuf *result_path, unsigned *mode)
 {
 	int retval = MISSING_OBJECT;
 	struct dir_state *parents = NULL;
tree-walk.h +1 -17
index eefd26bb62..de6b95179d 100644
@@ -51,23 +51,7 @@ struct traverse_info;
 typedef int (*traverse_callback_t)(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *);
 int traverse_trees(struct index_state *istate, int n, struct tree_desc *t, struct traverse_info *info);
 
-enum follow_symlinks_result {
-	FOUND = 0, /* This includes out-of-tree links */
-	MISSING_OBJECT = -1, /* The initial symlink is missing */
-	DANGLING_SYMLINK = -2, /*
-				* The initial symlink is there, but
-				* (transitively) points to a missing
-				* in-tree file
-				*/
-	SYMLINK_LOOP = -3,
-	NOT_DIR = -4, /*
-		       * Somewhere along the symlink chain, a path is
-		       * requested which contains a file as a
-		       * non-final element.
-		       */
-};
-
-enum follow_symlinks_result get_tree_entry_follow_symlinks(struct object_id *tree_oid, const char *name, struct object_id *result, struct strbuf *result_path, unsigned *mode);
+enum get_oid_result get_tree_entry_follow_symlinks(struct object_id *tree_oid, const char *name, struct object_id *result, struct strbuf *result_path, unsigned *mode);
 
 struct traverse_info {
 	const char *traverse_path;
Signature · 1 line
2.19.1
author
#2 lore raw
Quoted 6 lines · David Turner

It appears that get_oid_with_context calls into get_short_oid for that case, and get_short_oid returns SHORT_NAME_AMBIGUOUS, which is -2. We treat that as DANGLING_SYMLINK, which also seems to have the value -2. So, it's an ambiguity in ambiguity resolution.

Fix attached.

Thanks for the quick response. Unfortunately, the patch makes the situation worse for me...

Quoted 14 lines
builtin/cat-file.c +1 -1
index 2ca56fd086..f2344b199f 100644
@@ -380,7 +380,7 @@ static void batch_one_object(const char *obj_name,
 {
 	struct object_context ctx;
 	int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
-	enum follow_symlinks_result result;
+	enum get_oid_result result;
 
 	result = get_oid_with_context(obj_name, flags, &data->oid, &ctx);
 	if (result != FOUND) {

builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 393

Quoted 6 lines
builtin/cat-file.c +3 -0
@@ -388,6 +388,9 @@ static void batch_one_object(const char *obj_name,
 		case MISSING_OBJECT:
 			printf("%s missing\n", obj_name);
 			break;
+		case SHORT_NAME_AMBIGUOUS:
+			/* We've already printed the ambiguous message */
+			break;

Because the message about ambiguity only goes to stderr. Something needs to go to stdout, or else the reader process would hang forever.

I've already updated my code[1] to work around "dangling", but maybe printing "ambiguous" similar to the "missing" message would be appropriate? Something like:

		case SHORT_NAME_AMBIGUOUS:
			printf("%s ambiguous\n", obj_name);
			break;

(Along with a manpage update)

[1] new stuff for public-inbox on the way :>

#3 lore raw

Let's try this version.

Quoted 47 lines · Eric Wong
Quoted 9 lines · David Turner

It appears that get_oid_with_context calls into get_short_oid for that
case, and get_short_oid returns SHORT_NAME_AMBIGUOUS, which is -2. We
treat that as DANGLING_SYMLINK, which also seems to have the value -2.
So, it's an ambiguity in ambiguity resolution.

Fix attached.

Thanks for the quick response. Unfortunately, the patch makes the situation worse for me...

Quoted 16 lines
builtin/cat-file.c +1 -1
index 2ca56fd086..f2344b199f 100644
@@ -380,7 +380,7 @@ static void batch_one_object(const char
 *obj_name,
 {
 	struct object_context ctx;
 	int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
-	enum follow_symlinks_result result;
+	enum get_oid_result result;
 
 	result = get_oid_with_context(obj_name, flags, &data->oid,

&ctx);

builtin/cat-file.c +0 -0
 	if (result != FOUND) {

builtin/cat-file.c static void batch_one_object(const char · line 395

Quoted 8 lines
builtin/cat-file.c +3 -0
@@ -388,6 +388,9 @@ static void batch_one_object(const char
 *obj_name,
 		case MISSING_OBJECT:
 			printf("%s missing\n", obj_name);
 			break;
+		case SHORT_NAME_AMBIGUOUS:
+			/* We've already printed the ambiguous message
 */
+			break;

Because the message about ambiguity only goes to stderr. Something needs to go to stdout, or else the reader process would hang forever.

I've already updated my code[1] to work around "dangling", but maybe printing "ambiguous" similar to the "missing" message would be appropriate? Something like:

Quoted 9 lines
		case SHORT_NAME_AMBIGUOUS:
			printf("%s ambiguous\n", obj_name);
			break;

(Along with a manpage update)

[1] new stuff for public-inbox on the way :>

From b152b3463477a8260d2b8c0ab46ad76562feed37 Mon Sep 17 00:00:00 2001
From: David Turner <[email protected]>
Date: Thu, 17 Jan 2019 23:19:43 -0500
Subject: [PATCH v2] Do not print 'dangling' for cat-file in case of ambiguity

The return values -1 and -2 from get_oid could mean two different things, depending on whether they were from an enum returned by get_tree_entry_follow_symlinks, or from a different code path. This caused 'dangling' to be printed from a git cat-file in the case of an ambiguous (-2) result.

Unify the results of get_oid* and get_tree_entry_follow_symlinks to be one common type, with unambiguous values.

---
 Documentation/git-cat-file.txt |  6 ++++
 builtin/cat-file.c             |  5 ++-
 cache.h                        | 20 +++++++++++-
 sha1-name.c                    | 58 ++++++++++++++++++----------------
 tree-walk.c                    |  4 +--
 tree-walk.h                    | 18 +----------
 6 files changed, 62 insertions(+), 49 deletions(-)
Documentation/git-cat-file.txt +6 -0
index 74013335a1..e27a940985 100644
@@ -252,6 +252,12 @@ the repository, then `cat-file` will ignore any custom format and print:
 <object> SP missing LF
 ------------
 
+If a name is specified that might refer to more than one object (an ambiguous short sha), then `cat-file` will ignore any custom format and print:
+
+------------
+<object> SP ambiguous LF
+------------
+
 If --follow-symlinks is used, and a symlink in the repository points
 outside the repository, then `cat-file` will ignore any custom format
 and print:
builtin/cat-file.c +4 -1
index 2ca56fd086..cebc6d7f8a 100644
@@ -380,7 +380,7 @@ static void batch_one_object(const char *obj_name,
 {
 	struct object_context ctx;
 	int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
-	enum follow_symlinks_result result;
+	enum get_oid_result result;
 
 	result = get_oid_with_context(obj_name, flags, &data->oid, &ctx);
 	if (result != FOUND) {
@@ -388,6 +388,9 @@ static void batch_one_object(const char *obj_name,
 		case MISSING_OBJECT:
 			printf("%s missing\n", obj_name);
 			break;
+		case SHORT_NAME_AMBIGUOUS:
+			printf("%s ambiguous\n", obj_name);
+			break;
 		case DANGLING_SYMLINK:
 			printf("dangling %"PRIuMAX"\n%s\n",
 			       (uintmax_t)strlen(obj_name), obj_name);
cache.h +19 -1
index 49713cc5a5..70652e99dc 100644
@@ -1332,6 +1332,24 @@ struct object_context {
 	GET_OID_TREE | GET_OID_TREEISH | \
 	GET_OID_BLOB)
 
+enum get_oid_result {
+	FOUND = 0,
+	MISSING_OBJECT = -1, /* The requested object is missing */
+	SHORT_NAME_AMBIGUOUS = -2,
+	/* The following only apply when symlinks are followed */
+	DANGLING_SYMLINK = -4, /*
+				* The initial symlink is there, but
+				* (transitively) points to a missing
+				* in-tree file
+				*/
+	SYMLINK_LOOP = -5,
+	NOT_DIR = -6, /*
+		       * Somewhere along the symlink chain, a path is
+		       * requested which contains a file as a
+		       * non-final element.
+		       */
+};
+
 extern int get_oid(const char *str, struct object_id *oid);
 extern int get_oid_commit(const char *str, struct object_id *oid);
 extern int get_oid_committish(const char *str, struct object_id *oid);
@@ -1339,7 +1357,7 @@ extern int get_oid_tree(const char *str, struct object_id *oid);
 extern int get_oid_treeish(const char *str, struct object_id *oid);
 extern int get_oid_blob(const char *str, struct object_id *oid);
 extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
-extern int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc);
+extern enum get_oid_result get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc);
 
 
 typedef int each_abbrev_fn(const struct object_id *oid, void *);
sha1-name.c +30 -28

145 lines not on this page · raw message

tree-walk.c +2 -2
index 08210a4109..fb7959b21d 100644
@@ -579,10 +579,10 @@ int get_tree_entry(const struct object_id *tree_oid, const char *name, struct ob
  * with the sha1 of the found object, and *mode will hold the mode of
  * the object.
  *
- * See the code for enum follow_symlink_result for a description of
+ * See the code for enum get_oid_result for a description of
  * the return values.
  */
-enum follow_symlinks_result get_tree_entry_follow_symlinks(struct object_id *tree_oid, const char *name, struct object_id *result, struct strbuf *result_path, unsigned *mode)
+enum get_oid_result get_tree_entry_follow_symlinks(struct object_id *tree_oid, const char *name, struct object_id *result, struct strbuf *result_path, unsigned *mode)
 {
 	int retval = MISSING_OBJECT;
 	struct dir_state *parents = NULL;
tree-walk.h +1 -17
index eefd26bb62..de6b95179d 100644
@@ -51,23 +51,7 @@ struct traverse_info;
 typedef int (*traverse_callback_t)(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *);
 int traverse_trees(struct index_state *istate, int n, struct tree_desc *t, struct traverse_info *info);
 
-enum follow_symlinks_result {
-	FOUND = 0, /* This includes out-of-tree links */
-	MISSING_OBJECT = -1, /* The initial symlink is missing */
-	DANGLING_SYMLINK = -2, /*
-				* The initial symlink is there, but
-				* (transitively) points to a missing
-				* in-tree file
-				*/
-	SYMLINK_LOOP = -3,
-	NOT_DIR = -4, /*
-		       * Somewhere along the symlink chain, a path is
-		       * requested which contains a file as a
-		       * non-final element.
-		       */
-};
-
-enum follow_symlinks_result get_tree_entry_follow_symlinks(struct object_id *tree_oid, const char *name, struct object_id *result, struct strbuf *result_path, unsigned *mode);
+enum get_oid_result get_tree_entry_follow_symlinks(struct object_id *tree_oid, const char *name, struct object_id *result, struct strbuf *result_path, unsigned *mode);
 
 struct traverse_info {
 	const char *traverse_path;
Signature · 1 line
2.19.1
author
#4 lore raw

PATCH 1/1 t1512: test ambiguous cat-file --batch and --batch-output

Quoted 1 line · David Turner

Let's try this version.

Thanks!

Not sure if t1512 would be the best place to put the test, but having a repo with ambiguous data already at hand was easiest.

---------8<-------
Subject: [PATCH] t1512: test ambiguous cat-file --batch and --batch-output

Test the new "ambiguous" result from cat-file --batch and --batch-check. This is in t1512 instead of t1006 since we need a repo with ambiguous object_id names.

---
 t/t1512-rev-parse-disambiguation.sh | 10 ++++++++++
 1 file changed, 10 insertions(+)
t/t1512-rev-parse-disambiguation.sh +10 -0
index e4d5b56014..b0819630c9 100755
@@ -388,4 +388,14 @@ test_expect_success C_LOCALE_OUTPUT 'ambiguous commits are printed by type first
 	done
 '
 
+test_expect_success 'cat-file --batch and --batch-check show ambiguous' '
+	echo "0000 ambiguous" >expect &&
+	echo 0000 | git cat-file --batch-check >actual 2>err &&
+	test_cmp expect actual &&
+	grep hint: err &&
+	echo 0000 | git cat-file --batch >actual 2>err &&
+	test_cmp expect actual &&
+	grep hint: err
+'
+
 test_done
Signature · 1 line
EW
#5 lore raw

Re: [PATCH] t1512: test ambiguous cat-file --batch and --batch-output

Thanks for adding the test.

Quoted 45 lines · Eric Wong
Quoted 1 line · David Turner

Let's try this version.

Thanks!

Not sure if t1512 would be the best place to put the test, but having a repo with ambiguous data already at hand was easiest.

---------8<-------
Subject: [PATCH] t1512: test ambiguous cat-file --batch and --batch- output

Test the new "ambiguous" result from cat-file --batch and --batch-check. This is in t1512 instead of t1006 since we need a repo with ambiguous object_id names.

---
 t/t1512-rev-parse-disambiguation.sh | 10 ++++++++++
 1 file changed, 10 insertions(+)
t/t1512-rev-parse- renamed from t/t1512-rev-parse-disambiguation.sh +0 -0

disambiguation.sh
index e4d5b56014..b0819630c9 100755

t/t1512-rev-parse-disambiguation.sh +0 -0
@@ -388,4 +388,14 @@ test_expect_success C_LOCALE_OUTPUT 'ambiguous

commits are printed by type first

t/t1512-rev-parse-disambiguation.sh +1 -0
 	done
 '
 
+test_expect_success 'cat-file --batch and --batch-check show

ambiguous' '

t/t1512-rev-parse-disambiguation.sh +9 -0
+	echo "0000 ambiguous" >expect &&
+	echo 0000 | git cat-file --batch-check >actual 2>err &&
+	test_cmp expect actual &&
+	grep hint: err &&
+	echo 0000 | git cat-file --batch >actual 2>err &&
+	test_cmp expect actual &&
+	grep hint: err
+'
+
 test_done
#6 lore raw

Re: [PATCH] t1512: test ambiguous cat-file --batch and --batch-output

In reply to Eric Wong
Quoted 15 lines · Eric Wong

Test the new "ambiguous" result from cat-file --batch and --batch-check. This is in t1512 instead of t1006 since we need a repo with ambiguous object_id names.

---
 t/t1512-rev-parse-disambiguation.sh | 10 ++++++++++
 1 file changed, 10 insertions(+)
t/t1512-rev-parse-disambiguation.sh +0 -0
index e4d5b56014..b0819630c9 100755
@@ -388,4 +388,14 @@ test_expect_success C_LOCALE_OUTPUT 'ambiguous commits are printed by type first
 	done

t/t1512-rev-parse-disambiguation.sh test_expect_success C_LOCALE_OUTPUT 'ambiguous commits are printed by type first · line 398

Quoted 10 lines
t/t1512-rev-parse-disambiguation.sh +8 -0
 '
 
+test_expect_success 'cat-file --batch and --batch-check show ambiguous' '
+	echo "0000 ambiguous" >expect &&
+	echo 0000 | git cat-file --batch-check >actual 2>err &&
+	test_cmp expect actual &&
+	grep hint: err &&
+	echo 0000 | git cat-file --batch >actual 2>err &&
+	test_cmp expect actual &&
+	grep hint: err

The 'git cat-file's error message and hints are translated, so they should be checked by 'test_i18ngrep' instead of plain old grep.

Quoted 5 lines
t/t1512-rev-parse-disambiguation.sh +2 -0
+'
+
 test_done
Signature · 1 line
EW
author
#7 lore raw

PATCH v2 1/1 t1512: test ambiguous cat-file --batch and --batch-output

Quoted 2 lines · SZEDER Gábor

The 'git cat-file's error message and hints are translated, so they should be checked by 'test_i18ngrep' instead of plain old grep.

Thanks, here's v2 with test_i18ngrep

--------8<------
Subject: [PATCH] t1512: test ambiguous cat-file --batch and --batch-output

Test the new "ambiguous" result from cat-file --batch and --batch-check. This is in t1512 instead of t1006 since we need a repo with ambiguous object_id names.

---
 t/t1512-rev-parse-disambiguation.sh | 10 ++++++++++
 1 file changed, 10 insertions(+)
t/t1512-rev-parse-disambiguation.sh +10 -0
index e4d5b56014..c19fb500cb 100755
@@ -388,4 +388,14 @@ test_expect_success C_LOCALE_OUTPUT 'ambiguous commits are printed by type first
 	done
 '
 
+test_expect_success 'cat-file --batch and --batch-check show ambiguous' '
+	echo "0000 ambiguous" >expect &&
+	echo 0000 | git cat-file --batch-check >actual 2>err &&
+	test_cmp expect actual &&
+	test_i18ngrep hint: err &&
+	echo 0000 | git cat-file --batch >actual 2>err &&
+	test_cmp expect actual &&
+	test_i18ngrep hint: err
+'
+
 test_done
Signature · 1 line
EW