Skip to content

Commit

Permalink
Merge branch 'ps/use-the-repository'
Browse files Browse the repository at this point in the history
A CPP macro USE_THE_REPOSITORY_VARIABLE is introduced to help
transition the codebase to rely less on the availability of the
singleton the_repository instance.

* ps/use-the-repository:
  hex: guard declarations with `USE_THE_REPOSITORY_VARIABLE`
  t/helper: remove dependency on `the_repository` in "proc-receive"
  t/helper: fix segfault in "oid-array" command without repository
  t/helper: use correct object hash in partial-clone helper
  compat/fsmonitor: fix socket path in networked SHA256 repos
  replace-object: use hash algorithm from passed-in repository
  protocol-caps: use hash algorithm from passed-in repository
  oidset: pass hash algorithm when parsing file
  http-fetch: don't crash when parsing packfile without a repo
  hash-ll: merge with "hash.h"
  refs: avoid include cycle with "repository.h"
  global: introduce `USE_THE_REPOSITORY_VARIABLE` macro
  hash: require hash algorithm in `empty_tree_oid_hex()`
  hash: require hash algorithm in `is_empty_{blob,tree}_oid()`
  hash: make `is_null_oid()` independent of `the_repository`
  hash: convert `oidcmp()` and `oideq()` to compare whole hash
  global: ensure that object IDs are always padded
  hash: require hash algorithm in `oidread()` and `oidclr()`
  hash: require hash algorithm in `hasheq()`, `hashcmp()` and `hashclr()`
  hash: drop (mostly) unused `is_empty_{blob,tree}_sha1()` functions
  • Loading branch information
gitster committed Jul 2, 2024
2 parents ae447ed + dc89b7d commit 7b472da
Show file tree
Hide file tree
Showing 228 changed files with 1,004 additions and 617 deletions.
4 changes: 3 additions & 1 deletion add-interactive.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "add-interactive.h"
#include "color.h"
Expand Down Expand Up @@ -557,7 +559,7 @@ static int get_modified_files(struct repository *r,
s.skip_unseen = filter && i;

opt.def = is_initial ?
empty_tree_oid_hex() : oid_to_hex(&head_oid);
empty_tree_oid_hex(the_repository->hash_algo) : oid_to_hex(&head_oid);

repo_init_revisions(r, &rev, NULL);
setup_revisions(0, NULL, &rev, &opt);
Expand Down
4 changes: 3 additions & 1 deletion add-patch.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "add-interactive.h"
#include "advice.h"
Expand Down Expand Up @@ -421,7 +423,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
/* could be on an unborn branch */
!strcmp("HEAD", s->revision) &&
repo_get_oid(the_repository, "HEAD", &oid) ?
empty_tree_oid_hex() : s->revision);
empty_tree_oid_hex(the_repository->hash_algo) : s->revision);
}
color_arg_index = args.nr;
/* Use `--no-color` explicitly, just in case `diff.color = always`. */
Expand Down
4 changes: 3 additions & 1 deletion apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*
*/

