Commit Diff


commit - 141c2bffb4db6a2d0f6eec869b8eefe61b5deedf
commit + 1e37702e75053f3a6cee5414bef197b8ab26c983
blob - 6305af48b43066c909580a54469f78b77d10f6d4
blob + 7a65cc39eb3b4dc33f5761f394e929bf83e2c89b
--- lib/reference.c
+++ lib/reference.c
@@ -135,7 +135,7 @@ parse_ref_file(struct got_reference **ref, const char 
 	const char delim[3] = {'\0', '\0', '\0'};
 
 	if (f == NULL)
-		return got_error_not_ref(name);
+		return NULL;
 
 	line = fparseln(f, &len, NULL, delim, 0);
 	if (line == NULL) {
@@ -212,14 +212,14 @@ open_packed_ref(struct got_reference **ref, FILE *f, c
 	const char delim[3] = {'\0', '\0', '\0'};
 	int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
 
+	*ref = NULL;
+
 	if (ref_is_absolute)
 		abs_refname = (char *)refname;
 	do {
 		line = fparseln(f, &len, NULL, delim, 0);
-		if (line == NULL) {
-			err = got_error_not_ref(refname);
+		if (line == NULL)
 			break;
-		}
 		for (i = 0; i < nsubdirs; i++) {
 			if (!ref_is_absolute &&
 			    asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
@@ -250,6 +250,8 @@ open_ref(struct got_reference **ref, const char *path_
 	int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
 	int ref_is_well_known = is_well_known_ref(name);
 
+	*ref = NULL;
+
 	if (ref_is_absolute || ref_is_well_known) {
 		if (asprintf(&path, "%s/%s", path_refs, name) == -1)
 			return got_error_from_errno();
@@ -289,6 +291,8 @@ got_ref_open(struct got_reference **ref, struct got_re
 	    GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
 	};
 	int i, well_known = is_well_known_ref(refname);
+
+	*ref = NULL;
 
 	if (!well_known) {
 		char *packed_refs_path;
@@ -304,7 +308,7 @@ got_ref_open(struct got_reference **ref, struct got_re
 			err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
 			    refname);
 			fclose(f);
-			if (err == NULL)
+			if (err || *ref)
 				goto done;
 		}
 	}
@@ -318,12 +322,16 @@ got_ref_open(struct got_reference **ref, struct got_re
 	if (!well_known) {
 		for (i = 0; i < nitems(subdirs); i++) {
 			err = open_ref(ref, path_refs, subdirs[i], refname);
-			if (err == NULL)
+			if (err || *ref)
 				goto done;
 		}
 	}
 
 	err = open_ref(ref, path_refs, "", refname);
+	if (err)
+		goto done;
+	if (*ref == NULL)
+		err = got_error_not_ref(refname);
 done:
 	free(path_refs);
 	return err;
@@ -497,7 +505,7 @@ gather_refs(struct got_reflist_head *refs, const char 
 		switch (dent->d_type) {
 		case DT_REG:
 			err = open_ref(&ref, path_refs, subdir, dent->d_name);
-			if (err && err->code != GOT_ERR_NOT_REF)
+			if (err)
 				goto done;
 			if (ref) {
 				err = append_ref(refs, ref, repo);