Skip to content

Commit 7d70b29

Browse files
pks-tgitster
authored andcommitted
hash: stop depending on the_repository in null_oid()
The `null_oid()` function returns the object ID that only consists of zeroes. Naturally, this ID also depends on the hash algorithm used, as the number of zeroes is different between SHA1 and SHA256. Consequently, the function returns the hash-algorithm-specific null object ID. This is currently done by depending on `the_hash_algo`, which implicitly makes us depend on `the_repository`. Refactor the function to instead pass in the hash algorithm for which we want to retrieve the null object ID. Adapt callsites accordingly by passing in `the_repository`, thus bubbling up the dependency on that global variable by one layer. There are a couple of trivial exceptions for subsystems that already got rid of `the_repository`. These subsystems instead use the repository that is available via the calling context: - "builtin/grep.c" - "grep.c" - "refs/debug.c" There are also two non-trivial exceptions: - "diff-no-index.c": Here we know that we may not have a repository initialized at all, so we cannot rely on `the_repository`. Instead, we adapt `diff_no_index()` to get a `struct git_hash_algo` as parameter. The only caller is located in "builtin/diff.c", where we know to call `repo_set_hash_algo()` in case we're running outside of a Git repository. Consequently, it is fine to continue passing `the_repository->hash_algo` even in this case. - "builtin/ls-files.c": There is an in-flight patch series that drops `USE_THE_REPOSITORY_VARIABLE` in this file, which causes a semantic conflict because we use `null_oid()` in `show_submodule()`. The value is passed to `repo_submodule_init()`, which may use the object ID to resolve a tree-ish in the superproject from which we want to read the submodule config. As such, the object ID should refer to an object in the superproject, and consequently we need to use its hash algorithm. This means that we could in theory just not bother about this edge case at all and just use `the_repository` in "diff-no-index.c". But doing so would feel misdesigned. Remove the `USE_THE_REPOSITORY_VARIABLE` preprocessor define in "hash.c". Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8ca9fa6 commit 7d70b29

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+136
-136
lines changed

