commit 38d18775b821d504089ef6268fc301f527ccb169 from: Omar Polo date: Fri Jul 19 19:52:46 2024 UTC gotadmin: add flag to `init' to choose the hashing algorithm Do the same for `got init' too obviously. Repositories created via `clone' are implicitly sha1 since we don't speak the v2 protocol (yet). ok stsp@ commit - ff129794b3e633a240763b063e8966f92f74d299 commit + 38d18775b821d504089ef6268fc301f527ccb169 blob - 0efd11eb1ea05278d857c70ca545f17de8f87588 blob + f2dbcb38ea508b565dbea5ebfbccbf5530868b1c --- 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 blob + 8d7484c497a231b5351f541295c4fa4d6609a5e0 --- 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 A Ar hashing-algorithm Oc Oo Fl b Ar branch Oc Ar repository-path Create a new empty repository at the specified .Ar repository-path . .Pp @@ -108,6 +108,15 @@ The options for .Cm got init are as follows: .Bl -tag -width Ds +.It Fl A Ar hashing-algorithm +Configure the repository's +.Ar hashing-algorithm +used for the computation of Git object IDs. +Possible values are +.Cm sha1 +.Pq the default +or +.Cm sha256 . .It Fl b Ar branch Make the repository's HEAD reference point to the specified .Ar branch blob - 3f403ec8e8aec0b1e344d07f677b0cd872cdaa28 blob + eedba272792e095dfe5a62504a47e88c579c69c8 --- 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 [-A hashing-algorithm] [-b branch]" + " repository-path\n", getprogname()); exit(1); } @@ -368,10 +369,20 @@ 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, "A:b:")) != -1) { switch (ch) { + case 'A': + 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; case 'b': head_name = optarg; break; @@ -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 blob + d9461631d623c70a1fa3f0a27dc61e1fbc5f011a --- 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 A Ar hashing-algorithm Oc Oo Fl b Ar branch Oc Ar repository-path Create a new empty repository at the specified .Ar repository-path . .Pp @@ -86,6 +86,15 @@ The options for .Cm gotadmin init are as follows: .Bl -tag -width Ds +.It Fl A Ar hashing-algorithm +Configure the repository's +.Ar hashing-algorithm +used for the computation of Git object IDs. +Possible values are +.Cm sha1 +.Pq the default +or +.Cm sha256 . .It Fl b Ar branch Make the repository's HEAD reference point to the specified .Ar branch blob - d1a3eb16e15433fc6661b79fd57450c7752f6b39 blob + 6678a9dd097384ee9c7b2545f4a1e9211a83054d --- 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 [-A hashing-algorithm] [-b branch]" + " 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,8 +300,17 @@ cmd_init(int argc, char *argv[]) err(1, "pledge"); #endif - while ((ch = getopt(argc, argv, "b:")) != -1) { + while ((ch = getopt(argc, argv, "A:b:")) != -1) { switch (ch) { + case 'A': + 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; case 'b': head_name = optarg; break; @@ -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; blob - c53bf9434590b858c1ce59018954e81ff934dfc1 blob + e4dd636f145daee8d00edb26040118dabe291813 --- 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 blob + f6c9b881e64ee0270d643a9dc809378060318b2b --- 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,12 +1735,22 @@ 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;