Skip to content

Commit

Permalink
refs.c: change resolve_ref_unsafe reading argument to be a flags field
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rsahlberg authored and gitster committed Oct 15, 2014
1 parent aae383d commit 7695d11
Show file tree
Hide file tree
Showing 28 changed files with 124 additions and 92 deletions.
2 changes: 1 addition & 1 deletion branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ int validate_new_branchname(const char *name, struct strbuf *ref,
const char *head;
unsigned char sha1[20];

head = resolve_ref_unsafe("HEAD", sha1, 0, NULL);
head = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
if (!is_bare_repository() && head && !strcmp(head, ref->buf))
die(_("Cannot force update the current branch."));
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -2286,7 +2286,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
commit->date = now;
parent_tail = &commit->parents;

if (!resolve_ref_unsafe("HEAD", head_sha1, 1, NULL))
if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
die("no such ref: HEAD");

parent_tail = append_parent(parent_tail, head_sha1);
Expand Down
9 changes: 5 additions & 4 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ static int branch_merged(int kind, const char *name,
branch->merge[0] &&
branch->merge[0]->dst &&
(reference_name = reference_name_to_free =
resolve_refdup(branch->merge[0]->dst, sha1, 1, NULL)) != NULL)
resolve_refdup(branch->merge[0]->dst, RESOLVE_REF_READING,
sha1, NULL)) != NULL)
reference_rev = lookup_commit_reference(sha1);
}
if (!reference_rev)
Expand Down Expand Up @@ -235,7 +236,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
free(name);