archive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ int write_archive_entries(struct archiver_args *args,
312312
struct object_id fake_oid;
313313
int i;
314314

315-
oidcpy(&fake_oid, null_oid());
315+
oidcpy(&fake_oid, null_oid(the_hash_algo));
316316

317317
if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
318318
size_t len = args->baselen;

blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ static struct commit *fake_working_tree_commit(struct repository *r,
255255
switch (st.st_mode & S_IFMT) {
256256
case S_IFREG:
257257
if (opt->flags.allow_textconv &&
258-
textconv_object(r, read_from, mode, null_oid(), 0, &buf_ptr, &buf_len))
258+
textconv_object(r, read_from, mode, null_oid(the_hash_algo), 0, &buf_ptr, &buf_len))
259259
strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
260260
else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
261261
die_errno("cannot open or read '%s'", read_from);

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ void create_branch(struct repository *r,
633633
0, &err);
634634
if (!transaction ||
635635
ref_transaction_update(transaction, ref.buf,
636-
&oid, forcing ? NULL : null_oid(),
636+
&oid, forcing ? NULL : null_oid(the_hash_algo),
637637
NULL, NULL, flags, msg, &err) ||
638638
ref_transaction_commit(transaction, &err))
639639
die("%s", err.buf);

builtin/checkout.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ static int post_checkout_hook(struct commit *old_commit, struct commit *new_comm
130130
int changed)
131131
{
132132
return run_hooks_l(the_repository, "post-checkout",
133-
oid_to_hex(old_commit ? &old_commit->object.oid : null_oid()),
134-
oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()),
133+
oid_to_hex(old_commit ? &old_commit->object.oid : null_oid(the_hash_algo)),
134+
oid_to_hex(new_commit ? &new_commit->object.oid : null_oid(the_hash_algo)),
135135
changed ? "1" : "0", NULL);
136136
/* "new_commit" can be NULL when checking out from the index before
137137
a commit exists. */
@@ -710,7 +710,7 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o,
710710
opts.src_index = the_repository->index;
711711
opts.dst_index = the_repository->index;
712712
init_checkout_metadata(&opts.meta, info->refname,
713-
info->commit ? &info->commit->object.oid : null_oid(),
713+
info->commit ? &info->commit->object.oid : null_oid(the_hash_algo),
714714
NULL);
715715
if (parse_tree(tree) < 0)
716716
return 128;

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ static int checkout(int submodule_progress, int filter_submodules,
690690
if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
691691
die(_("unable to write new index file"));
692692

693-
err |= run_hooks_l(the_repository, "post-checkout", oid_to_hex(null_oid()),
693+
err |= run_hooks_l(the_repository, "post-checkout", oid_to_hex(null_oid(the_hash_algo)),
694694
oid_to_hex(&oid), "1", NULL);
695695

696696
if (!err && (option_recurse_submodules.nr > 0)) {

builtin/describe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ static void describe_blob(struct object_id oid, struct strbuf *dst)
518518
{
519519
struct rev_info revs;
520520
struct strvec args = STRVEC_INIT;
521-
struct process_commit_data pcd = { *null_oid(), oid, dst, &revs};
521+
struct process_commit_data pcd = { *null_oid(the_hash_algo), oid, dst, &revs};
522522

523523
strvec_pushl(&args, "internal: The first arg is not parsed",
524524
"--objects", "--in-commit-order", "--reverse", "HEAD",

builtin/diff.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static void builtin_diff_b_f(struct rev_info *revs,
104104

105105
stuff_change(&revs->diffopt,
106106
blob[0]->mode, canon_mode(st.st_mode),
107-
&blob[0]->item->oid, null_oid(),
107+
&blob[0]->item->oid, null_oid(the_hash_algo),
108108
1, 0,
109109
blob[0]->path ? blob[0]->path : path,
110110
path);
@@ -498,7 +498,8 @@ int cmd_diff(int argc,
498498

499499
/* If this is a no-index diff, just run it and exit there. */
500500
if (no_index)
501-
exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
501+
exit(diff_no_index(&rev, the_repository->hash_algo,
502+
no_index == DIFF_NO_INDEX_IMPLICIT,
502503
argc, argv));
503504

504505

builtin/fast-export.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ static void handle_tag(const char *name, struct tag *tag)
869869
p = rewrite_commit((struct commit *)tagged);
870870
if (!p) {
871871
printf("reset %s\nfrom %s\n\n",
872-
name, oid_to_hex(null_oid()));
872+
name, oid_to_hex(null_oid(the_hash_algo)));
873873
free(buf);
874874
return;
875875
}
@@ -883,7 +883,7 @@ static void handle_tag(const char *name, struct tag *tag)
883883

884884
if (tagged->type == OBJ_TAG) {
885885
printf("reset %s\nfrom %s\n\n",
886-
name, oid_to_hex(null_oid()));
886+
name, oid_to_hex(null_oid(the_hash_algo)));
887887
}
888888
skip_prefix(name, "refs/tags/", &name);
889889
printf("tag %s\n", name);
@@ -1023,7 +1023,7 @@ static void handle_tags_and_duplicates(struct string_list *extras)
10231023
* it.
10241024
*/
10251025
printf("reset %s\nfrom %s\n\n",
1026-
name, oid_to_hex(null_oid()));
1026+
name, oid_to_hex(null_oid(the_hash_algo)));
10271027
continue;
10281028
}
10291029

@@ -1042,7 +1042,7 @@ static void handle_tags_and_duplicates(struct string_list *extras)
10421042
if (!reference_excluded_commits) {
10431043
/* delete the ref */
10441044
printf("reset %s\nfrom %s\n\n",
1045-
name, oid_to_hex(null_oid()));
1045+
name, oid_to_hex(null_oid(the_hash_algo)));
10461046
continue;
10471047
}
10481048
/* set ref to commit using oid, not mark */
@@ -1153,7 +1153,7 @@ static void handle_deletes(void)
11531153
continue;
11541154

11551155
printf("reset %s\nfrom %s\n\n",
1156-
refspec->dst, oid_to_hex(null_oid()));
1156+
refspec->dst, oid_to_hex(null_oid(the_hash_algo)));
11571157
}
11581158
}
11591159

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ static int fsck_loose(const struct object_id *oid, const char *path, void *data)
625625
void *contents = NULL;
626626
int eaten;
627627
struct object_info oi = OBJECT_INFO_INIT;
628-
struct object_id real_oid = *null_oid();
628+
struct object_id real_oid = *null_oid(the_hash_algo);
629629
int err = 0;
630630

631631
strbuf_reset(&cb_data->obj_type);

builtin/grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ static int grep_submodule(struct grep_opt *opt,
453453
return 0;
454454

455455
subrepo = xmalloc(sizeof(*subrepo));
456-
if (repo_submodule_init(subrepo, superproject, path, null_oid())) {
456+
if (repo_submodule_init(subrepo, superproject, path, null_oid(opt->repo->hash_algo))) {
457457
free(subrepo);
458458
return 0;
459459
}

0 commit comments

Comments
 (0)