"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
add and use got_repo_get_object_format()
To:
gameoftrees@openbsd.org
Date:
Mon, 27 Feb 2023 15:09:25 +0100

Download raw body.

Thread
This adds a new field to the struct got_repository and a getter for it
so that we can avoid hardcoding GOT_HASH_SHA1 in a few places.  At the
moment it is implicitly set to GOT_HASH_SHA1, but once we have more
support for sha256 (the immediate targets are packfiles and loose
objects) we can start honouring the `objectformat=sha256' and have all
the code in repository.c / repository_admin.c / reference.c working
regardless of the hash used.

There is a concern, which luckily doesn't have any consequence but it's
still worth noticing: lib/repository.c:/^is_git_repo tries to resolve
"HEAD" before the repo config is parsed.  This means that, say, on a
sha256 repos we'd parse the object ids as sha1.  Since we do nothing
with the resolved ref this doesn't cause issues in practice but is still
weird.

diff /home/op/w/got
commit - 5c5396e5c1ee0b8ede1cce4c7262a946ab1b3970
path + /home/op/w/got
blob - 53e99801ab01ad8804d048c5f59a94930e5ef10a
file + include/got_repository.h
--- include/got_repository.h
+++ include/got_repository.h
@@ -35,6 +35,9 @@ int got_repo_get_fd(struct got_repository *);
 /* Obtain the file descriptor of the repository's .git directory. */
 int got_repo_get_fd(struct got_repository *);
 
+/* Obtain the object format */
+enum got_hash_algorithm got_repo_get_object_format(struct got_repository *);
+
 /* Obtain the commit author name if parsed from gitconfig, else NULL. */
 const char *got_repo_get_gitconfig_author_name(struct got_repository *);
 
blob - 83d718aa029bf9afded3debb15731d617de2169c
file + lib/got_lib_repository.h
--- lib/got_lib_repository.h
+++ lib/got_lib_repository.h
@@ -63,6 +63,7 @@ struct got_repository {
 	char *path;
 	char *path_git_dir;
 	int gitdir_fd;
+	enum got_hash_algorithm algo;
 
 	struct got_pathlist_head packidx_paths;
 	struct timespec pack_path_mtime;
blob - fd8aa92b4400836a36c9ff24e521107661b42342
file + lib/reference.c
--- lib/reference.c
+++ lib/reference.c
@@ -153,9 +153,8 @@ parse_ref_line(struct got_reference **ref, const char 
 
 static const struct got_error *
 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
-    time_t mtime)
+    time_t mtime, enum got_hash_algorithm algo)
 {
-	enum got_hash_algorithm algo = GOT_HASH_SHA1;
 	struct got_object_id id;
 
 	if (strncmp(line, "ref: ", 5) == 0) {
@@ -171,7 +170,8 @@ parse_ref_file(struct got_reference **ref, const char 
 
 static const struct got_error *
 parse_ref_file(struct got_reference **ref, const char *name,
-    const char *absname, const char *abspath, int lock)
+    const char *absname, const char *abspath, int lock,
+    enum got_hash_algorithm algo)
 {
 	const struct got_error *err = NULL;
 	FILE *f;
@@ -226,7 +226,7 @@ parse_ref_file(struct got_reference **ref, const char 
 		linelen--;
 	}
 
-	err = parse_ref_line(ref, absname, line, sb.st_mtime);
+	err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
 	if (lock) {
 		if (err)
 			got_lockfile_unlock(lf, -1);
@@ -291,9 +291,8 @@ parse_packed_ref_line(struct got_reference **ref, cons
 
 static const struct got_error *
 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
-    const char *line, time_t mtime)
+    const char *line, time_t mtime, enum got_hash_algorithm algo)
 {
-	enum got_hash_algorithm algo = GOT_HASH_SHA1;
 	struct got_object_id id;
 	const char *name;
 
@@ -317,7 +316,8 @@ open_packed_ref(struct got_reference **ref, FILE *f, c
 
 static const struct got_error *
 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
-    int nsubdirs, const char *refname, time_t mtime)
+    int nsubdirs, const char *refname, time_t mtime,
+    enum got_hash_algorithm algo)
 {
 	const struct got_error *err = NULL;
 	char *abs_refname;
@@ -346,7 +346,7 @@ open_packed_ref(struct got_reference **ref, FILE *f, c
 			    refname) == -1)
 				return got_error_from_errno("asprintf");
 			err = parse_packed_ref_line(ref, abs_refname, line,
-			    mtime);
+			    mtime, algo);
 			if (!ref_is_absolute)
 				free(abs_refname);
 			if (err || *ref != NULL)
@@ -362,7 +362,7 @@ open_ref(struct got_reference **ref, const char *path_
 
 static const struct got_error *
 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
-    const char *name, int lock)
+    const char *name, int lock, enum got_hash_algorithm algo)
 {
 	const struct got_error *err = NULL;
 	char *path = NULL;
@@ -391,7 +391,7 @@ open_ref(struct got_reference **ref, const char *path_
 		}
 	}
 
-	err = parse_ref_file(ref, name, absname, path, lock);
+	err = parse_ref_file(ref, name, absname, path, lock, algo);
 done:
 	if (!ref_is_absolute && !ref_is_well_known)
 		free(absname);
@@ -421,14 +421,15 @@ got_ref_open(struct got_reference **ref, struct got_re
 	}
 
 	if (well_known) {
-		err = open_ref(ref, path_refs, "", refname, lock);
+		err = open_ref(ref, path_refs, "", refname, lock,
+		    got_repo_get_object_format(repo));
 	} else {
 		FILE *f;
 
 		/* Search on-disk refs before packed refs! */
 		for (i = 0; i < nitems(subdirs); i++) {
 			err = open_ref(ref, path_refs, subdirs[i], refname,
-			    lock);
+			    lock, got_repo_get_object_format(repo));
 			if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
 				goto done;
 		}
@@ -454,7 +455,8 @@ got_ref_open(struct got_reference **ref, struct got_re
 				goto done;
 			}
 			err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
-			    refname, sb.st_mtime);
+			    refname, sb.st_mtime,
+			    got_repo_get_object_format(repo));
 			if (!err) {
 				if (fclose(f) == EOF) {
 					err = got_error_from_errno("fclose");
@@ -917,7 +919,7 @@ gather_on_disk_refs(struct got_reflist_head *refs, con
 		switch (type) {
 		case DT_REG:
 			err = open_ref(&ref, path_refs, subdir, dent->d_name,
-			    0);
+			    0, got_repo_get_object_format(repo));
 			if (err)
 				goto done;
 			if (ref) {
@@ -970,7 +972,8 @@ got_ref_list(struct got_reflist_head *refs, struct got
 			err = got_error_from_errno("get_refs_dir_path");
 			goto done;
 		}
-		err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
+		err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
+		    got_repo_get_object_format(repo));
 		if (err)
 			goto done;
 		err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
@@ -986,7 +989,8 @@ got_ref_list(struct got_reflist_head *refs, struct got
 			err = got_error_from_errno("get_refs_dir_path");
 			goto done;
 		}
-		err = open_ref(&ref, path_refs, "", refname, 0);
+		err = open_ref(&ref, path_refs, "", refname, 0,
+		    got_repo_get_object_format(repo));
 		if (err) {
 			if (err->code != GOT_ERR_NOT_REF)
 				goto done;
@@ -1069,7 +1073,7 @@ got_ref_list(struct got_reflist_head *refs, struct got
 			if (linelen > 0 && line[linelen - 1] == '\n')
 				line[linelen - 1] = '\0';
 			err = parse_packed_ref_line(&ref, NULL, line,
-			    sb.st_mtime);
+			    sb.st_mtime, got_repo_get_object_format(repo));
 			if (err)
 				goto done;
 			if (ref) {
@@ -1322,7 +1326,8 @@ delete_packed_ref(struct got_reference *delref, struct
 		}
 		if (linelen > 0 && line[linelen - 1] == '\n')
 			line[linelen - 1] = '\0';
-		err = parse_packed_ref_line(&ref, NULL, line, 0);
+		err = parse_packed_ref_line(&ref, NULL, line, 0,
+		    got_repo_get_object_format(repo));
 		if (err)
 			goto done;
 		if (ref == NULL)
blob - bb181bc9236aba33630380d438c03cdfc85ae0d9
file + lib/repository.c
--- lib/repository.c
+++ lib/repository.c
@@ -115,6 +115,12 @@ const char *
 	return repo->gitdir_fd;
 }
 
+enum got_hash_algorithm
+got_repo_get_object_format(struct got_repository *repo)
+{
+	return repo->algo;
+}
+
 const char *
 got_repo_get_gitconfig_author_name(struct got_repository *repo)
 {
@@ -1738,7 +1744,6 @@ match_loose_object(struct got_object_id **unique_id, c
 	}
 	while ((dent = readdir(dir)) != NULL) {
 		int cmp;
-		enum got_hash_algorithm algo = GOT_HASH_SHA1;
 
 		free(id_str);
 		id_str = NULL;
@@ -1752,7 +1757,7 @@ match_loose_object(struct got_object_id **unique_id, c
 			goto done;
 		}
 
-		if (!got_parse_object_id(&id, id_str, algo))
+		if (!got_parse_object_id(&id, id_str, repo->algo))
 			continue;
 
 		/*
@@ -2292,7 +2297,6 @@ got_repo_get_loose_object_info(int *nobjects, off_t *o
 			char *id_str;
 			int fd;
 			struct stat sb;
-			enum got_hash_algorithm algo = GOT_HASH_SHA1;
 
 			if (strcmp(dent->d_name, ".") == 0 ||
 			    strcmp(dent->d_name, "..") == 0)
@@ -2303,7 +2307,7 @@ got_repo_get_loose_object_info(int *nobjects, off_t *o
 				goto done;
 			}
 
-			if (!got_parse_object_id(&id, id_str, algo)) {
+			if (!got_parse_object_id(&id, id_str, repo->algo)) {
 				free(id_str);
 				continue;
 			}
blob - 8608ef9dfd20957fb5e02caccfd03c8ca891ebc4
file + lib/repository_admin.c
--- lib/repository_admin.c
+++ lib/repository_admin.c
@@ -486,7 +486,7 @@ got_repo_find_pack(FILE **packfile, struct got_object_
 		goto done;
 	}
 	*dot = '\0';
-	if (!got_parse_object_id(&id, p, GOT_HASH_SHA1)) {
+	if (!got_parse_object_id(&id, p, repo->algo)) {
 		err = got_error_fmt(GOT_ERR_BAD_PATH,
 		    "'%s' is not a valid pack file name",
 		    packfile_name);
@@ -685,8 +685,7 @@ get_loose_object_ids(struct got_object_idset **loose_i
 				goto done;
 			}
 
-			if (!got_parse_object_id(&id, id_str,
-			    GOT_HASH_SHA1)) {
+			if (!got_parse_object_id(&id, id_str, repo->algo)) {
 				free(id_str);
 				continue;
 			}