Skip to content

Commit

Permalink
cocci: apply rules to rewrite callers of "refs" interfaces
Browse files Browse the repository at this point in the history
Apply the rules that rewrite callers of "refs" interfaces to explicitly
pass `struct ref_store`. The resulting patch has been applied with the
`--whitespace=fix` option.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
pks-t authored and gitster committed May 7, 2024
1 parent b198ee0 commit 2e5c475
Show file tree
Hide file tree
Showing 75 changed files with 711 additions and 436 deletions.
17 changes: 11 additions & 6 deletions add-interactive.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,9 @@ static int get_modified_files(struct repository *r,
size_t *binary_count)
{
struct object_id head_oid;
int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
&head_oid, NULL);
int is_initial = !refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
"HEAD", RESOLVE_REF_READING,
&head_oid, NULL);
struct collection_status s = { 0 };
int i;

Expand Down Expand Up @@ -761,8 +762,10 @@ static int run_revert(struct add_i_state *s, const struct pathspec *ps,
size_t count, i, j;

struct object_id oid;
int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid,
NULL);
int is_initial = !refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
"HEAD", RESOLVE_REF_READING,
&oid,
NULL);
struct lock_file index_lock;
const char **paths;
struct tree *tree;
Expand Down Expand Up @@ -990,8 +993,10 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps,
ssize_t count, i;

struct object_id oid;
int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid,
NULL);
int is_initial = !refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
"HEAD", RESOLVE_REF_READING,
&oid,
NULL);
if (get_modified_files(s->r, INDEX_ONLY, files, ps, NULL, NULL) < 0)
return -1;

Expand Down
25 changes: 17 additions & 8 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ static int register_ref(const char *refname, const struct object_id *oid,

static int read_bisect_refs(void)
{
return for_each_ref_in("refs/bisect/", register_ref, NULL);
return refs_for_each_ref_in(get_main_ref_store(the_repository),
"refs/bisect/", register_ref, NULL);
}

static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
Expand Down Expand Up @@ -709,7 +710,7 @@ static enum bisect_error error_if_skipped_commits(struct commit_list *tried,
static int is_expected_rev(const struct object_id *oid)
{
struct object_id expected_oid;
if (read_ref("BISECT_EXPECTED_REV", &expected_oid))
if (refs_read_ref(get_main_ref_store(the_repository), "BISECT_EXPECTED_REV", &expected_oid))
return 0;
return oideq(oid, &expected_oid);
}
Expand All @@ -721,11 +722,14 @@ enum bisect_error bisect_checkout(const struct object_id *bisect_rev,
struct pretty_print_context pp = {0};
struct strbuf commit_msg = STRBUF_INIT;

update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
refs_update_ref(get_main_ref_store(the_repository), NULL,
"BISECT_EXPECTED_REV", bisect_rev, NULL, 0,
UPDATE_REFS_DIE_ON_ERR);

if (no_checkout) {
update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0,
UPDATE_REFS_DIE_ON_ERR);
refs_update_ref(get_main_ref_store(the_repository), NULL,
"BISECT_HEAD", bisect_rev, NULL, 0,
UPDATE_REFS_DIE_ON_ERR);
} else {
struct child_process cmd = CHILD_PROCESS_INIT;

Expand Down Expand Up @@ -1027,7 +1031,8 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
* If no_checkout is non-zero, the bisection process does not
* checkout the trial commit but instead simply updates BISECT_HEAD.
*/
int no_checkout = ref_exists("BISECT_HEAD");
int no_checkout = refs_ref_exists(get_main_ref_store(the_repository),
"BISECT_HEAD");
unsigned bisect_flags = 0;

read_bisect_terms(&term_bad, &term_good);
Expand Down Expand Up @@ -1178,10 +1183,14 @@ int bisect_clean_state(void)

/* There may be some refs packed during bisection */
struct string_list refs_for_removal = STRING_LIST_INIT_NODUP;
for_each_ref_in("refs/bisect", mark_for_removal, (void *) &refs_for_removal);
refs_for_each_ref_in(get_main_ref_store(the_repository),
"refs/bisect", mark_for_removal,
(void *) &refs_for_removal);
string_list_append(&refs_for_removal, xstrdup("BISECT_HEAD"));
string_list_append(&refs_for_removal, xstrdup("BISECT_EXPECTED_REV"));
result = delete_refs("bisect: remove", &refs_for_removal, REF_NO_DEREF);
result = refs_delete_refs(get_main_ref_store(the_repository),
"bisect: remove", &refs_for_removal,
REF_NO_DEREF);
refs_for_removal.strdup_strings = 1;
string_list_clear(&refs_for_removal, 0);
unlink_or_warn(git_path_bisect_ancestors_ok());
Expand Down
4 changes: 2 additions & 2 deletions blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -2700,7 +2700,7 @@ static struct commit *dwim_reverse_initial(struct rev_info *revs,
return NULL;

/* Do we have HEAD? */
if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING, &head_oid, NULL))
return NULL;
head_commit = lookup_commit_reference_gently(revs->repo,
&head_oid, 1);
Expand Down Expand Up @@ -2803,7 +2803,7 @@ void setup_scoreboard(struct blame_scoreboard *sb,
if (sb->final) {
parent_oid = &sb->final->object.oid;
} else {
if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING, &head_oid, NULL))
die("no such ref: HEAD");
parent_oid = &head_oid;
}
Expand Down
5 changes: 3 additions & 2 deletions branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ int validate_branchname(const char *name, struct strbuf *ref)
exit(code);
}