name = mkpathdup(fmt, bname.buf);
target = resolve_ref_unsafe(name, sha1, 0, &flags);
target = resolve_ref_unsafe(name, 0, sha1, &flags);
if (!target ||
(!(flags & REF_ISSYMREF) && is_null_sha1(sha1))) {
error(remote_branch
Expand Down Expand Up @@ -299,7 +300,7 @@ static char *resolve_symref(const char *src, const char *prefix)
int flag;
const char *dst;

dst = resolve_ref_unsafe(src, sha1, 0, &flag);
dst = resolve_ref_unsafe(src, 0, sha1, &flag);
if (!(dst && (flag & REF_ISSYMREF)))
return NULL;
if (prefix)
Expand Down Expand Up @@ -869,7 +870,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)

track = git_branch_track;

head = resolve_refdup("HEAD", head_sha1, 0, NULL);
head = resolve_refdup("HEAD", 0, head_sha1, NULL);
if (!head)
die(_("Failed to resolve HEAD as a valid ref."));
if (!strcmp(head, "HEAD"))
Expand Down
6 changes: 3 additions & 3 deletions builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ static int checkout_paths(const struct checkout_opts *opts,
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
die(_("unable to write new index file"));

read_ref_full("HEAD", rev, 0, &flag);
read_ref_full("HEAD", 0, rev, &flag);
head = lookup_commit_reference_gently(rev, 1);

errs |= post_checkout_hook(head, head, 0);
Expand Down Expand Up @@ -775,7 +775,7 @@ static int switch_branches(const struct checkout_opts *opts,
unsigned char rev[20];
int flag, writeout_error = 0;
memset(&old, 0, sizeof(old));
old.path = path_to_free = resolve_refdup("HEAD", rev, 0, &flag);
old.path = path_to_free = resolve_refdup("HEAD", 0, rev, &flag);
old.commit = lookup_commit_reference_gently(rev, 1);
if (!(flag & REF_ISSYMREF))
old.path = NULL;
Expand Down Expand Up @@ -1072,7 +1072,7 @@ static int checkout_branch(struct checkout_opts *opts,
unsigned char rev[20];
int flag;

if (!read_ref_full("HEAD", rev, 0, &flag) &&
if (!read_ref_full("HEAD", 0, rev, &flag) &&
(flag & REF_ISSYMREF) && is_null_sha1(rev))
return switch_unborn_to_new_branch(opts);
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ static int checkout(void)
if (option_no_checkout)
return 0;

head = resolve_refdup("HEAD", sha1, 1, NULL);
head = resolve_refdup("HEAD", RESOLVE_REF_READING, sha1, NULL);
if (!head) {
warning(_("remote HEAD refers to nonexistent ref, "
"unable to checkout.\n"));
Expand Down
2 changes: 1 addition & 1 deletion builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
rev.diffopt.break_opt = 0;
diff_setup_done(&rev.diffopt);

head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
head = resolve_ref_unsafe("HEAD", 0, junk_sha1, NULL);
if (!strcmp(head, "HEAD"))
head = _("detached HEAD");
else
Expand Down
2 changes: 1 addition & 1 deletion builtin/fmt-merge-msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,

/* get current branch */
current_branch = current_branch_to_free =
resolve_refdup("HEAD", head_sha1, 1, NULL);
resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
if (!current_branch)
die("No current branch");
if (starts_with(current_branch, "refs/heads/"))
Expand Down
6 changes: 4 additions & 2 deletions builtin/for-each-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,8 @@ static void populate_value(struct refinfo *ref)

if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
unsigned char unused1[20];
ref->symref = resolve_refdup(ref->refname, unused1, 1, NULL);
ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
unused1, NULL);
if (!ref->symref)
ref->symref = "";
}
Expand Down Expand Up @@ -693,7 +694,8 @@ static void populate_value(struct refinfo *ref)
const char *head;
unsigned char sha1[20];

head = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
sha1, NULL);
if (!strcmp(ref->refname, head))
v->s = "*";
else
Expand Down
2 changes: 1 addition & 1 deletion builtin/fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ static int fsck_head_link(void)
if (verbose)
fprintf(stderr, "Checking HEAD link\n");

head_points_at = resolve_ref_unsafe("HEAD", head_sha1, 0, &flag);
head_points_at = resolve_ref_unsafe("HEAD", 0, head_sha1, &flag);
if (!head_points_at)
return error("Invalid HEAD");
if (!strcmp(head_points_at, "HEAD"))
Expand Down
3 changes: 2 additions & 1 deletion builtin/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
if (check_head) {
unsigned char sha1[20];
const char *ref, *v;
ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
sha1, NULL);
if (ref && skip_prefix(ref, "refs/heads/", &v))
branch_name = xstrdup(v);
else
Expand Down
2 changes: 1 addition & 1 deletion builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
* Check if we are _not_ on a detached HEAD, i.e. if there is a
* current branch.
*/
branch = branch_to_free = resolve_refdup("HEAD", head_sha1, 0, &flag);
branch = branch_to_free = resolve_refdup("HEAD", 0, head_sha1, &flag);
if (branch && starts_with(branch, "refs/heads/"))
branch += 11;
if (!branch || is_null_sha1(head_sha1))
Expand Down
2 changes: 1 addition & 1 deletion builtin/notes.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ static int merge_commit(struct notes_merge_options *o)
init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);

o->local_ref = local_ref_to_free =
resolve_refdup("NOTES_MERGE_REF", sha1, 0, NULL);
resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL);
if (!o->local_ref)
die("Failed to resolve NOTES_MERGE_REF");

Expand Down
4 changes: 2 additions & 2 deletions builtin/receive-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
int flag;

strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
dst_name = resolve_ref_unsafe(buf.buf, 0, sha1, &flag);
strbuf_release(&buf);

if (!(flag & REF_ISSYMREF))
Expand Down Expand Up @@ -1070,7 +1070,7 @@ static void execute_commands(struct command *commands,
check_aliased_updates(commands);

free(head_name_to_free);
head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
head_name = head_name_to_free = resolve_refdup("HEAD", 0, sha1, NULL);

checked_connectivity = 1;
for (cmd = commands; cmd; cmd = cmd->next) {
Expand Down
5 changes: 3 additions & 2 deletions builtin/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ static int read_remote_branches(const char *refname,
strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
if (starts_with(refname, buf.buf)) {
item = string_list_append(rename->remote_branches, xstrdup(refname));
symref = resolve_ref_unsafe(refname, orig_sha1, 1, &flag);
symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
orig_sha1, &flag);
if (flag & REF_ISSYMREF)
item->util = xstrdup(symref);
else
Expand Down Expand Up @@ -703,7 +704,7 @@ static int mv(int argc, const char **argv)
int flag = 0;
unsigned char sha1[20];

read_ref_full(item->string, sha1, 1, &flag);
read_ref_full(item->string, RESOLVE_REF_READING, sha1, &flag);
if (!(flag & REF_ISSYMREF))
continue;
if (delete_ref(item->string, NULL, REF_NODEREF))
Expand Down
7 changes: 5 additions & 2 deletions builtin/show-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
if (ac == 0) {
static const char *fake_av[2];

fake_av[0] = resolve_refdup("HEAD", sha1, 1, NULL);
fake_av[0] = resolve_refdup("HEAD",
RESOLVE_REF_READING,
sha1, NULL);
fake_av[1] = NULL;
av = fake_av;
ac = 1;
Expand Down Expand Up @@ -789,7 +791,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
}
}

head_p = resolve_ref_unsafe("HEAD", head_sha1, 1, NULL);
head_p = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
head_sha1, NULL);
if (head_p) {
head_len = strlen(head_p);
memcpy(head, head_p, head_len + 1);
Expand Down
2 changes: 1 addition & 1 deletion builtin/symbolic-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static int check_symref(const char *HEAD, int quiet, int shorten, int print)
{
unsigned char sha1[20];
int flag;
const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
const char *refname = resolve_ref_unsafe(HEAD, 0, sha1, &flag);

if (!refname)
die("No such ref: %s", HEAD);
Expand Down
2 changes: 1 addition & 1 deletion bundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ int create_bundle(struct bundle_header *header, const char *path,
continue;
if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
continue;
if (read_ref_full(e->name, sha1, 1, &flag))
if (read_ref_full(e->name, RESOLVE_REF_READING, sha1, &flag))
flag = 0;
display_ref = (flag & REF_ISSYMREF) ? e->name : ref;

Expand Down
23 changes: 12 additions & 11 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,8 @@ extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
extern int get_sha1_hex(const char *hex, unsigned char *sha1);

extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
extern int read_ref_full(const char *refname, unsigned char *sha1,
int reading, int *flags);
extern int read_ref_full(const char *refname, int resolve_flags,
unsigned char *sha1, int *flags);
extern int read_ref(const char *refname, unsigned char *sha1);

/*
Expand All @@ -963,29 +963,30 @@ extern int read_ref(const char *refname, unsigned char *sha1);
* or the input ref.
*
* If the reference cannot be resolved to an object, the behavior
* depends on the "reading" argument:
* depends on the RESOLVE_REF_READING flag:
*
* - If reading is set, return NULL.
* - If RESOLVE_REF_READING is set, return NULL.
*
* - If reading is not set, clear sha1 and return the name of the last
* reference name in the chain, which will either be a non-symbolic
* - If RESOLVE_REF_READING is not set, clear sha1 and return the name of
* the last reference name in the chain, which will either be a non-symbolic
* reference or an undefined reference. If this is a prelude to
* "writing" to the ref, the return value is the name of the ref
* that will actually be created or changed.
*
* If flag is non-NULL, set the value that it points to the
* If flags is non-NULL, set the value that it points to the
* combination of REF_ISPACKED (if the reference was found among the
* packed references) and REF_ISSYMREF (if the initial reference was a
* symbolic reference).
* packed references), REF_ISSYMREF (if the initial reference was a
* symbolic reference) and REF_ISBROKEN (if the ref is malformed).
*
* If ref is not a properly-formatted, normalized reference, return
* NULL. If more than MAXDEPTH recursive symbolic lookups are needed,
* give up and return NULL.
*
* errno is set to something meaningful on error.
*/
extern const char *resolve_ref_unsafe(const char *ref, unsigned char *sha1, int reading, int *flag);
extern char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag);
#define RESOLVE_REF_READING 0x01
extern const char *resolve_ref_unsafe(const char *ref, int resolve_flags, unsigned char *sha1, int *flags);
extern char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags);

extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
Expand Down
4 changes: 3 additions & 1 deletion http-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,9 @@ static int show_head_ref(const char *refname, const unsigned char *sha1,

if (flag & REF_ISSYMREF) {
unsigned char unused[20];
const char *target = resolve_ref_unsafe(refname, unused, 1, NULL);
const char *target = resolve_ref_unsafe(refname,
RESOLVE_REF_READING,
unused, NULL);
const char *target_nons = strip_namespace(target);

strbuf_addf(buf, "ref: %s\n", target_nons);
Expand Down
2 changes: 1 addition & 1 deletion notes-merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ int notes_merge(struct notes_merge_options *o,
o->local_ref, o->remote_ref);

/* Dereference o->local_ref into local_sha1 */
if (read_ref_full(o->local_ref, local_sha1, 0, NULL))
if (read_ref_full(o->local_ref, 0, local_sha1, NULL))
die("Failed to resolve local notes ref '%s'", o->local_ref);
else if (!check_refname_format(o->local_ref, 0) &&
is_null_sha1(local_sha1))
Expand Down
5 changes: 3 additions & 2 deletions reflog-walk.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ static struct complete_reflogs *read_complete_reflog(const char *ref)
unsigned char sha1[20];
const char *name;
void *name_to_free;
name = name_to_free = resolve_refdup(ref, sha1, 1, NULL);
name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
sha1, NULL);
if (name) {
for_each_reflog_ent(name, read_one_reflog, reflogs);
free(name_to_free);
Expand Down Expand Up @@ -174,7 +175,7 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
if (*branch == '\0') {
unsigned char sha1[20];
free(branch);
branch = resolve_refdup("HEAD", sha1, 0, NULL);
branch = resolve_refdup("HEAD", 0, sha1, NULL);
if (!branch)
die ("No current branch");

Expand Down
Loading

0 comments on commit 7695d11

Please sign in to comment.