refs API: make resolve_ref_unsafe() not set errno

Change the resolve_ref_unsafe() wrapper function to use the underlying refs_werrres_ref_unsafe() directly. From a reading of the callers I determined that the only one who cared about errno was a sequencer.c caller added in e47c6cafcb5 (commit: move print_commit_summary() to libgit, 2017-11-24), I'm migrating it to using refs_werrres_ref_unsafe() directly. This adds another "set errno" instance, but in this case it's OK and idiomatic. We are setting it just before calling die_errno(). We could have some hypothetical die_errno_var(&saved_errno, ...) here, but I don't think it's worth it. The problem with errno is subtle action at distance, not this sort of thing. We already use this pattern in a couple of places in wrapper.c Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 16, 2021 at 11:39 UTC ed90f04155df1ec757b241ae3f45889c05efae26
2 files changed +12 -4
refs.c
+4 -2
@@ -1785,8 +1785,10 @@ int refs_init_db(struct strbuf *err)
1785 const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1786 struct object_id *oid, int *flags)
1787 {
1788 - return refs_resolve_ref_unsafe(get_main_ref_store(the_repository), refname,
1789 - resolve_flags, oid, flags);
1788 + int ignore_errno;
1789 +
1790 + return refs_werrres_ref_unsafe(get_main_ref_store(the_repository), refname,
1791 + resolve_flags, oid, flags, &ignore_errno);
1792 }
1793
1794 int resolve_gitlink_ref(const char *submodule, const char *refname,
sequencer.c
+8 -2
@@ -1282,6 +1282,8 @@ void print_commit_summary(struct repository *r,
1282 struct pretty_print_context pctx = {0};
1283 struct strbuf author_ident = STRBUF_INIT;
1284 struct strbuf committer_ident = STRBUF_INIT;
1285 + struct ref_store *refs;
1286 + int resolve_errno;
1287
1288 commit = lookup_commit(r, oid);
1289 if (!commit)
@@ -1331,9 +1333,13 @@ void print_commit_summary(struct repository *r,
1333 rev.diffopt.break_opt = 0;
1334 diff_setup_done(&rev.diffopt);
1335
1334 - head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1335 - if (!head)
1336 + refs = get_main_ref_store(the_repository);
1337 + head = refs_werrres_ref_unsafe(refs, "HEAD", 0, NULL, NULL,
1338 + &resolve_errno);
1339 + if (!head) {
1340 + errno = resolve_errno;
1341 die_errno(_("unable to resolve HEAD after creating commit"));
1342 + }
1343 if (!strcmp(head, "HEAD"))
1344 head = _("detached HEAD");
1345 else