Skip to content

Commit

Permalink
hash: provide per-algorithm null OIDs
Browse files Browse the repository at this point in the history
Up until recently, object IDs did not have an algorithm member, only a
hash.  Consequently, it was possible to share one null (all-zeros)
object ID among all hash algorithms.  Now that we're going to be
handling objects from multiple hash algorithms, it's important to make
sure that all object IDs have a correct algorithm field.

Introduce a per-algorithm null OID, and add it to struct hash_algo.
Introduce a wrapper function as well, and use it everywhere we used to
use the null_oid constant.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
bk2204 authored and gitster committed Apr 27, 2021
1 parent 5a6dce7 commit 1422844
Show file tree
Hide file tree
Showing 42 changed files with 119 additions and 95 deletions.
4 changes: 3 additions & 1 deletion archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,11 @@ int write_archive_entries(struct archiver_args *args,
int err;
struct strbuf path_in_archive = STRBUF_INIT;
struct strbuf content = STRBUF_INIT;
struct object_id fake_oid = null_oid;
struct object_id fake_oid;
int i;

oidcpy(&fake_oid, null_oid());

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

Expand Down
2 changes: 1 addition & 1 deletion blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ static struct commit *fake_working_tree_commit(struct repository *r,
switch (st.st_mode & S_IFMT) {
case S_IFREG:
if (opt->flags.allow_textconv &&
textconv_object(r, read_from, mode, &null_oid, 0, &buf_ptr, &buf_len))
textconv_object(r, read_from, mode, null_oid(), 0, &buf_ptr, &buf_len))
strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
die_errno("cannot open or read '%s'", read_from);
Expand Down
2 changes: 1 addition & 1 deletion branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void create_branch(struct repository *r,
transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, ref.buf,
&oid, forcing ? NULL : &null_oid,
&oid, forcing ? NULL : null_oid(),
0, msg, &err) ||
ref_transaction_commit(transaction, &err))
die("%s", err.buf);
Expand Down
6 changes: 3 additions & 3 deletions builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ static int post_checkout_hook(struct commit *old_commit, struct commit *new_comm
int changed)
{
return run_hook_le(NULL, "post-checkout",
oid_to_hex(old_commit ? &old_commit->object.oid : &null_oid),
oid_to_hex(new_commit ? &new_commit->object.oid : &null_oid),
oid_to_hex(old_commit ? &old_commit->object.oid : null_oid()),
oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()),
changed ? "1" : "0", NULL);
/* "new_commit" can be NULL when checking out from the index before
a commit exists. */
Expand Down Expand Up @@ -638,7 +638,7 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o,
opts.src_index = &the_index;
opts.dst_index = &the_index;
init_checkout_metadata(&opts.meta, info->refname,
info->commit ? &info->commit->object.oid : &null_oid,
info->commit ? &info->commit->object.oid : null_oid(),
NULL);
parse_tree(tree);
init_tree_desc(&tree_desc, tree->buffer, tree->size);
Expand Down
2 changes: 1 addition & 1 deletion builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ static int checkout(int submodule_progress)
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
die(_("unable to write new index file"));

err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
err |= run_hook_le(NULL, "post-checkout", oid_to_hex(null_oid()),
oid_to_hex(&oid), "1", NULL);

if (!err && (option_recurse_submodules.nr > 0)) {
Expand Down
2 changes: 1 addition & 1 deletion builtin/describe.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ static void describe_blob(struct object_id oid, struct strbuf *dst)
{
struct rev_info revs;
struct strvec args = STRVEC_INIT;
struct process_commit_data pcd = { null_oid, oid, dst, &revs};
struct process_commit_data pcd = { *null_oid(), oid, dst, &revs};

strvec_pushl(&args, "internal: The first arg is not parsed",
"--objects", "--in-commit-order", "--reverse", "HEAD",
Expand Down
2 changes: 1 addition & 1 deletion builtin/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static int builtin_diff_b_f(struct rev_info *revs,

stuff_change(&revs->diffopt,
blob[0]->mode, canon_mode(st.st_mode),
&blob[0]->item->oid, &null_oid,
&blob[0]->item->oid, null_oid(),
1, 0,
blob[0]->path ? blob[0]->path : path,
path);
Expand Down
10 changes: 5 additions & 5 deletions builtin/fast-export.c
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ static void handle_tag(const char *name, struct tag *tag)
p = rewrite_commit((struct commit *)tagged);
if (!p) {
printf("reset %s\nfrom %s\n\n",
name, oid_to_hex(&null_oid));
name, oid_to_hex(null_oid()));
free(buf);
return;
}
Expand All @@ -884,7 +884,7 @@ static void handle_tag(const char *name, struct tag *tag)

if (tagged->type == OBJ_TAG) {
printf("reset %s\nfrom %s\n\n",
name, oid_to_hex(&null_oid));
name, oid_to_hex(null_oid()));
}
skip_prefix(name, "refs/tags/", &name);
printf("tag %s\n", name);
Expand Down Expand Up @@ -1016,7 +1016,7 @@ static void handle_tags_and_duplicates(struct string_list *extras)
* it.
*/
printf("reset %s\nfrom %s\n\n",
name, oid_to_hex(&null_oid));
name, oid_to_hex(null_oid()));
continue;
}

Expand All @@ -1035,7 +1035,7 @@ static void handle_tags_and_duplicates(struct string_list *extras)
if (!reference_excluded_commits) {
/* delete the ref */
printf("reset %s\nfrom %s\n\n",
name, oid_to_hex(&null_oid));
name, oid_to_hex(null_oid()));
continue;
}
/* set ref to commit using oid, not mark */
Expand Down Expand Up @@ -1146,7 +1146,7 @@ static void handle_deletes(void)
continue;

