Commit 0ed2285b0119b293d6b77b882c707c0377e176cd

Stefan Sperling 2022-03-09T08:55:42

handle reference arguments which look like short object IDs Match command line arguments against references before matching object IDs. This makes it possible to use reference names that happen to match a short object ID. For example, a branch called "11ac" could not be diffed in OpenBSD src.git which happens to contain commit IDs that begin with hex digits 0x11ac. A bogus error would be reported in this situation: $ got diff master 11ac got: ambiguous object ID ok naddy

diff --git a/lib/repository.c b/lib/repository.c
index 5260575..8885743 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -1776,12 +1776,10 @@ got_repo_match_object_id(struct got_object_id **id, char **label,
 			return err;
 	}
 
-	err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
-	if (err) {
-		if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
-			return err;
-		err = got_ref_open(&ref, repo, id_str, 0);
-		if (err != NULL)
+	err = got_ref_open(&ref, repo, id_str, 0);
+	if (err == NULL) {
+		err = got_ref_resolve(id, repo, ref);
+		if (err)
 			goto done;
 		if (label) {
 			*label = strdup(got_ref_get_name(ref));
@@ -1790,12 +1788,23 @@ got_repo_match_object_id(struct got_object_id **id, char **label,
 				goto done;
 			}
 		}
-		err = got_ref_resolve(id, repo, ref);
-	} else if (label) {
-		err = got_object_id_str(label, *id);
-		if (*label == NULL) {
-			err = got_error_from_errno("strdup");
+	} else {
+		if (err->code != GOT_ERR_NOT_REF &&
+		    err->code != GOT_ERR_BAD_REF_NAME)
 			goto done;
+		err = got_repo_match_object_id_prefix(id, id_str,
+		    obj_type, repo);
+		if (err) {
+			if (err->code == GOT_ERR_BAD_OBJ_ID_STR)
+				err = got_error_not_ref(id_str);
+			goto done;
+		}
+		if (label) {
+			err = got_object_id_str(label, *id);
+			if (*label == NULL) {
+				err = got_error_from_errno("strdup");
+				goto done;
+			}
 		}
 	}
 done: