Integrate hash algorithm support with repo setup

In future versions of Git, we plan to support an additional hash algorithm. Integrate the enumeration of hash algorithms with repository setup, and store a pointer to the enumerated data in struct repository. Of course, we currently only support SHA-1, so hard-code this value in read_repository_format. In the future, we'll enumerate this value from the configuration. Add a constant, the_hash_algo, which points to the hash_algo structure pointer in the repository global. Note that this is the hash which is used to serialize data to disk, not the hash which is used to display items to the user. The transition plan anticipates that these may be different. We can add an additional element in the future (say, ui_hash_algo) to provide for this case. Include repository.h in cache.h since we now need to have access to these struct and variable definitions. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Nov 12, 2017 at 21:28 UTC 78a6766802ef32b82e6062e8e6ac5621894cc4a2
4 files changed +19
cache.h
+4
@@ -14,6 +14,7 @@
14 #include "hash.h"
15 #include "path.h"
16 #include "sha1-array.h"
17 +#include "repository.h"
18
19 #ifndef platform_SHA_CTX
20 /*
@@ -77,6 +78,8 @@ struct object_id {
78 unsigned char hash[GIT_MAX_RAWSZ];
79 };
80
81 +#define the_hash_algo the_repository->hash_algo
82 +
83 #if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
84 #define DTYPE(de) ((de)->d_type)
85 #else
@@ -888,6 +891,7 @@ struct repository_format {
891 int version;
892 int precious_objects;
893 int is_bare;
894 + int hash_algo;
895 char *work_tree;
896 struct string_list unknown_extensions;
897 };
repository.c
+7
@@ -64,6 +64,11 @@ void repo_set_gitdir(struct repository *repo, const char *path)
64 free(old_gitdir);
65 }
66
67 +void repo_set_hash_algo(struct repository *repo, int hash_algo)
68 +{
69 + repo->hash_algo = &hash_algos[hash_algo];
70 +}
71 +
72 /*
73 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
74 * Return 0 upon success and a non-zero value upon failure.
@@ -136,6 +141,8 @@ int repo_init(struct repository *repo, const char *gitdir, const char *worktree)
141 if (read_and_verify_repository_format(&format, repo->commondir))
142 goto error;
143
144 + repo_set_hash_algo(repo, format.hash_algo);
145 +
146 if (worktree)
147 repo_set_worktree(repo, worktree);
148
repository.h
+5
@@ -4,6 +4,7 @@
4 struct config_set;
5 struct index_state;
6 struct submodule_cache;
7 +struct git_hash_algo;
8
9 struct repository {
10 /* Environment */
@@ -67,6 +68,9 @@ struct repository {
68 */
69 struct index_state *index;
70
71 + /* Repository's current hash algorithm, as serialized on disk. */
72 + const struct git_hash_algo *hash_algo;
73 +
74 /* Configurations */
75 /*
76 * Bit used during initialization to indicate if repository state (like
@@ -86,6 +90,7 @@ extern struct repository *the_repository;
90
91 extern void repo_set_gitdir(struct repository *repo, const char *path);
92 extern void repo_set_worktree(struct repository *repo, const char *path);
93 +extern void repo_set_hash_algo(struct repository *repo, int algo);
94 extern int repo_init(struct repository *repo, const char *gitdir, const char *worktree);
95 extern int repo_submodule_init(struct repository *submodule,
96 struct repository *superproject,
setup.c
+3
@@ -488,6 +488,7 @@ int read_repository_format(struct repository_format *format, const char *path)
488 memset(format, 0, sizeof(*format));
489 format->version = -1;
490 format->is_bare = -1;
491 + format->hash_algo = GIT_HASH_SHA1;
492 string_list_init(&format->unknown_extensions, 1);
493 git_config_from_file(check_repo_format, path, format);
494 return format->version;
@@ -1113,6 +1114,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
1114 repo_set_gitdir(the_repository, gitdir);
1115 setup_git_env();
1116 }
1117 + if (startup_info->have_repository)
1118 + repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
1119 }
1120
1121 strbuf_release(&dir);