printf("reset %s\nfrom %s\n\n",
refspec->dst, oid_to_hex(&null_oid));
refspec->dst, oid_to_hex(null_oid()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion builtin/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ static int grep_submodule(struct grep_opt *opt,
struct grep_opt subopt;
int hit;

sub = submodule_from_path(superproject, &null_oid, path);
sub = submodule_from_path(superproject, null_oid(), path);

if (!is_submodule_active(superproject, path))
return 0;
Expand Down
2 changes: 1 addition & 1 deletion builtin/ls-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ static void show_submodule(struct repository *superproject,
{
struct repository subrepo;
const struct submodule *sub = submodule_from_path(superproject,
&null_oid, path);
null_oid(), path);

if (repo_submodule_init(&subrepo, superproject, sub))
return;
Expand Down
4 changes: 2 additions & 2 deletions builtin/rebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ static const char * const builtin_rebase_interactive_usage[] = {
int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
{
struct rebase_options opts = REBASE_OPTIONS_INIT;
struct object_id squash_onto = null_oid;
struct object_id squash_onto = *null_oid();
enum action command = ACTION_NONE;
struct option options[] = {
OPT_NEGBIT(0, "ff", &opts.flags, N_("allow fast-forward"),
Expand Down Expand Up @@ -1139,7 +1139,7 @@ static int can_fast_forward(struct commit *onto, struct commit *upstream,

merge_bases = get_merge_bases(onto, head);
if (!merge_bases || merge_bases->next) {
oidcpy(merge_base, &null_oid);
oidcpy(merge_base, null_oid());
goto done;
}

Expand Down
2 changes: 1 addition & 1 deletion builtin/receive-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ static void write_head_info(void)
for_each_alternate_ref(show_one_alternate_ref, &seen);
oidset_clear(&seen);
if (!sent_capabilities)
show_ref("capabilities^{}", &null_oid);
show_ref("capabilities^{}", null_oid());

advertise_shallow_grafts(1);

Expand Down
29 changes: 15 additions & 14 deletions builtin/submodule--helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ static int module_list(int argc, const char **argv, const char *prefix)
const struct cache_entry *ce = list.entries[i];

if (ce_stage(ce))
printf("%06o %s U\t", ce->ce_mode, oid_to_hex(&null_oid));
printf("%06o %s U\t", ce->ce_mode,
oid_to_hex(null_oid()));
else
printf("%06o %s %d\t", ce->ce_mode,
oid_to_hex(&ce->oid), ce_stage(ce));
Expand Down Expand Up @@ -466,7 +467,7 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item,

displaypath = get_submodule_displaypath(path, info->prefix);

sub = submodule_from_path(the_repository, &null_oid, path);
sub = submodule_from_path(the_repository, null_oid(), path);

if (!sub)
die(_("No url found for submodule path '%s' in .gitmodules"),
Expand Down Expand Up @@ -623,7 +624,7 @@ static void init_submodule(const char *path, const char *prefix,

displaypath = get_submodule_displaypath(path, prefix);

sub = submodule_from_path(the_repository, &null_oid, path);
sub = submodule_from_path(the_repository, null_oid(), path);

if (!sub)
die(_("No url found for submodule path '%s' in .gitmodules"),
Expand Down Expand Up @@ -783,14 +784,14 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
struct strbuf buf = STRBUF_INIT;
const char *git_dir;

if (!submodule_from_path(the_repository, &null_oid, path))
if (!submodule_from_path(the_repository, null_oid(), path))
die(_("no submodule mapping found in .gitmodules for path '%s'"),
path);

displaypath = get_submodule_displaypath(path, prefix);

if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
print_status(flags, 'U', path, &null_oid, displaypath);
print_status(flags, 'U', path, null_oid(), displaypath);
goto cleanup;
}

Expand Down Expand Up @@ -916,7 +917,7 @@ static int module_name(int argc, const char **argv, const char *prefix)
if (argc != 2)
usage(_("git submodule--helper name <path>"));

sub = submodule_from_path(the_repository, &null_oid, argv[1]);
sub = submodule_from_path(the_repository, null_oid(), argv[1]);

if (!sub)
die(_("no submodule mapping found in .gitmodules for path '%s'"),
Expand Down Expand Up @@ -1040,7 +1041,7 @@ static void generate_submodule_summary(struct summary_cb *info,
char *errmsg = NULL;
int total_commits = -1;

if (!info->cached && oideq(&p->oid_dst, &null_oid)) {
if (!info->cached && oideq(&p->oid_dst, null_oid())) {
if (S_ISGITLINK(p->mod_dst)) {
struct ref_store *refs = get_submodule_ref_store(p->sm_path);
if (refs)
Expand Down Expand Up @@ -1177,7 +1178,7 @@ static void prepare_submodule_summary(struct summary_cb *info,

if (info->for_status && p->status != 'A' &&
(sub = submodule_from_path(the_repository,
&null_oid, p->sm_path))) {
null_oid(), p->sm_path))) {
char *config_key = NULL;
const char *value;
int ignore_all = 0;
Expand Down Expand Up @@ -1373,7 +1374,7 @@ static void sync_submodule(const char *path, const char *prefix,
if (!is_submodule_active(the_repository, path))
return;

sub = submodule_from_path(the_repository, &null_oid, path);
sub = submodule_from_path(the_repository, null_oid(), path);

if (sub && sub->url) {
if (starts_with_dot_dot_slash(sub->url) ||
Expand Down Expand Up @@ -1525,7 +1526,7 @@ static void deinit_submodule(const char *path, const char *prefix,
struct strbuf sb_config = STRBUF_INIT;
char *sub_git_dir = xstrfmt("%s/.git", path);

sub = submodule_from_path(the_repository, &null_oid, path);
sub = submodule_from_path(the_repository, null_oid(), path);

if (!sub || !sub->name)
goto cleanup;
Expand Down Expand Up @@ -1925,7 +1926,7 @@ static void determine_submodule_update_strategy(struct repository *r,
const char *update,
struct submodule_update_strategy *out)
{
const struct submodule *sub = submodule_from_path(r, &null_oid, path);
const struct submodule *sub = submodule_from_path(r, null_oid(), path);
char *key;
const char *val;

Expand Down Expand Up @@ -2077,7 +2078,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
goto cleanup;
}

sub = submodule_from_path(the_repository, &null_oid, ce->name);
sub = submodule_from_path(the_repository, null_oid(), ce->name);

if (suc->recursive_prefix)
displaypath = relative_path(suc->recursive_prefix,
Expand Down Expand Up @@ -2395,7 +2396,7 @@ static const char *remote_submodule_branch(const char *path)
const char *branch = NULL;
char *key;

sub = submodule_from_path(the_repository, &null_oid, path);
sub = submodule_from_path(the_repository, null_oid(), path);
if (!sub)
return NULL;

Expand Down Expand Up @@ -2533,7 +2534,7 @@ static int ensure_core_worktree(int argc, const char **argv, const char *prefix)

path = argv[1];

sub = submodule_from_path(the_repository, &null_oid, path);
sub = submodule_from_path(the_repository, null_oid(), path);
if (!sub)
BUG("We could get the submodule handle before?");

Expand Down
3 changes: 2 additions & 1 deletion builtin/unpack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
* has not been resolved yet.
*/
oidclr(&obj_list[nr].oid);
add_delta_to_list(nr, &null_oid, base_offset, delta_data, delta_size);
add_delta_to_list(nr, null_oid(), base_offset,
delta_data, delta_size);
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions builtin/worktree.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ static int add_worktree(const char *path, const char *refname,
*/
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
write_file(sb.buf, "%s", oid_to_hex(&null_oid));
write_file(sb.buf, "%s", oid_to_hex(null_oid()));
strbuf_reset(&sb);
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
write_file(sb.buf, "../..");
Expand Down Expand Up @@ -394,7 +394,7 @@ static int add_worktree(const char *path, const char *refname,
cp.argv = NULL;
cp.trace2_hook_name = "post-checkout";
strvec_pushl(&cp.args, absolute_path(hook),
oid_to_hex(&null_oid),
oid_to_hex(null_oid()),
oid_to_hex(&commit->object.oid),
"1", NULL);
ret = run_command(&cp);
Expand Down
2 changes: 1 addition & 1 deletion combine-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
&result_size, NULL, NULL);
} else if (textconv) {
struct diff_filespec *df = alloc_filespec(elem->path);
fill_filespec(df, &null_oid, 0, st.st_mode);
fill_filespec(df, null_oid(), 0, st.st_mode);
result_size = fill_textconv(opt->repo, textconv, df, &result);
free_filespec(df);
} else if (0 <= (fd = open(elem->path, O_RDONLY))) {
Expand Down
2 changes: 1 addition & 1 deletion connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ static int process_dummy_ref(const struct packet_reader *reader)
return 0;
name++;

return oideq(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
return oideq(null_oid(), &oid) && !strcmp(name, "capabilities^{}");
}

static void check_no_capabilities(const char *line, int len)
Expand Down
6 changes: 3 additions & 3 deletions diff-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
ce_intent_to_add(ce)) {
newmode = ce_mode_from_stat(ce, st.st_mode);
diff_addremove(&revs->diffopt, '+', newmode,
&null_oid, 0, ce->name, 0);
null_oid(), 0, ce->name, 0);
continue;
}

Expand All @@ -249,7 +249,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
}
oldmode = ce->ce_mode;
old_oid = &ce->oid;
new_oid = changed ? &null_oid : &ce->oid;
new_oid = changed ? null_oid() : &ce->oid;
diff_change(&revs->diffopt, oldmode, newmode,
old_oid, new_oid,
!is_null_oid(old_oid),
Expand Down Expand Up @@ -307,7 +307,7 @@ static int get_stat_data(const struct index_state *istate,
0, dirty_submodule);
if (changed) {
mode = ce_mode_from_stat(ce, st.st_mode);
oid = &null_oid;
oid = null_oid();
}
}

Expand Down
2 changes: 1 addition & 1 deletion diff-no-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static struct diff_filespec *noindex_filespec(const char *name, int mode)
if (!name)
name = "/dev/null";
s = alloc_filespec(name);
fill_filespec(s, &null_oid, 0, mode);
fill_filespec(s, null_oid(), 0, mode);
if (name == file_from_standard_input)
populate_from_stdin(s);
return s;
Expand Down
4 changes: 2 additions & 2 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -4190,7 +4190,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
die_errno("readlink(%s)", name);
prep_temp_blob(r->index, name, temp, sb.buf, sb.len,
(one->oid_valid ?
&one->oid : &null_oid),
&one->oid : null_oid()),
(one->oid_valid ?
one->mode : S_IFLNK));
strbuf_release(&sb);
Expand All @@ -4199,7 +4199,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
/* we can borrow from the file in the work tree */
temp->name = name;
if (!one->oid_valid)
oid_to_hex_r(temp->hex, &null_oid);
oid_to_hex_r(temp->hex, null_oid());
else
oid_to_hex_r(temp->hex, &one->oid);
/* Even though we may sometimes borrow the
Expand Down
Loading

0 comments on commit 1422844

Please sign in to comment.