return ref_exists(ref->buf);
return refs_ref_exists(get_main_ref_store(the_repository), ref->buf);
}

static int initialized_checked_out_branches;
Expand Down Expand Up @@ -623,7 +623,8 @@ void create_branch(struct repository *r,
msg = xstrfmt("branch: Reset to %s", start_name);
else
msg = xstrfmt("branch: Created from %s", start_name);
transaction = ref_transaction_begin(&err);
transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
&err);
if (!transaction ||
ref_transaction_update(transaction, ref.buf,
&oid, forcing ? NULL : null_oid(),
Expand Down
38 changes: 24 additions & 14 deletions builtin/am.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,

if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
die_errno(_("failed to create directory '%s'"), state->dir);
delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
refs_delete_ref(get_main_ref_store(the_repository), NULL,
"REBASE_HEAD", NULL, REF_NO_DEREF);

if (split_mail(state, patch_format, paths, keep_cr) < 0) {
am_destroy(state);
Expand Down Expand Up @@ -1081,12 +1082,15 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
if (!repo_get_oid(the_repository, "HEAD", &curr_head)) {
write_state_text(state, "abort-safety", oid_to_hex(&curr_head));
if (!state->rebasing)
update_ref("am", "ORIG_HEAD", &curr_head, NULL, 0,
UPDATE_REFS_DIE_ON_ERR);
refs_update_ref(get_main_ref_store(the_repository),
"am", "ORIG_HEAD", &curr_head, NULL,
0,
UPDATE_REFS_DIE_ON_ERR);
} else {
write_state_text(state, "abort-safety", "");
if (!state->rebasing)
delete_ref(NULL, "ORIG_HEAD", NULL, 0);
refs_delete_ref(get_main_ref_store(the_repository),
NULL, "ORIG_HEAD", NULL, 0);
}

/*
Expand Down Expand Up @@ -1119,7 +1123,8 @@ static void am_next(struct am_state *state)

oidclr(&state->orig_commit);
unlink(am_path(state, "original-commit"));
delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
refs_delete_ref(get_main_ref_store(the_repository), NULL,
"REBASE_HEAD", NULL, REF_NO_DEREF);

if (!repo_get_oid(the_repository, "HEAD", &head))
write_state_text(state, "abort-safety", oid_to_hex(&head));
Expand Down Expand Up @@ -1466,8 +1471,9 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)

oidcpy(&state->orig_commit, &commit_oid);
write_state_text(state, "original-commit", oid_to_hex(&commit_oid));
update_ref("am", "REBASE_HEAD", &commit_oid,
NULL, REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR);
refs_update_ref(get_main_ref_store(the_repository), "am",
"REBASE_HEAD", &commit_oid,
NULL, REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR);

return 0;
}
Expand Down Expand Up @@ -1697,8 +1703,9 @@ static void do_commit(const struct am_state *state)
strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
state->msg);

update_ref(sb.buf, "HEAD", &commit, old_oid, 0,
UPDATE_REFS_DIE_ON_ERR);
refs_update_ref(get_main_ref_store(the_repository), sb.buf, "HEAD",
&commit, old_oid, 0,
UPDATE_REFS_DIE_ON_ERR);

if (state->rebasing) {
FILE *fp = xfopen(am_path(state, "rewritten"), "a");
Expand Down Expand Up @@ -2175,7 +2182,8 @@ static void am_abort(struct am_state *state)

am_rerere_clear();

curr_branch = resolve_refdup("HEAD", 0, &curr_head, NULL);
curr_branch = refs_resolve_refdup(get_main_ref_store(the_repository),
"HEAD", 0, &curr_head, NULL);
has_curr_head = curr_branch && !is_null_oid(&curr_head);
if (!has_curr_head)
oidcpy(&curr_head, the_hash_algo->empty_tree);
Expand All @@ -2188,11 +2196,13 @@ static void am_abort(struct am_state *state)
die(_("failed to clean index"));

if (has_orig_head)
update_ref("am --abort", "HEAD", &orig_head,
has_curr_head ? &curr_head : NULL, 0,
UPDATE_REFS_DIE_ON_ERR);
refs_update_ref(get_main_ref_store(the_repository),
"am --abort", "HEAD", &orig_head,
has_curr_head ? &curr_head : NULL, 0,
UPDATE_REFS_DIE_ON_ERR);
else if (curr_branch)
delete_ref(NULL, curr_branch, NULL, REF_NO_DEREF);
refs_delete_ref(get_main_ref_store(the_repository), NULL,
curr_branch, NULL, REF_NO_DEREF);

free(curr_branch);
am_destroy(state);
Expand Down
44 changes: 26 additions & 18 deletions builtin/bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ static int bisect_reset(const char *commit)
strbuf_addstr(&branch, commit);
}

if (branch.len && !ref_exists("BISECT_HEAD")) {
if (branch.len && !refs_ref_exists(get_main_ref_store(the_repository), "BISECT_HEAD")) {
struct child_process cmd = CHILD_PROCESS_INIT;

cmd.git_cmd = 1;
Expand Down Expand Up @@ -302,8 +302,8 @@ static int bisect_write(const char *state, const char *rev,
goto finish;
}

if (update_ref(NULL, tag.buf, &oid, NULL, 0,
UPDATE_REFS_MSG_ON_ERR)) {
if (refs_update_ref(get_main_ref_store(the_repository), NULL, tag.buf, &oid, NULL, 0,
UPDATE_REFS_MSG_ON_ERR)) {
res = -1;
goto finish;
}
Expand Down Expand Up @@ -416,11 +416,12 @@ static void bisect_status(struct bisect_state *state,
char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
char *good_glob = xstrfmt("%s-*", terms->term_good);

if (ref_exists(bad_ref))
if (refs_ref_exists(get_main_ref_store(the_repository), bad_ref))
state->nr_bad = 1;

for_each_glob_ref_in(inc_nr, good_glob, "refs/bisect/",
(void *) &state->nr_good);
refs_for_each_glob_ref_in(get_main_ref_store(the_repository), inc_nr,
good_glob, "refs/bisect/",
(void *) &state->nr_good);

free(good_glob);
free(bad_ref);
Expand Down Expand Up @@ -574,9 +575,11 @@ static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
reset_revision_walk();
repo_init_revisions(the_repository, revs, NULL);
setup_revisions(0, NULL, revs, NULL);
for_each_glob_ref_in(add_bisect_ref, bad, "refs/bisect/", &cb);
refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
add_bisect_ref, bad, "refs/bisect/", &cb);
cb.object_flags = UNINTERESTING;
for_each_glob_ref_in(add_bisect_ref, good, "refs/bisect/", &cb);
refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
add_bisect_ref, good, "refs/bisect/", &cb);
if (prepare_revision_walk(revs))
res = error(_("revision walk setup failed\n"));

Expand Down Expand Up @@ -636,7 +639,7 @@ static int bisect_successful(struct bisect_terms *terms)
char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad);
int res;

read_ref(bad_ref, &oid);
refs_read_ref(get_main_ref_store(the_repository), bad_ref, &oid);
commit = lookup_commit_reference_by_name(bad_ref);
repo_format_commit_message(the_repository, commit, "%s", &commit_name,
&pp);
Expand Down Expand Up @@ -779,7 +782,8 @@ static enum bisect_error bisect_start(struct bisect_terms *terms, int argc,
/*
* Verify HEAD
*/
head = resolve_ref_unsafe("HEAD", 0, &head_oid, &flags);
head = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
"HEAD", 0, &head_oid, &flags);
if (!head)
if (repo_get_oid(the_repository, "HEAD", &head_oid))
return error(_("bad HEAD - I need a HEAD"));
Expand Down Expand Up @@ -838,8 +842,8 @@ static enum bisect_error bisect_start(struct bisect_terms *terms, int argc,
res = error(_("invalid ref: '%s'"), start_head.buf);
goto finish;
}
if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0,
UPDATE_REFS_MSG_ON_ERR)) {
if (refs_update_ref(get_main_ref_store(the_repository), NULL, "BISECT_HEAD", &oid, NULL, 0,
UPDATE_REFS_MSG_ON_ERR)) {
res = BISECT_FAILED;
goto finish;
}
Expand Down Expand Up @@ -972,7 +976,7 @@ static enum bisect_error bisect_state(struct bisect_terms *terms, int argc,
oid_array_append(&revs, &commit->object.oid);
}

if (read_ref("BISECT_EXPECTED_REV", &expected))
if (refs_read_ref(get_main_ref_store(the_repository), "BISECT_EXPECTED_REV", &expected))
verify_expected = 0; /* Ignore invalid file contents */

for (i = 0; i < revs.nr; i++) {
Expand All @@ -982,7 +986,9 @@ static enum bisect_error bisect_state(struct bisect_terms *terms, int argc,
}
if (verify_expected && !oideq(&revs.oid[i], &expected)) {
unlink_or_warn(git_path_bisect_ancestors_ok());
delete_ref(NULL, "BISECT_EXPECTED_REV", NULL, REF_NO_DEREF);
refs_delete_ref(get_main_ref_store(the_repository),
NULL, "BISECT_EXPECTED_REV", NULL,
REF_NO_DEREF);
verify_expected = 0;
}
}
Expand Down Expand Up @@ -1179,13 +1185,15 @@ static int verify_good(const struct bisect_terms *terms, const char *command)
struct object_id good_rev;
struct object_id current_rev;
char *good_glob = xstrfmt("%s-*", terms->term_good);
int no_checkout = ref_exists("BISECT_HEAD");
int no_checkout = refs_ref_exists(get_main_ref_store(the_repository),
"BISECT_HEAD");

for_each_glob_ref_in(get_first_good, good_glob, "refs/bisect/",
&good_rev);
refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
get_first_good, good_glob, "refs/bisect/",
&good_rev);
free(good_glob);

if (read_ref(no_checkout ? "BISECT_HEAD" : "HEAD", &current_rev))
if (refs_read_ref(get_main_ref_store(the_repository), no_checkout ? "BISECT_HEAD" : "HEAD", &current_rev))
return -1;

res = bisect_checkout(&good_rev, no_checkout);
Expand Down
4 changes: 2 additions & 2 deletions builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,8 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
struct commit *head_commit;
struct object_id head_oid;

if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
&head_oid, NULL) ||
if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING,
&head_oid, NULL) ||
!(head_commit = lookup_commit_reference_gently(revs.repo,
&head_oid, 1)))
die("no such ref: HEAD");
Expand Down
Loading

0 comments on commit 2e5c475

Please sign in to comment.