Skip to content

Commit 78a6766

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f50e766 commit 78a6766

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "hash.h"
1515
#include "path.h"
1616
#include "sha1-array.h"
17+
#include "repository.h"
1718

1819
#ifndef platform_SHA_CTX
1920
/*
@@ -77,6 +78,8 @@ struct object_id {
7778
unsigned char hash[GIT_MAX_RAWSZ];
7879
};
7980

81+
#define the_hash_algo the_repository->hash_algo
82+
8083
#if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
8184
#define DTYPE(de) ((de)->d_type)
8285
#else
@@ -888,6 +891,7 @@ struct repository_format {
888891
int version;
889892
int precious_objects;
890893
int is_bare;
894+
int hash_algo;
891895
char *work_tree;
892896
struct string_list unknown_extensions;
893897
};

repository.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ void repo_set_gitdir(struct repository *repo, const char *path)
6464
free(old_gitdir);
6565
}
6666

67+
void repo_set_hash_algo(struct repository *repo, int hash_algo)
68+
{
69+
repo->hash_algo = &hash_algos[hash_algo];
70+
}
71+
6772
/*
6873
* Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
6974
* 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)
136141
if (read_and_verify_repository_format(&format, repo->commondir))
137142
goto error;
138143

144+
repo_set_hash_algo(repo, format.hash_algo);
145+
139146
if (worktree)
140147
repo_set_worktree(repo, worktree);
141148

repository.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
struct config_set;
55
struct index_state;
66
struct submodule_cache;
7+
struct git_hash_algo;
78

89
struct repository {
910
/* Environment */
@@ -67,6 +68,9 @@ struct repository {
6768
*/
6869
struct index_state *index;
6970

71+
/* Repository's current hash algorithm, as serialized on disk. */
72+
const struct git_hash_algo *hash_algo;
73+
7074
/* Configurations */
7175
/*
7276
* Bit used during initialization to indicate if repository state (like
@@ -86,6 +90,7 @@ extern struct repository *the_repository;
8690

8791
extern void repo_set_gitdir(struct repository *repo, const char *path);
8892
extern void repo_set_worktree(struct repository *repo, const char *path);
93+
extern void repo_set_hash_algo(struct repository *repo, int algo);
8994
extern int repo_init(struct repository *repo, const char *gitdir, const char *worktree);
9095
extern int repo_submodule_init(struct repository *submodule,
9196
struct repository *superproject,

setup.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ int read_repository_format(struct repository_format *format, const char *path)
488488
memset(format, 0, sizeof(*format));
489489
format->version = -1;
490490
format->is_bare = -1;
491+
format->hash_algo = GIT_HASH_SHA1;
491492
string_list_init(&format->unknown_extensions, 1);
492493
git_config_from_file(check_repo_format, path, format);
493494
return format->version;
@@ -1113,6 +1114,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
11131114
repo_set_gitdir(the_repository, gitdir);
11141115
setup_git_env();
11151116
}
1117+
if (startup_info->have_repository)
1118+
repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
11161119
}
11171120

11181121
strbuf_release(&dir);

0 commit comments

Comments
 (0)