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

From:
Omar Polo <op@omarpolo.com>
Subject:
gotadmin init -t sha256 ?
To:
gameoftrees@openbsd.org
Date:
Tue, 16 Jul 2024 14:29:29 +0200

Download raw body.

Thread
Here's two more sha256-related diffs that are small enough to fit one
mail.

The first is to enable the creation of sha256 repos via `init -t sha256'.
Opinions about this?  We have now enough feature to work (albeit locally
only) on sha256 repos so we might as well allow people to create them.

(GOT_HASH_SHA1 is hardcoded only when creating repos via git/cvg clone
since the network protocol doesn't support sha256 -- we'll need to
implement the v2 of it.  Actually maybe I should add a warning against
fetch/send in sha256 repos.)

The second one is to show the object-format in the gotadmin info output
which is something I believe it's handy.

Ideas/better verbiage/oks/etc? :)


diff /home/op/w/got
commit - a5a5a156a91a17c59934179af8eca099a95b6c2d
path + /home/op/w/got
blob - 0efd11eb1ea05278d857c70ca545f17de8f87588
file + cvg/cvg.c
--- cvg/cvg.c
+++ cvg/cvg.c
@@ -1675,7 +1675,7 @@ cmd_clone(int argc, char *argv[])
 		goto done;
 
 	if (!list_refs_only) {
-		error = got_repo_init(repo_path, NULL);
+		error = got_repo_init(repo_path, NULL, GOT_HASH_SHA1);
 		if (error)
 			goto done;
 		error = got_repo_pack_fds_open(&pack_fds);
blob - 82cc6a0781f740c40e728b1018f50ffc72607f9b
file + got/got.1
--- got/got.1
+++ got/got.1
@@ -75,7 +75,7 @@ The commands for
 .Nm
 are as follows:
 .Bl -tag -width checkout
-.It Cm init Oo Fl b Ar branch Oc Ar repository-path
+.It Cm init Oo Fl b Ar branch Oc Oo Fl t Ar format Oc Ar repository-path
 Create a new empty repository at the specified
 .Ar repository-path .
 .Pp
@@ -113,6 +113,13 @@ Make the repository's HEAD reference point to the spec
 .Ar branch
 instead of the default branch
 .Dq main .
+.It Fl t Ar format
+Select the hashing function to use.
+Possible values are
+.Cm sha1
+.Pq the default
+or
+.Cm sha256 .
 .El
 .Pp
 The
blob - 3f403ec8e8aec0b1e344d07f677b0cd872cdaa28
file + got/got.c
--- got/got.c
+++ got/got.c
@@ -357,7 +357,8 @@ apply_unveil(const char *repo_path, int repo_read_only
 __dead static void
 usage_init(void)
 {
-	fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
+	fprintf(stderr, "usage: %s init [-b branch] [-t format]"
+	    " repository-path\n",
 	    getprogname());
 	exit(1);
 }
@@ -368,13 +369,23 @@ cmd_init(int argc, char *argv[])
 	const struct got_error *error = NULL;
 	const char *head_name = NULL;
 	char *repo_path = NULL;
+	enum got_hash_algorithm algo = GOT_HASH_SHA1;
 	int ch;
 
-	while ((ch = getopt(argc, argv, "b:")) != -1) {
+	while ((ch = getopt(argc, argv, "b:t:")) != -1) {
 		switch (ch) {
 		case 'b':
 			head_name = optarg;
 			break;
+		case 't':
+			if (!strcmp(optarg, "sha1"))
+				algo = GOT_HASH_SHA1;
+			else if (!strcmp(optarg, "sha256"))
+				algo = GOT_HASH_SHA256;
+			else
+				return got_error_path(optarg,
+				    GOT_ERR_OBJECT_FORMAT);
+			break;
 		default:
 			usage_init();
 			/* NOTREACHED */
@@ -406,7 +417,7 @@ cmd_init(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	error = got_repo_init(repo_path, head_name);
+	error = got_repo_init(repo_path, head_name, algo);
 done:
 	free(repo_path);
 	return error;
@@ -1765,7 +1776,7 @@ cmd_clone(int argc, char *argv[])
 		err(1, "pledge");
 #endif
 	if (!list_refs_only) {
-		error = got_repo_init(repo_path, NULL);
+		error = got_repo_init(repo_path, NULL, GOT_HASH_SHA1);
 		if (error)
 			goto done;
 		error = got_repo_pack_fds_open(&pack_fds);
blob - 00bb07ed7939d88c75f1fb6e94a7b77a86d50730
file + gotadmin/gotadmin.1
--- gotadmin/gotadmin.1
+++ gotadmin/gotadmin.1
@@ -53,7 +53,7 @@ The commands for
 .Nm
 are as follows:
 .Bl -tag -width checkout
-.It Cm init Oo Fl b Ar branch Oc Ar repository-path
+.It Cm init Oo Fl b Ar branch Oc Oo Fl t format Oc Ar repository-path
 Create a new empty repository at the specified
 .Ar repository-path .
 .Pp
@@ -91,6 +91,13 @@ Make the repository's HEAD reference point to the spec
 .Ar branch
 instead of the default branch
 .Dq main .
+.It Fl t Ar format
+Select the hashing function to use.
+Possible values are
+.Cm sha1
+.Pq the default
+or
+.Cm sha256 .
 .El
 .Pp
 The
blob - ac13af4d643c13b2ba5b2cdc4285db4bb2b2789c
file + gotadmin/gotadmin.c
--- gotadmin/gotadmin.c
+++ gotadmin/gotadmin.c
@@ -280,7 +280,8 @@ done:
 __dead static void
 usage_init(void)
 {
-	fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
+	fprintf(stderr, "usage: %s init [-b branch] [-t format]"
+	    " repository-path\n",
 	    getprogname());
 	exit(1);
 }
@@ -291,6 +292,7 @@ cmd_init(int argc, char *argv[])
 	const struct got_error *error = NULL;
 	const char *head_name = NULL;
 	char *repo_path = NULL;
+	enum got_hash_algorithm algo = GOT_HASH_SHA1;
 	int ch;
 
 #ifndef PROFILE
@@ -298,11 +300,20 @@ cmd_init(int argc, char *argv[])
 		err(1, "pledge");
 #endif
 
-	while ((ch = getopt(argc, argv, "b:")) != -1) {
+	while ((ch = getopt(argc, argv, "b:t:")) != -1) {
 		switch (ch) {
 		case 'b':
 			head_name = optarg;
 			break;
+		case 't':
+			if (!strcmp(optarg, "sha1"))
+				algo = GOT_HASH_SHA1;
+			else if (!strcmp(optarg, "sha256"))
+				algo = GOT_HASH_SHA256;
+			else
+				return got_error_path(optarg,
+				    GOT_ERR_OBJECT_FORMAT);
+			break;
 		default:
 			usage_init();
 			/* NOTREACHED */
@@ -330,7 +341,7 @@ cmd_init(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	error = got_repo_init(repo_path, head_name);
+	error = got_repo_init(repo_path, head_name, algo);
 done:
 	free(repo_path);
 	return error;
@@ -394,6 +405,9 @@ cmd_info(int argc, char *argv[])
 		goto done;
 
 	printf("repository: %s\n", got_repo_get_path_git_dir(repo));
+	printf("object-format: %s\n",
+	    got_repo_get_object_format(repo) == GOT_HASH_SHA1 ? "sha1"
+	    : "sha256");
 
 	gotconfig = got_repo_get_gotconfig(repo);
 	if (gotconfig) {
blob - c53bf9434590b858c1ce59018954e81ff934dfc1
file + include/got_repository.h
--- include/got_repository.h
+++ include/got_repository.h
@@ -144,7 +144,8 @@ const struct got_error *got_repo_map_path(char **, str
  * Create a new repository with optional specified
  * HEAD ref in an empty directory at a specified path.
  */
-const struct got_error *got_repo_init(const char *, const char *);
+const struct got_error *got_repo_init(const char *, const char *,
+    enum got_hash_algorithm);
 
 /* Attempt to find a unique object ID for a given ID string prefix. */
 const struct got_error *got_repo_match_object_id_prefix(struct got_object_id **,
blob - d39172980ea798fa07c161a3cecc055dfc76136c
file + lib/repository.c
--- lib/repository.c
+++ lib/repository.c
@@ -1723,7 +1723,8 @@ got_repo_unpin_pack(struct got_repository *repo)
 }
 
 const struct got_error *
-got_repo_init(const char *repo_path, const char *head_name)
+got_repo_init(const char *repo_path, const char *head_name,
+    enum got_hash_algorithm algo)
 {
 	const struct got_error *err = NULL;
 	const char *dirnames[] = {
@@ -1734,13 +1735,23 @@ got_repo_init(const char *repo_path, const char *head_
 	const char *description_str = "Unnamed repository; "
 	    "edit this file 'description' to name the repository.";
 	const char *headref = "ref: refs/heads/";
-	const char *gitconfig_str = "[core]\n"
+	const char *gitconfig_sha1 = "[core]\n"
 	    "\trepositoryformatversion = 0\n"
 	    "\tfilemode = true\n"
 	    "\tbare = true\n";
+	const char *gitconfig_sha256 = "[core]\n"
+	    "\trepositoryformatversion = 1\n"
+	    "\tfilemode = true\n"
+	    "\tbare = true\n"
+	    "[extensions]\n"
+	    "\tobjectformat = sha256\n";
+	const char *gitconfig = gitconfig_sha1;
 	char *headref_str, *path;
 	size_t i;
 
+	if (algo == GOT_HASH_SHA256)
+		gitconfig = gitconfig_sha256;
+
 	if (!got_path_dir_is_empty(repo_path))
 		return got_error(GOT_ERR_DIR_NOT_EMPTY);
 
@@ -1776,7 +1787,7 @@ got_repo_init(const char *repo_path, const char *head_
 
 	if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
 		return got_error_from_errno("asprintf");
-	err = got_path_create_file(path, gitconfig_str);
+	err = got_path_create_file(path, gitconfig);
 	free(path);
 	if (err)
 		return err;