Skip to content

Commit 7695d11

Browse files
rsahlberggitster
authored andcommitted
refs.c: change resolve_ref_unsafe reading argument to be a flags field
resolve_ref_unsafe takes a boolean argument for reading (a nonexistent ref resolves successfully for writing but not for reading). Change this to be a flags field instead, and pass the new constant RESOLVE_REF_READING when we want this behaviour. While at it, swap two of the arguments in the function to put output arguments at the end. As a nice side effect, this ensures that we can catch callers that were unaware of the new API so they can be audited. Give the wrapper functions resolve_refdup and read_ref_full the same treatment for consistency. Signed-off-by: Ronnie Sahlberg <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aae383d commit 7695d11

28 files changed

+124
-92
lines changed

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ int validate_new_branchname(const char *name, struct strbuf *ref,
170170
const char *head;
171171
unsigned char sha1[20];
172172

173-
head = resolve_ref_unsafe("HEAD", sha1, 0, NULL);
173+
head = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
174174
if (!is_bare_repository() && head && !strcmp(head, ref->buf))
175175
die(_("Cannot force update the current branch."));
176176
}

builtin/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2286,7 +2286,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
22862286
commit->date = now;
22872287
parent_tail = &commit->parents;
22882288

2289-
if (!resolve_ref_unsafe("HEAD", head_sha1, 1, NULL))
2289+
if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
22902290
die("no such ref: HEAD");
22912291

22922292
parent_tail = append_parent(parent_tail, head_sha1);

builtin/branch.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ static int branch_merged(int kind, const char *name,
131131
branch->merge[0] &&
132132
branch->merge[0]->dst &&
133133
(reference_name = reference_name_to_free =
134-
resolve_refdup(branch->merge[0]->dst, sha1, 1, NULL)) != NULL)
134+
resolve_refdup(branch->merge[0]->dst, RESOLVE_REF_READING,
135+
sha1, NULL)) != NULL)
135136
reference_rev = lookup_commit_reference(sha1);
136137
}
137138
if (!reference_rev)
@@ -235,7 +236,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
235236
free(name);
236237

237238
name = mkpathdup(fmt, bname.buf);
238-
target = resolve_ref_unsafe(name, sha1, 0, &flags);
239+
target = resolve_ref_unsafe(name, 0, sha1, &flags);
239240
if (!target ||
240241
(!(flags & REF_ISSYMREF) && is_null_sha1(sha1))) {
241242
error(remote_branch
@@ -299,7 +300,7 @@ static char *resolve_symref(const char *src, const char *prefix)
299300
int flag;
300301
const char *dst;
301302

302-
dst = resolve_ref_unsafe(src, sha1, 0, &flag);
303+
dst = resolve_ref_unsafe(src, 0, sha1, &flag);
303304
if (!(dst && (flag & REF_ISSYMREF)))
304305
return NULL;
305306
if (prefix)
@@ -869,7 +870,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
869870

870871
track = git_branch_track;
871872

872-
head = resolve_refdup("HEAD", head_sha1, 0, NULL);
873+
head = resolve_refdup("HEAD", 0, head_sha1, NULL);
873874
if (!head)
874875
die(_("Failed to resolve HEAD as a valid ref."));
875876
if (!strcmp(head, "HEAD"))

builtin/checkout.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ static int checkout_paths(const struct checkout_opts *opts,
355355
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
356356
die(_("unable to write new index file"));
357357

358-
read_ref_full("HEAD", rev, 0, &flag);
358+
read_ref_full("HEAD", 0, rev, &flag);
359359
head = lookup_commit_reference_gently(rev, 1);
360360

361361
errs |= post_checkout_hook(head, head, 0);
@@ -775,7 +775,7 @@ static int switch_branches(const struct checkout_opts *opts,
775775
unsigned char rev[20];
776776
int flag, writeout_error = 0;
777777
memset(&old, 0, sizeof(old));
778-
old.path = path_to_free = resolve_refdup("HEAD", rev, 0, &flag);
778+
old.path = path_to_free = resolve_refdup("HEAD", 0, rev, &flag);
779779
old.commit = lookup_commit_reference_gently(rev, 1);
780780
if (!(flag & REF_ISSYMREF))
781781
old.path = NULL;
@@ -1072,7 +1072,7 @@ static int checkout_branch(struct checkout_opts *opts,
10721072
unsigned char rev[20];
10731073
int flag;
10741074

1075-
if (!read_ref_full("HEAD", rev, 0, &flag) &&
1075+
if (!read_ref_full("HEAD", 0, rev, &flag) &&
10761076
(flag & REF_ISSYMREF) && is_null_sha1(rev))
10771077
return switch_unborn_to_new_branch(opts);
10781078
}

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ static int checkout(void)
623623
if (option_no_checkout)
624624
return 0;
625625

626-
head = resolve_refdup("HEAD", sha1, 1, NULL);
626+
head = resolve_refdup("HEAD", RESOLVE_REF_READING, sha1, NULL);
627627
if (!head) {
628628
warning(_("remote HEAD refers to nonexistent ref, "
629629
"unable to checkout.\n"));

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
15151515
rev.diffopt.break_opt = 0;
15161516
diff_setup_done(&rev.diffopt);
15171517

1518-
head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
1518+
head = resolve_ref_unsafe("HEAD", 0, junk_sha1, NULL);
15191519
if (!strcmp(head, "HEAD"))
15201520
head = _("detached HEAD");
15211521
else

builtin/fmt-merge-msg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
602602

603603
/* get current branch */
604604
current_branch = current_branch_to_free =
605-
resolve_refdup("HEAD", head_sha1, 1, NULL);
605+
resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
606606
if (!current_branch)
607607
die("No current branch");
608608
if (starts_with(current_branch, "refs/heads/"))

builtin/for-each-ref.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,8 @@ static void populate_value(struct refinfo *ref)
635635

636636
if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
637637
unsigned char unused1[20];
638-
ref->symref = resolve_refdup(ref->refname, unused1, 1, NULL);
638+
ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
639+
unused1, NULL);
639640
if (!ref->symref)
640641
ref->symref = "";
641642
}
@@ -693,7 +694,8 @@ static void populate_value(struct refinfo *ref)
693694
const char *head;
694695
unsigned char sha1[20];
695696

696-
head = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
697+
head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
698+
sha1, NULL);
697699
if (!strcmp(ref->refname, head))
698700
v->s = "*";
699701
else

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ static int fsck_head_link(void)
556556
if (verbose)
557557
fprintf(stderr, "Checking HEAD link\n");
558558

559-
head_points_at = resolve_ref_unsafe("HEAD", head_sha1, 0, &flag);
559+
head_points_at = resolve_ref_unsafe("HEAD", 0, head_sha1, &flag);
560560
if (!head_points_at)
561561
return error("Invalid HEAD");
562562
if (!strcmp(head_points_at, "HEAD"))

builtin/log.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
14001400
if (check_head) {
14011401
unsigned char sha1[20];
14021402
const char *ref, *v;
1403-
ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
1403+
ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
1404+
sha1, NULL);
14041405
if (ref && skip_prefix(ref, "refs/heads/", &v))
14051406
branch_name = xstrdup(v);
14061407
else

0 commit comments

Comments
 (0)