Skip to content

Commit d3b2ff7

Browse files
committed
setup: add an escape hatch for "no more default hash algorithm" change
Partially revert c8aed5e (repository: stop setting SHA1 as the default object hash, 2024-05-07), to keep end-user systems still broken when we have gap in our test coverage but yet give them an escape hatch to set the GIT_TEST_DEFAULT_HASH_ALGO environment variable to "sha1" in order to revert to the previous behaviour, in case we haven't done a thorough job in fixing the fallout from c8aed5e. After we build confidence, we should remove the escape hatch support, but we are not there yet after only fixing three commands (hash-object, apply, and patch-id) in this series. Due to the way the end-user facing GIT_DEFAULT_HASH environment variable is used in our test suite, we unfortunately cannot reuse it for this purpose. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 17bc3a4 commit d3b2ff7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

repository.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,57 @@
1919
static struct repository the_repo;
2020
struct repository *the_repository = &the_repo;
2121

22+
/*
23+
* An escape hatch: if we hit a bug in the production code that fails
24+
* to set an appropriate hash algorithm (most likely to happen when
25+
* running outside a repository), we can tell the user who reported
26+
* the crash to set the environment variable to "sha1" (all lowercase)
27+
* to revert to the historical behaviour of defaulting to SHA-1.
28+
*/
29+
static void set_default_hash_algo(struct repository *repo)
30+
{
31+
const char *hash_name;
32+
int algo;
33+
34+
hash_name = getenv("GIT_TEST_DEFAULT_HASH_ALGO");
35+
if (!hash_name)
36+
return;
37+
algo = hash_algo_by_name(hash_name);
38+
if (algo == GIT_HASH_UNKNOWN)
39+
return;
40+
41+
repo_set_hash_algo(repo, algo);
42+
}
43+
2244
void initialize_repository(struct repository *repo)
2345
{
2446
repo->objects = raw_object_store_new();
2547
repo->remote_state = remote_state_new();
2648
repo->parsed_objects = parsed_object_pool_new();
2749
ALLOC_ARRAY(repo->index, 1);
2850
index_state_init(repo->index, repo);
51+
52+
/*
53+
* When a command runs inside a repository, it learns what
54+
* hash algorithm is in use from the repository, but some
55+
* commands are designed to work outside a repository, yet
56+
* they want to access the_hash_algo, if only for the length
57+
* of the hashed value to see if their input looks like a
58+
* plausible hash value.
59+
*
60+
* We are in the process of identifying such code paths and
61+
* giving them an appropriate default individually; any
62+
* unconverted code paths that try to access the_hash_algo
63+
* will thus fail. The end-users however have an escape hatch
64+
* to set GIT_TEST_DEFAULT_HASH_ALGO environment variable to
65+
* "sha1" to get back the old behaviour of defaulting to SHA-1.
66+
*
67+
* This escape hatch is deliberately kept unadvertised, so
68+
* that they see crashes and we can get a report before
69+
* telling them about it.
70+
*/
71+
if (repo == the_repository)
72+
set_default_hash_algo(repo);
2973
}
3074

3175
static void expand_base_dir(char **out, const char *in,

0 commit comments

Comments
 (0)