#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "abspath.h"
#include "base85.h"
Expand Down Expand Up @@ -3680,7 +3682,7 @@ static int try_threeway(struct apply_state *state,
if (status) {
patch->conflicted_threeway = 1;
if (patch->is_new)
oidclr(&patch->threeway_stage[0]);
oidclr(&patch->threeway_stage[0], the_repository->hash_algo);
else
oidcpy(&patch->threeway_stage[0], &pre_oid);
oidcpy(&patch->threeway_stage[1], &our_oid);
Expand Down
2 changes: 1 addition & 1 deletion apply.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef APPLY_H
#define APPLY_H

#include "hash-ll.h"
#include "hash.h"
#include "lockfile.h"
#include "string-list.h"
#include "strmap.h"
Expand Down
3 changes: 3 additions & 0 deletions archive-tar.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
* Copyright (c) 2005, 2006 Rene Scharfe
*/

#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "config.h"
#include "gettext.h"
Expand Down
3 changes: 3 additions & 0 deletions archive-zip.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
* Copyright (c) 2006 Rene Scharfe
*/

#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "config.h"
#include "archive.h"
Expand Down
2 changes: 2 additions & 0 deletions archive.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "abspath.h"
#include "config.h"
Expand Down
2 changes: 2 additions & 0 deletions attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* an insanely large number of attributes.
*/

#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "config.h"
#include "environment.h"
Expand Down
2 changes: 2 additions & 0 deletions bisect.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "config.h"
#include "commit.h"
Expand Down
4 changes: 3 additions & 1 deletion blame.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "refs.h"
#include "object-store-ll.h"
Expand Down Expand Up @@ -1246,7 +1248,7 @@ static int fill_blob_sha1_and_mode(struct repository *r,
goto error_out;
return 0;
error_out:
oidclr(&origin->blob_oid);
oidclr(&origin->blob_oid, the_repository->hash_algo);
origin->mode = S_IFINVALID;
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions bloom.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "commit-graph.h"
#include "commit.h"
#include "commit-slab.h"
#include "repository.h"

define_commit_slab(bloom_filter_slab, struct bloom_filter);

Expand Down
2 changes: 2 additions & 0 deletions branch.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "advice.h"
#include "config.h"
Expand Down
8 changes: 8 additions & 0 deletions builtin.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#ifndef BUILTIN_H
#define BUILTIN_H

/*
* TODO: Almost all of our builtins access `the_repository` by necessity
* because they do not get passed a pointer to it. We should adapt the function
* signature of those main functions to accept a `struct repository *` and then
* remove the macro here.
*/
#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"

/*
Expand Down
8 changes: 4 additions & 4 deletions builtin/am.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ static void am_load(struct am_state *state)
read_commit_msg(state);

if (read_state_file(&sb, state, "original-commit", 1) < 0)
oidclr(&state->orig_commit);
oidclr(&state->orig_commit, the_repository->hash_algo);
else if (get_oid_hex(sb.buf, &state->orig_commit) < 0)
die(_("could not parse %s"), am_path(state, "original-commit"));

Expand Down Expand Up @@ -1121,7 +1121,7 @@ static void am_next(struct am_state *state)
unlink(am_path(state, "author-script"));
unlink(am_path(state, "final-commit"));

oidclr(&state->orig_commit);
oidclr(&state->orig_commit, the_repository->hash_algo);
unlink(am_path(state, "original-commit"));
refs_delete_ref(get_main_ref_store(the_repository), NULL,
"REBASE_HEAD", NULL, REF_NO_DEREF);
Expand Down Expand Up @@ -2151,11 +2151,11 @@ static int safe_to_abort(const struct am_state *state)
if (get_oid_hex(sb.buf, &abort_safety))
die(_("could not parse %s"), am_path(state, "abort-safety"));
} else
oidclr(&abort_safety);
oidclr(&abort_safety, the_repository->hash_algo);
strbuf_release(&sb);

if (repo_get_oid(the_repository, "HEAD", &head))
oidclr(&head);
oidclr(&head, the_repository->hash_algo);

if (oideq(&head, &abort_safety))
return 1;
Expand Down
3 changes: 2 additions & 1 deletion builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* See COPYING for licensing conditions
*/

#include "git-compat-util.h"
#include "builtin.h"
#include "config.h"
#include "color.h"
#include "builtin.h"
Expand Down Expand Up @@ -852,6 +852,7 @@ static void build_ignorelist(struct blame_scoreboard *sb,
oidset_clear(&sb->ignore_list);
else
oidset_parse_file_carefully(&sb->ignore_list, i->string,
the_repository->hash_algo,
peel_to_commit_oid, sb);
}
for_each_string_list_item(i, ignore_rev_list) {
Expand Down
2 changes: 1 addition & 1 deletion builtin/fast-export.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ static char *generate_fake_oid(void)
struct object_id oid;
char *hex = xmallocz(GIT_MAX_HEXSZ);

oidclr(&oid);
oidclr(&oid, the_repository->hash_algo);
put_be32(oid.hash + hashsz - 4, counter++);
return oid_to_hex_r(hex, &oid);
}
Expand Down
43 changes: 24 additions & 19 deletions builtin/fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1279,8 +1279,10 @@ static void load_tree(struct tree_entry *root)
e->versions[0].mode = e->versions[1].mode;
e->name = to_atom(c, strlen(c));
c += e->name->str_len + 1;
oidread(&e->versions[0].oid, (unsigned char *)c);
oidread(&e->versions[1].oid, (unsigned char *)c);
oidread(&e->versions[0].oid, (unsigned char *)c,
the_repository->hash_algo);
oidread(&e->versions[1].oid, (unsigned char *)c,
the_repository->hash_algo);
c += the_hash_algo->rawsz;
}
free(buf);
Expand Down Expand Up @@ -1386,7 +1388,7 @@ static void tree_content_replace(
{
if (!S_ISDIR(mode))
die("Root cannot be a non-directory");
oidclr(&root->versions[0].oid);
oidclr(&root->versions[0].oid, the_repository->hash_algo);
oidcpy(&root->versions[1].oid, oid);
if (root->tree)
release_tree_content_recursive(root->tree);
Expand Down Expand Up @@ -1445,7 +1447,7 @@ static int tree_content_set(
if (S_ISDIR(e->versions[0].mode))
e->versions[0].mode |= NO_DELTA;

oidclr(&root->versions[1].oid);
oidclr(&root->versions[1].oid, the_repository->hash_algo);
return 1;
}
if (!S_ISDIR(e->versions[1].mode)) {
Expand All @@ -1455,7 +1457,7 @@ static int tree_content_set(
if (!e->tree)
load_tree(e);
if (tree_content_set(e, slash1 + 1, oid, mode, subtree)) {
oidclr(&root->versions[1].oid);
oidclr(&root->versions[1].oid, the_repository->hash_algo);
return 1;
}
return 0;
Expand All @@ -1467,7 +1469,7 @@ static int tree_content_set(
e = new_tree_entry();
e->name = to_atom(p, n);
e->versions[0].mode = 0;
oidclr(&e->versions[0].oid);
oidclr(&e->versions[0].oid, the_repository->hash_algo);
t->entries[t->entry_count++] = e;
if (*slash1) {
e->tree = new_tree_content(8);
Expand All @@ -1478,7 +1480,7 @@ static int tree_content_set(
e->versions[1].mode = mode;
oidcpy(&e->versions[1].oid, oid);
}
oidclr(&root->versions[1].oid);
oidclr(&root->versions[1].oid, the_repository->hash_algo);
return 1;
}

Expand Down Expand Up @@ -1523,7 +1525,8 @@ static int tree_content_remove(
if (tree_content_remove(e, slash1 + 1, backup_leaf, 0)) {
for (n = 0; n < e->tree->entry_count; n++) {
if (e->tree->entries[n]->versions[1].mode) {
oidclr(&root->versions[1].oid);
oidclr(&root->versions[1].oid,
the_repository->hash_algo);
return 1;
}
}
Expand All @@ -1542,8 +1545,8 @@ static int tree_content_remove(
release_tree_content_recursive(e->tree);
e->tree = NULL;
e->versions[1].mode = 0;
oidclr(&e->versions[1].oid);
oidclr(&root->versions[1].oid);
oidclr(&e->versions[1].oid, the_repository->hash_algo);
oidclr(&root->versions[1].oid, the_repository->hash_algo);
return 1;
}

Expand Down Expand Up @@ -1609,7 +1612,7 @@ static int update_branch(struct branch *b)
return 0;
}
if (refs_read_ref(get_main_ref_store(the_repository), b->name, &old_oid))
oidclr(&old_oid);
oidclr(&old_oid, the_repository->hash_algo);
if (!force_update && !is_null_oid(&old_oid)) {
struct commit *old_cmit, *new_cmit;
int ret;
Expand Down Expand Up @@ -2358,7 +2361,9 @@ static void file_change_m(const char *p, struct branch *b)
parse_path_eol(&path, p, "path");

/* Git does not track empty, non-toplevel directories. */
if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *path.buf) {
if (S_ISDIR(mode) &&
is_empty_tree_oid(&oid, the_repository->hash_algo) &&
*path.buf) {
tree_content_remove(&b->branch_tree, path.buf, NULL, 0);
return;
}
Expand Down Expand Up @@ -2550,8 +2555,8 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
static void file_change_deleteall(struct branch *b)
{
release_tree_content_recursive(b->branch_tree.tree);
oidclr(&b->branch_tree.versions[0].oid);
oidclr(&b->branch_tree.versions[1].oid);
oidclr(&b->branch_tree.versions[0].oid, the_repository->hash_algo);
oidclr(&b->branch_tree.versions[1].oid, the_repository->hash_algo);
load_tree(&b->branch_tree);
b->num_notes = 0;
}
Expand All @@ -2570,8 +2575,8 @@ static void parse_from_commit(struct branch *b, char *buf, unsigned long size)
static void parse_from_existing(struct branch *b)
{
if (is_null_oid(&b->oid)) {
oidclr(&b->branch_tree.versions[0].oid);
oidclr(&b->branch_tree.versions[1].oid);
oidclr(&b->branch_tree.versions[0].oid, the_repository->hash_algo);
oidclr(&b->branch_tree.versions[1].oid, the_repository->hash_algo);
} else {
unsigned long size;
char *buf;
Expand Down Expand Up @@ -2894,9 +2899,9 @@ static void parse_reset_branch(const char *arg)

b = lookup_branch(arg);
if (b) {
oidclr(&b->oid);
oidclr(&b->branch_tree.versions[0].oid);
oidclr(&b->branch_tree.versions[1].oid);
oidclr(&b->oid, the_repository->hash_algo);
oidclr(&b->branch_tree.versions[0].oid, the_repository->hash_algo);
oidclr(&b->branch_tree.versions[1].oid, the_repository->hash_algo);
if (b->branch_tree.tree) {
release_tree_content_recursive(b->branch_tree.tree);
b->branch_tree.tree = NULL;
Expand Down
4 changes: 2 additions & 2 deletions builtin/fetch-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
; /* <oid>, leave oid as name */
} else {
/* <ref>, clear cruft from oid */
oidclr(&oid);
oidclr(&oid, the_repository->hash_algo);
}
} else {
/* <ref>, clear cruft from get_oid_hex */
oidclr(&oid);
oidclr(&oid, the_repository->hash_algo);
}

ref = alloc_ref(name);
Expand Down
11 changes: 6 additions & 5 deletions builtin/index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,8 @@ static void *unpack_raw_entry(struct object_entry *obj,

switch (obj->type) {
case OBJ_REF_DELTA:
oidread(ref_oid, fill(the_hash_algo->rawsz));
oidread(ref_oid, fill(the_hash_algo->rawsz),
the_repository->hash_algo);
use(the_hash_algo->rawsz);
break;
case OBJ_OFS_DELTA:
Expand Down Expand Up @@ -1204,7 +1205,7 @@ static void parse_pack_objects(unsigned char *hash)
the_hash_algo->init_fn(&tmp_ctx);
the_hash_algo->clone_fn(&tmp_ctx, &input_ctx);
the_hash_algo->final_fn(hash, &tmp_ctx);
if (!hasheq(fill(the_hash_algo->rawsz), hash))
if (!hasheq(fill(the_hash_algo->rawsz), hash, the_repository->hash_algo))
die(_("pack is corrupted (SHA1 mismatch)"));
use(the_hash_algo->rawsz);

Expand Down Expand Up @@ -1307,11 +1308,11 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
stop_progress_msg(&progress, msg.buf);
strbuf_release(&msg);
finalize_hashfile(f, tail_hash, FSYNC_COMPONENT_PACK, 0);
hashcpy(read_hash, pack_hash);
hashcpy(read_hash, pack_hash, the_repository->hash_algo);
fixup_pack_header_footer(output_fd, pack_hash,
curr_pack, nr_objects,
read_hash, consumed_bytes-the_hash_algo->rawsz);
if (!hasheq(read_hash, tail_hash))
if (!hasheq(read_hash, tail_hash, the_repository->hash_algo))
die(_("Unexpected tail checksum for %s "
"(disk corruption?)"), curr_pack);
}
Expand Down Expand Up @@ -1372,7 +1373,7 @@ static struct object_entry *append_obj_to_pack(struct hashfile *f,
obj[1].idx.offset += write_compressed(f, buf, size);
obj[0].idx.crc32 = crc32_end(f);
hashflush(f);
oidread(&obj->idx.oid, sha1);
oidread(&obj->idx.oid, sha1, the_repository->hash_algo);
return obj;
}

Expand Down
Loading

0 comments on commit 7b472da

Please sign in to comment.