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 <sahlberg@google.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ronnie Sahlberg committed Jul 15, 2014 at 12:59 UTC 7695d118e5a3c9c6fcb4cb15eb766a1c57422aed
28 files changed +124 -92
branch.c
+1 -1
@@ -170,7 +170,7 @@ int validate_new_branchname(const char *name, struct strbuf *ref,
170 const char *head;
171 unsigned char sha1[20];
172
173 - head = resolve_ref_unsafe("HEAD", sha1, 0, NULL);
173 + head = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
174 if (!is_bare_repository() && head && !strcmp(head, ref->buf))
175 die(_("Cannot force update the current branch."));
176 }
builtin/blame.c
+1 -1
@@ -2286,7 +2286,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
2286 commit->date = now;
2287 parent_tail = &commit->parents;
2288
2289 - if (!resolve_ref_unsafe("HEAD", head_sha1, 1, NULL))
2289 + if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
2290 die("no such ref: HEAD");
2291
2292 parent_tail = append_parent(parent_tail, head_sha1);
builtin/branch.c
+5 -4
@@ -131,7 +131,8 @@ static int branch_merged(int kind, const char *name,
131 branch->merge[0] &&
132 branch->merge[0]->dst &&
133 (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)
136 reference_rev = lookup_commit_reference(sha1);
137 }
138 if (!reference_rev)
@@ -235,7 +236,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
236 free(name);
237
238 name = mkpathdup(fmt, bname.buf);
238 - target = resolve_ref_unsafe(name, sha1, 0, &flags);
239 + target = resolve_ref_unsafe(name, 0, sha1, &flags);
240 if (!target ||
241 (!(flags & REF_ISSYMREF) && is_null_sha1(sha1))) {
242 error(remote_branch
@@ -299,7 +300,7 @@ static char *resolve_symref(const char *src, const char *prefix)
300 int flag;
301 const char *dst;
302
302 - dst = resolve_ref_unsafe(src, sha1, 0, &flag);
303 + dst = resolve_ref_unsafe(src, 0, sha1, &flag);
304 if (!(dst && (flag & REF_ISSYMREF)))
305 return NULL;
306 if (prefix)
@@ -869,7 +870,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
870
871 track = git_branch_track;
872
872 - head = resolve_refdup("HEAD", head_sha1, 0, NULL);
873 + head = resolve_refdup("HEAD", 0, head_sha1, NULL);
874 if (!head)
875 die(_("Failed to resolve HEAD as a valid ref."));
876 if (!strcmp(head, "HEAD"))
builtin/checkout.c
+3 -3
@@ -355,7 +355,7 @@ static int checkout_paths(const struct checkout_opts *opts,
355 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
356 die(_("unable to write new index file"));
357
358 - read_ref_full("HEAD", rev, 0, &flag);
358 + read_ref_full("HEAD", 0, rev, &flag);
359 head = lookup_commit_reference_gently(rev, 1);
360
361 errs |= post_checkout_hook(head, head, 0);
@@ -775,7 +775,7 @@ static int switch_branches(const struct checkout_opts *opts,
775 unsigned char rev[20];
776 int flag, writeout_error = 0;
777 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);
779 old.commit = lookup_commit_reference_gently(rev, 1);
780 if (!(flag & REF_ISSYMREF))
781 old.path = NULL;
@@ -1072,7 +1072,7 @@ static int checkout_branch(struct checkout_opts *opts,
1072 unsigned char rev[20];
1073 int flag;
1074
1075 - if (!read_ref_full("HEAD", rev, 0, &flag) &&
1075 + if (!read_ref_full("HEAD", 0, rev, &flag) &&
1076 (flag & REF_ISSYMREF) && is_null_sha1(rev))
1077 return switch_unborn_to_new_branch(opts);
1078 }
builtin/clone.c
+1 -1
@@ -623,7 +623,7 @@ static int checkout(void)
623 if (option_no_checkout)
624 return 0;
625
626 - head = resolve_refdup("HEAD", sha1, 1, NULL);
626 + head = resolve_refdup("HEAD", RESOLVE_REF_READING, sha1, NULL);
627 if (!head) {
628 warning(_("remote HEAD refers to nonexistent ref, "
629 "unable to checkout.\n"));
builtin/commit.c
+1 -1
@@ -1515,7 +1515,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
1515 rev.diffopt.break_opt = 0;
1516 diff_setup_done(&rev.diffopt);
1517
1518 - head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
1518 + head = resolve_ref_unsafe("HEAD", 0, junk_sha1, NULL);
1519 if (!strcmp(head, "HEAD"))
1520 head = _("detached HEAD");
1521 else
builtin/fmt-merge-msg.c
+1 -1
@@ -602,7 +602,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
602
603 /* get current branch */
604 current_branch = current_branch_to_free =
605 - resolve_refdup("HEAD", head_sha1, 1, NULL);
605 + resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
606 if (!current_branch)
607 die("No current branch");
608 if (starts_with(current_branch, "refs/heads/"))
builtin/for-each-ref.c
+4 -2
@@ -635,7 +635,8 @@ static void populate_value(struct refinfo *ref)
635
636 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
637 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);
640 if (!ref->symref)
641 ref->symref = "";
642 }
@@ -693,7 +694,8 @@ static void populate_value(struct refinfo *ref)
694 const char *head;
695 unsigned char sha1[20];
696
696 - head = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
697 + head = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
698 + sha1, NULL);
699 if (!strcmp(ref->refname, head))
700 v->s = "*";
701 else
builtin/fsck.c
+1 -1
@@ -556,7 +556,7 @@ static int fsck_head_link(void)
556 if (verbose)
557 fprintf(stderr, "Checking HEAD link\n");
558
559 - head_points_at = resolve_ref_unsafe("HEAD", head_sha1, 0, &flag);
559 + head_points_at = resolve_ref_unsafe("HEAD", 0, head_sha1, &flag);
560 if (!head_points_at)
561 return error("Invalid HEAD");
562 if (!strcmp(head_points_at, "HEAD"))
builtin/log.c
+2 -1
@@ -1400,7 +1400,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1400 if (check_head) {
1401 unsigned char sha1[20];
1402 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);
1405 if (ref && skip_prefix(ref, "refs/heads/", &v))
1406 branch_name = xstrdup(v);
1407 else
builtin/merge.c
+1 -1
@@ -1101,7 +1101,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1101 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1102 * current branch.
1103 */
1104 - branch = branch_to_free = resolve_refdup("HEAD", head_sha1, 0, &flag);
1104 + branch = branch_to_free = resolve_refdup("HEAD", 0, head_sha1, &flag);
1105 if (branch && starts_with(branch, "refs/heads/"))
1106 branch += 11;
1107 if (!branch || is_null_sha1(head_sha1))
builtin/notes.c
+1 -1
@@ -702,7 +702,7 @@ static int merge_commit(struct notes_merge_options *o)
702 init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
703
704 o->local_ref = local_ref_to_free =
705 - resolve_refdup("NOTES_MERGE_REF", sha1, 0, NULL);
705 + resolve_refdup("NOTES_MERGE_REF", 0, sha1, NULL);
706 if (!o->local_ref)
707 die("Failed to resolve NOTES_MERGE_REF");
708
builtin/receive-pack.c
+2 -2
@@ -909,7 +909,7 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
909 int flag;
910
911 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
912 - dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
912 + dst_name = resolve_ref_unsafe(buf.buf, 0, sha1, &flag);
913 strbuf_release(&buf);
914
915 if (!(flag & REF_ISSYMREF))
@@ -1070,7 +1070,7 @@ static void execute_commands(struct command *commands,
1070 check_aliased_updates(commands);
1071
1072 free(head_name_to_free);
1073 - head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
1073 + head_name = head_name_to_free = resolve_refdup("HEAD", 0, sha1, NULL);
1074
1075 checked_connectivity = 1;
1076 for (cmd = commands; cmd; cmd = cmd->next) {
builtin/remote.c
+3 -2
@@ -567,7 +567,8 @@ static int read_remote_branches(const char *refname,
567 strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
568 if (starts_with(refname, buf.buf)) {
569 item = string_list_append(rename->remote_branches, xstrdup(refname));
570 - symref = resolve_ref_unsafe(refname, orig_sha1, 1, &flag);
570 + symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
571 + orig_sha1, &flag);
572 if (flag & REF_ISSYMREF)
573 item->util = xstrdup(symref);
574 else
@@ -703,7 +704,7 @@ static int mv(int argc, const char **argv)
704 int flag = 0;
705 unsigned char sha1[20];
706
706 - read_ref_full(item->string, sha1, 1, &flag);
707 + read_ref_full(item->string, RESOLVE_REF_READING, sha1, &flag);
708 if (!(flag & REF_ISSYMREF))
709 continue;
710 if (delete_ref(item->string, NULL, REF_NODEREF))
builtin/show-branch.c
+5 -2
@@ -728,7 +728,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
728 if (ac == 0) {
729 static const char *fake_av[2];
730
731 - fake_av[0] = resolve_refdup("HEAD", sha1, 1, NULL);
731 + fake_av[0] = resolve_refdup("HEAD",
732 + RESOLVE_REF_READING,
733 + sha1, NULL);
734 fake_av[1] = NULL;
735 av = fake_av;
736 ac = 1;
@@ -789,7 +791,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
791 }
792 }
793
792 - head_p = resolve_ref_unsafe("HEAD", head_sha1, 1, NULL);
794 + head_p = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
795 + head_sha1, NULL);
796 if (head_p) {
797 head_len = strlen(head_p);
798 memcpy(head, head_p, head_len + 1);
builtin/symbolic-ref.c
+1 -1
@@ -13,7 +13,7 @@ static int check_symref(const char *HEAD, int quiet, int shorten, int print)
13 {
14 unsigned char sha1[20];
15 int flag;
16 - const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
16 + const char *refname = resolve_ref_unsafe(HEAD, 0, sha1, &flag);
17
18 if (!refname)
19 die("No such ref: %s", HEAD);
bundle.c
+1 -1
@@ -310,7 +310,7 @@ int create_bundle(struct bundle_header *header, const char *path,
310 continue;
311 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
312 continue;
313 - if (read_ref_full(e->name, sha1, 1, &flag))
313 + if (read_ref_full(e->name, RESOLVE_REF_READING, sha1, &flag))
314 flag = 0;
315 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
316
cache.h
+12 -11
@@ -950,8 +950,8 @@ extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
950 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
951
952 extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
953 -extern int read_ref_full(const char *refname, unsigned char *sha1,
954 - int reading, int *flags);
953 +extern int read_ref_full(const char *refname, int resolve_flags,
954 + unsigned char *sha1, int *flags);
955 extern int read_ref(const char *refname, unsigned char *sha1);
956
957 /*
@@ -963,20 +963,20 @@ extern int read_ref(const char *refname, unsigned char *sha1);
963 * or the input ref.
964 *
965 * If the reference cannot be resolved to an object, the behavior
966 - * depends on the "reading" argument:
966 + * depends on the RESOLVE_REF_READING flag:
967 *
968 - * - If reading is set, return NULL.
968 + * - If RESOLVE_REF_READING is set, return NULL.
969 *
970 - * - If reading is not set, clear sha1 and return the name of the last
971 - * reference name in the chain, which will either be a non-symbolic
970 + * - If RESOLVE_REF_READING is not set, clear sha1 and return the name of
971 + * the last reference name in the chain, which will either be a non-symbolic
972 * reference or an undefined reference. If this is a prelude to
973 * "writing" to the ref, the return value is the name of the ref
974 * that will actually be created or changed.
975 *
976 - * If flag is non-NULL, set the value that it points to the
976 + * If flags is non-NULL, set the value that it points to the
977 * combination of REF_ISPACKED (if the reference was found among the
978 - * packed references) and REF_ISSYMREF (if the initial reference was a
979 - * symbolic reference).
978 + * packed references), REF_ISSYMREF (if the initial reference was a
979 + * symbolic reference) and REF_ISBROKEN (if the ref is malformed).
980 *
981 * If ref is not a properly-formatted, normalized reference, return
982 * NULL. If more than MAXDEPTH recursive symbolic lookups are needed,
@@ -984,8 +984,9 @@ extern int read_ref(const char *refname, unsigned char *sha1);
984 *
985 * errno is set to something meaningful on error.
986 */
987 -extern const char *resolve_ref_unsafe(const char *ref, unsigned char *sha1, int reading, int *flag);
988 -extern char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag);
987 +#define RESOLVE_REF_READING 0x01
988 +extern const char *resolve_ref_unsafe(const char *ref, int resolve_flags, unsigned char *sha1, int *flags);
989 +extern char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags);
990
991 extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
992 extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
http-backend.c
+3 -1
@@ -412,7 +412,9 @@ static int show_head_ref(const char *refname, const unsigned char *sha1,
412
413 if (flag & REF_ISSYMREF) {
414 unsigned char unused[20];
415 - const char *target = resolve_ref_unsafe(refname, unused, 1, NULL);
415 + const char *target = resolve_ref_unsafe(refname,
416 + RESOLVE_REF_READING,
417 + unused, NULL);
418 const char *target_nons = strip_namespace(target);
419
420 strbuf_addf(buf, "ref: %s\n", target_nons);
notes-merge.c
+1 -1
@@ -549,7 +549,7 @@ int notes_merge(struct notes_merge_options *o,
549 o->local_ref, o->remote_ref);
550
551 /* Dereference o->local_ref into local_sha1 */
552 - if (read_ref_full(o->local_ref, local_sha1, 0, NULL))
552 + if (read_ref_full(o->local_ref, 0, local_sha1, NULL))
553 die("Failed to resolve local notes ref '%s'", o->local_ref);
554 else if (!check_refname_format(o->local_ref, 0) &&
555 is_null_sha1(local_sha1))
reflog-walk.c
+3 -2
@@ -48,7 +48,8 @@ static struct complete_reflogs *read_complete_reflog(const char *ref)
48 unsigned char sha1[20];
49 const char *name;
50 void *name_to_free;
51 - name = name_to_free = resolve_refdup(ref, sha1, 1, NULL);
51 + name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING,
52 + sha1, NULL);
53 if (name) {
54 for_each_reflog_ent(name, read_one_reflog, reflogs);
55 free(name_to_free);
@@ -174,7 +175,7 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
175 if (*branch == '\0') {
176 unsigned char sha1[20];
177 free(branch);
177 - branch = resolve_refdup("HEAD", sha1, 0, NULL);
178 + branch = resolve_refdup("HEAD", 0, sha1, NULL);
179 if (!branch)
180 die ("No current branch");
181
refs.c
+53 -40
@@ -1251,7 +1251,9 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
1251 hashclr(sha1);
1252 flag |= REF_ISBROKEN;
1253 }
1254 - } else if (read_ref_full(refname.buf, sha1, 1, &flag)) {
1254 + } else if (read_ref_full(refname.buf,
1255 + RESOLVE_REF_READING,
1256 + sha1, &flag)) {
1257 hashclr(sha1);
1258 flag |= REF_ISBROKEN;
1259 }
@@ -1376,9 +1378,9 @@ static struct ref_entry *get_packed_ref(const char *refname)
1378 * options are forwarded from resolve_safe_unsafe().
1379 */
1380 static const char *handle_missing_loose_ref(const char *refname,
1381 + int resolve_flags,
1382 unsigned char *sha1,
1380 - int reading,
1381 - int *flag)
1383 + int *flags)
1384 {
1385 struct ref_entry *entry;
1386
@@ -1389,12 +1391,12 @@ static const char *handle_missing_loose_ref(const char *refname,
1391 entry = get_packed_ref(refname);
1392 if (entry) {
1393 hashcpy(sha1, entry->u.value.sha1);
1392 - if (flag)
1393 - *flag |= REF_ISPACKED;
1394 + if (flags)
1395 + *flags |= REF_ISPACKED;
1396 return refname;
1397 }
1398 /* The reference is not a packed reference, either. */
1397 - if (reading) {
1399 + if (resolve_flags & RESOLVE_REF_READING) {
1400 return NULL;
1401 } else {
1402 hashclr(sha1);
@@ -1403,21 +1405,20 @@ static const char *handle_missing_loose_ref(const char *refname,
1405 }
1406
1407 /* This function needs to return a meaningful errno on failure */
1406 -const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
1408 +const char *resolve_ref_unsafe(const char *refname, int resolve_flags, unsigned char *sha1, int *flags)
1409 {
1410 int depth = MAXDEPTH;
1411 ssize_t len;
1412 char buffer[256];
1413 static char refname_buffer[256];
1414
1413 - if (flag)
1414 - *flag = 0;
1415 + if (flags)
1416 + *flags = 0;
1417
1418 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1419 errno = EINVAL;
1420 return NULL;
1421 }
1420 -
1422 for (;;) {
1423 char path[PATH_MAX];
1424 struct stat st;
@@ -1443,8 +1444,8 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
1444 stat_ref:
1445 if (lstat(path, &st) < 0) {
1446 if (errno == ENOENT)
1446 - return handle_missing_loose_ref(refname, sha1,
1447 - reading, flag);
1447 + return handle_missing_loose_ref(refname,
1448 + resolve_flags, sha1, flags);
1449 else
1450 return NULL;
1451 }
@@ -1464,8 +1465,8 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
1465 !check_refname_format(buffer, 0)) {
1466 strcpy(refname_buffer, buffer);
1467 refname = refname_buffer;
1467 - if (flag)
1468 - *flag |= REF_ISSYMREF;
1468 + if (flags)
1469 + *flags |= REF_ISSYMREF;
1470 continue;
1471 }
1472 }
@@ -1510,21 +1511,21 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
1511 */
1512 if (get_sha1_hex(buffer, sha1) ||
1513 (buffer[40] != '\0' && !isspace(buffer[40]))) {
1513 - if (flag)
1514 - *flag |= REF_ISBROKEN;
1514 + if (flags)
1515 + *flags |= REF_ISBROKEN;
1516 errno = EINVAL;
1517 return NULL;
1518 }
1519 return refname;
1520 }
1520 - if (flag)
1521 - *flag |= REF_ISSYMREF;
1521 + if (flags)
1522 + *flags |= REF_ISSYMREF;
1523 buf = buffer + 4;
1524 while (isspace(*buf))
1525 buf++;
1526 if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) {
1526 - if (flag)
1527 - *flag |= REF_ISBROKEN;
1527 + if (flags)
1528 + *flags |= REF_ISBROKEN;
1529 errno = EINVAL;
1530 return NULL;
1531 }
@@ -1532,9 +1533,9 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
1533 }
1534 }
1535
1535 -char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag)
1536 +char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags)
1537 {
1537 - const char *ret = resolve_ref_unsafe(ref, sha1, reading, flag);
1538 + const char *ret = resolve_ref_unsafe(ref, resolve_flags, sha1, flags);
1539 return ret ? xstrdup(ret) : NULL;
1540 }
1541
@@ -1545,22 +1546,22 @@ struct ref_filter {
1546 void *cb_data;
1547 };
1548
1548 -int read_ref_full(const char *refname, unsigned char *sha1, int reading, int *flags)
1549 +int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags)
1550 {
1550 - if (resolve_ref_unsafe(refname, sha1, reading, flags))
1551 + if (resolve_ref_unsafe(refname, resolve_flags, sha1, flags))
1552 return 0;
1553 return -1;
1554 }
1555
1556 int read_ref(const char *refname, unsigned char *sha1)
1557 {
1557 - return read_ref_full(refname, sha1, 1, NULL);
1558 + return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL);
1559 }
1560
1561 int ref_exists(const char *refname)
1562 {
1563 unsigned char sha1[20];
1563 - return !!resolve_ref_unsafe(refname, sha1, 1, NULL);
1564 + return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
1565 }
1566
1567 static int filter_refs(const char *refname, const unsigned char *sha1, int flags,
@@ -1673,7 +1674,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
1674 return 0;
1675 }
1676
1676 - if (read_ref_full(refname, base, 1, &flag))
1677 + if (read_ref_full(refname, RESOLVE_REF_READING, base, &flag))
1678 return -1;
1679
1680 /*
@@ -1714,7 +1715,7 @@ static int warn_if_dangling_symref(const char *refname, const unsigned char *sha
1715 if (!(flags & REF_ISSYMREF))
1716 return 0;
1717
1717 - resolves_to = resolve_ref_unsafe(refname, junk, 0, NULL);
1718 + resolves_to = resolve_ref_unsafe(refname, 0, junk, NULL);
1719 if (!resolves_to
1720 || (d->refname
1721 ? strcmp(resolves_to, d->refname)
@@ -1839,7 +1840,7 @@ static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1840 return 0;
1841 }
1842
1842 - if (!read_ref_full("HEAD", sha1, 1, &flag))
1843 + if (!read_ref_full("HEAD", RESOLVE_REF_READING, sha1, &flag))
1844 return fn("HEAD", sha1, flag, cb_data);
1845
1846 return 0;
@@ -1919,7 +1920,7 @@ int head_ref_namespaced(each_ref_fn fn, void *cb_data)
1920 int flag;
1921
1922 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
1922 - if (!read_ref_full(buf.buf, sha1, 1, &flag))
1923 + if (!read_ref_full(buf.buf, RESOLVE_REF_READING, sha1, &flag))
1924 ret = fn(buf.buf, sha1, flag, cb_data);
1925 strbuf_release(&buf);
1926
@@ -2014,7 +2015,9 @@ int refname_match(const char *abbrev_name, const char *full_name)
2015 static struct ref_lock *verify_lock(struct ref_lock *lock,
2016 const unsigned char *old_sha1, int mustexist)
2017 {
2017 - if (read_ref_full(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
2018 + if (read_ref_full(lock->ref_name,
2019 + mustexist ? RESOLVE_REF_READING : 0,
2020 + lock->old_sha1, NULL)) {
2021 int save_errno = errno;
2022 error("Can't verify ref %s", lock->ref_name);
2023 unlock_ref(lock);
@@ -2087,7 +2090,8 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
2090
2091 this_result = refs_found ? sha1_from_ref : sha1;
2092 mksnpath(fullref, sizeof(fullref), *p, len, str);
2090 - r = resolve_ref_unsafe(fullref, this_result, 1, &flag);
2093 + r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING,
2094 + this_result, &flag);
2095 if (r) {
2096 if (!refs_found++)
2097 *ref = xstrdup(r);
@@ -2116,7 +2120,8 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
2120 const char *ref, *it;
2121
2122 mksnpath(path, sizeof(path), *p, len, str);
2119 - ref = resolve_ref_unsafe(path, hash, 1, NULL);
2123 + ref = resolve_ref_unsafe(path, RESOLVE_REF_READING,
2124 + hash, NULL);
2125 if (!ref)
2126 continue;
2127 if (reflog_exists(path))
@@ -2151,6 +2156,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2156 int last_errno = 0;
2157 int type, lflags;
2158 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
2159 + int resolve_flags = 0;
2160 int missing = 0;
2161 int attempts_remaining = 3;
2162
@@ -2162,7 +2168,11 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2168 lock = xcalloc(1, sizeof(struct ref_lock));
2169 lock->lock_fd = -1;
2170
2165 - refname = resolve_ref_unsafe(refname, lock->old_sha1, mustexist, &type);
2171 + if (mustexist)
2172 + resolve_flags |= RESOLVE_REF_READING;
2173 +
2174 + refname = resolve_ref_unsafe(refname, resolve_flags,
2175 + lock->old_sha1, &type);
2176 if (!refname && errno == EISDIR) {
2177 /* we are trying to lock foo but we used to
2178 * have foo/bar which now does not exist;
@@ -2175,7 +2185,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2185 error("there are still refs under '%s'", orig_refname);
2186 goto error_return;
2187 }
2178 - refname = resolve_ref_unsafe(orig_refname, lock->old_sha1, mustexist, &type);
2188 + refname = resolve_ref_unsafe(orig_refname, resolve_flags,
2189 + lock->old_sha1, &type);
2190 }
2191 if (type_p)
2192 *type_p = type;
@@ -2517,7 +2528,7 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
2528 unsigned char sha1[20];
2529 int flags;
2530
2520 - if (read_ref_full(entry->name, sha1, 0, &flags))
2531 + if (read_ref_full(entry->name, 0, sha1, &flags))
2532 /* We should at least have found the packed ref. */
2533 die("Internal error");
2534 if ((flags & REF_ISSYMREF) || !(flags & REF_ISPACKED)) {
@@ -2721,7 +2732,8 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2732 if (log && S_ISLNK(loginfo.st_mode))
2733 return error("reflog for %s is a symlink", oldrefname);
2734
2724 - symref = resolve_ref_unsafe(oldrefname, orig_sha1, 1, &flag);
2735 + symref = resolve_ref_unsafe(oldrefname, RESOLVE_REF_READING,
2736 + orig_sha1, &flag);
2737 if (flag & REF_ISSYMREF)
2738 return error("refname %s is a symbolic ref, renaming it is not supported",
2739 oldrefname);
@@ -2740,7 +2752,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2752 goto rollback;
2753 }
2754
2743 - if (!read_ref_full(newrefname, sha1, 1, NULL) &&
2755 + if (!read_ref_full(newrefname, RESOLVE_REF_READING, sha1, NULL) &&
2756 delete_ref(newrefname, sha1, REF_NODEREF)) {
2757 if (errno==EISDIR) {
2758 if (remove_empty_directories(git_path("%s", newrefname))) {
@@ -3018,7 +3030,8 @@ static int write_ref_sha1(struct ref_lock *lock,
3030 unsigned char head_sha1[20];
3031 int head_flag;
3032 const char *head_ref;
3021 - head_ref = resolve_ref_unsafe("HEAD", head_sha1, 1, &head_flag);
3033 + head_ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
3034 + head_sha1, &head_flag);
3035 if (head_ref && (head_flag & REF_ISSYMREF) &&
3036 !strcmp(head_ref, lock->ref_name))
3037 log_ref_write("HEAD", lock->old_sha1, sha1, logmsg);
@@ -3389,7 +3402,7 @@ static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data
3402 retval = do_for_each_reflog(name, fn, cb_data);
3403 } else {
3404 unsigned char sha1[20];
3392 - if (read_ref_full(name->buf, sha1, 0, NULL))
3405 + if (read_ref_full(name->buf, 0, sha1, NULL))
3406 retval = error("bad ref for %s", name->buf);
3407 else
3408 retval = fn(name->buf, sha1, 0, cb_data);
remote.c
+7 -4
@@ -508,7 +508,7 @@ static void read_config(void)
508 return;
509 default_remote_name = "origin";
510 current_branch = NULL;
511 - head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag);
511 + head_ref = resolve_ref_unsafe("HEAD", 0, sha1, &flag);
512 if (head_ref && (flag & REF_ISSYMREF) &&
513 skip_prefix(head_ref, "refs/heads/", &head_ref)) {
514 current_branch = make_branch(head_ref, 0);
@@ -1138,7 +1138,8 @@ static char *guess_ref(const char *name, struct ref *peer)
1138 struct strbuf buf = STRBUF_INIT;
1139 unsigned char sha1[20];
1140
1141 - const char *r = resolve_ref_unsafe(peer->name, sha1, 1, NULL);
1141 + const char *r = resolve_ref_unsafe(peer->name, RESOLVE_REF_READING,
1142 + sha1, NULL);
1143 if (!r)
1144 return NULL;
1145
@@ -1199,7 +1200,9 @@ static int match_explicit(struct ref *src, struct ref *dst,
1200 unsigned char sha1[20];
1201 int flag;
1202
1202 - dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);
1203 + dst_value = resolve_ref_unsafe(matched_src->name,
1204 + RESOLVE_REF_READING,
1205 + sha1, &flag);
1206 if (!dst_value ||
1207 ((flag & REF_ISSYMREF) &&
1208 !starts_with(dst_value, "refs/heads/")))
@@ -1673,7 +1676,7 @@ static int ignore_symref_update(const char *refname)
1676 unsigned char sha1[20];
1677 int flag;
1678
1676 - if (!resolve_ref_unsafe(refname, sha1, 0, &flag))
1679 + if (!resolve_ref_unsafe(refname, 0, sha1, &flag))
1680 return 0; /* non-existing refs are OK */
1681 return (flag & REF_ISSYMREF);
1682 }
sequencer.c
+2 -2
@@ -331,7 +331,7 @@ static int is_index_unchanged(void)
331 unsigned char head_sha1[20];
332 struct commit *head_commit;
333
334 - if (!resolve_ref_unsafe("HEAD", head_sha1, 1, NULL))
334 + if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
335 return error(_("Could not resolve HEAD commit\n"));
336
337 head_commit = lookup_commit(head_sha1);
@@ -871,7 +871,7 @@ static int rollback_single_pick(void)
871 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
872 !file_exists(git_path("REVERT_HEAD")))
873 return error(_("no cherry-pick or revert in progress"));
874 - if (read_ref_full("HEAD", head_sha1, 0, NULL))
874 + if (read_ref_full("HEAD", 0, head_sha1, NULL))
875 return error(_("cannot resolve HEAD"));
876 if (is_null_sha1(head_sha1))
877 return error(_("cannot abort from a branch yet to be born"));
transport-helper.c
+4 -1
@@ -897,7 +897,10 @@ static int push_refs_with_export(struct transport *transport,
897 int flag;
898
899 /* Follow symbolic refs (mainly for HEAD). */
900 - name = resolve_ref_unsafe(ref->peer_ref->name, sha1, 1, &flag);
900 + name = resolve_ref_unsafe(
901 + ref->peer_ref->name,
902 + RESOLVE_REF_READING,
903 + sha1, &flag);
904 if (!name || !(flag & REF_ISSYMREF))
905 name = ref->peer_ref->name;
906
transport.c
+3 -2
@@ -168,7 +168,8 @@ static void set_upstreams(struct transport *transport, struct ref *refs,
168 /* Follow symbolic refs (mainly for HEAD). */
169 localname = ref->peer_ref->name;
170 remotename = ref->name;
171 - tmp = resolve_ref_unsafe(localname, sha, 1, &flag);
171 + tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
172 + sha, &flag);
173 if (tmp && flag & REF_ISSYMREF &&
174 starts_with(tmp, "refs/heads/"))
175 localname = tmp;
@@ -743,7 +744,7 @@ void transport_print_push_status(const char *dest, struct ref *refs,
744 unsigned char head_sha1[20];
745 char *head;
746
746 - head = resolve_refdup("HEAD", head_sha1, 1, NULL);
747 + head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
748
749 if (verbose) {
750 for (ref = refs; ref; ref = ref->next)
upload-pack.c
+1 -1
@@ -744,7 +744,7 @@ static int find_symref(const char *refname, const unsigned char *sha1, int flag,
744
745 if ((flag & REF_ISSYMREF) == 0)
746 return 0;
747 - symref_target = resolve_ref_unsafe(refname, unused, 0, &flag);
747 + symref_target = resolve_ref_unsafe(refname, 0, unused, &flag);
748 if (!symref_target || (flag & REF_ISSYMREF) == 0)
749 die("'%s' is a symref but it is not?", refname);
750 item = string_list_append(cb_data, refname);
wt-status.c
+1 -1
@@ -128,7 +128,7 @@ void wt_status_prepare(struct wt_status *s)
128 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
129 s->use_color = -1;
130 s->relative_paths = 1;
131 - s->branch = resolve_refdup("HEAD", sha1, 0, NULL);
131 + s->branch = resolve_refdup("HEAD", 0, sha1, NULL);
132 s->reference = "HEAD";
133 s->fp = stdout;
134 s->index_file = get_index_file();