Merge branch 'ab/refs-errno-cleanup'

The "remainder" of hn/refs-errno-cleanup topic.

* ab/refs-errno-cleanup: (21 commits)
  refs API: post-migration API renaming [2/2]
  refs API: post-migration API renaming [1/2]
  refs API: don't expose "errno" in run_transaction_hook()
  refs API: make expand_ref() & repo_dwim_log() not set errno
  refs API: make resolve_ref_unsafe() not set errno
  refs API: make refs_ref_exists() not set errno
  refs API: make refs_resolve_refdup() not set errno
  refs tests: ignore ignore errno in test-ref-store helper
  refs API: ignore errno in worktree.c's find_shared_symref()
  refs API: ignore errno in worktree.c's add_head_info()
  refs API: make files_copy_or_rename_ref() et al not set errno
  refs API: make loose_fill_ref_dir() not set errno
  refs API: make resolve_gitlink_ref() not set errno
  refs API: remove refs_read_ref_full() wrapper
  refs/files: remove "name exist?" check in lock_ref_oid_basic()
  reflog tests: add --updateref tests
  refs API: make refs_rename_ref_available() static
  refs API: make parse_loose_ref_contents() not set errno
  refs API: make refs_read_raw_ref() not set errno
  refs API: add a version of refs_resolve_ref_unsafe() with "errno"
  ...
This commit is contained in:
Junio C Hamano
2021-11-29 15:41:45 -08:00
10 changed files with 295 additions and 144 deletions

126
refs.c
View File

@ -269,9 +269,10 @@ char *refs_resolve_refdup(struct ref_store *refs,
struct object_id *oid, int *flags)
{
const char *result;
int ignore_errno;
result = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
oid, flags);
oid, flags, &ignore_errno);
return xstrdup_or_null(result);
}
@ -291,18 +292,15 @@ struct ref_filter {
void *cb_data;
};
int refs_read_ref_full(struct ref_store *refs, const char *refname,
int resolve_flags, struct object_id *oid, int *flags)
{
if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, oid, flags))
return 0;
return -1;
}
int read_ref_full(const char *refname, int resolve_flags, struct object_id *oid, int *flags)
{
return refs_read_ref_full(get_main_ref_store(the_repository), refname,
resolve_flags, oid, flags);
int ignore_errno;
struct ref_store *refs = get_main_ref_store(the_repository);
if (refs_resolve_ref_unsafe(refs, refname, resolve_flags,
oid, flags, &ignore_errno))
return 0;
return -1;
}
int read_ref(const char *refname, struct object_id *oid)
@ -312,7 +310,9 @@ int read_ref(const char *refname, struct object_id *oid)
int refs_ref_exists(struct ref_store *refs, const char *refname)
{
return !!refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING, NULL, NULL);
int ignore_errno;
return !!refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING,
NULL, NULL, &ignore_errno);
}
int ref_exists(const char *refname)
@ -655,13 +655,16 @@ int expand_ref(struct repository *repo, const char *str, int len,
struct object_id oid_from_ref;
struct object_id *this_result;
int flag;
struct ref_store *refs = get_main_ref_store(repo);
int ignore_errno;
this_result = refs_found ? &oid_from_ref : oid;
strbuf_reset(&fullref);
strbuf_addf(&fullref, *p, len, str);
r = refs_resolve_ref_unsafe(get_main_ref_store(repo),
fullref.buf, RESOLVE_REF_READING,
this_result, &flag);
r = refs_resolve_ref_unsafe(refs, fullref.buf,
RESOLVE_REF_READING,
this_result, &flag,
&ignore_errno);
if (r) {
if (!refs_found++)
*ref = xstrdup(r);
@ -690,12 +693,14 @@ int repo_dwim_log(struct repository *r, const char *str, int len,
for (p = ref_rev_parse_rules; *p; p++) {
struct object_id hash;
const char *ref, *it;
int ignore_errno;
strbuf_reset(&path);
strbuf_addf(&path, *p, len, str);
ref = refs_resolve_ref_unsafe(refs, path.buf,
RESOLVE_REF_READING,
oid ? &hash : NULL, NULL);
oid ? &hash : NULL, NULL,
&ignore_errno);
if (!ref)
continue;
if (refs_reflog_exists(refs, path.buf))
@ -1373,32 +1378,14 @@ const char *find_descendant_ref(const char *dirname,
return NULL;
}
int refs_rename_ref_available(struct ref_store *refs,
const char *old_refname,
const char *new_refname)
{
struct string_list skip = STRING_LIST_INIT_NODUP;
struct strbuf err = STRBUF_INIT;
int ok;
string_list_insert(&skip, old_refname);
ok = !refs_verify_refname_available(refs, new_refname,
NULL, &skip, &err);
if (!ok)
error("%s", err.buf);
string_list_clear(&skip, 0);
strbuf_release(&err);
return ok;
}
int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
{
struct object_id oid;
int flag;
int ignore_errno;
if (!refs_read_ref_full(refs, "HEAD", RESOLVE_REF_READING,
&oid, &flag))
if (refs_resolve_ref_unsafe(refs, "HEAD", RESOLVE_REF_READING,
&oid, &flag, &ignore_errno))
return fn("HEAD", &oid, flag, cb_data);
return 0;
@ -1649,7 +1636,8 @@ int for_each_fullref_in_prefixes(const char *namespace,
static int refs_read_special_head(struct ref_store *ref_store,
const char *refname, struct object_id *oid,
struct strbuf *referent, unsigned int *type)
struct strbuf *referent, unsigned int *type,
int *failure_errno)
{
struct strbuf full_path = STRBUF_INIT;
struct strbuf content = STRBUF_INIT;
@ -1659,7 +1647,8 @@ static int refs_read_special_head(struct ref_store *ref_store,
if (strbuf_read_file(&content, full_path.buf, 0) < 0)
goto done;
result = parse_loose_ref_contents(content.buf, oid, referent, type);
result = parse_loose_ref_contents(content.buf, oid, referent, type,
failure_errno);
done:
strbuf_release(&full_path);
@ -1667,30 +1656,33 @@ done:
return result;
}
int refs_read_raw_ref(struct ref_store *ref_store,
const char *refname, struct object_id *oid,
struct strbuf *referent, unsigned int *type)
int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
struct object_id *oid, struct strbuf *referent,
unsigned int *type, int *failure_errno)
{
assert(failure_errno);
if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) {
return refs_read_special_head(ref_store, refname, oid, referent,
type);
type, failure_errno);
}
return ref_store->be->read_raw_ref(ref_store, refname, oid, referent,
type, &errno);
type, failure_errno);
}
/* This function needs to return a meaningful errno on failure */
const char *refs_resolve_ref_unsafe(struct ref_store *refs,
const char *refname,
int resolve_flags,
struct object_id *oid, int *flags)
struct object_id *oid,
int *flags, int *failure_errno)
{
static struct strbuf sb_refname = STRBUF_INIT;
struct object_id unused_oid;
int unused_flags;
int symref_count;
assert(failure_errno);
if (!oid)
oid = &unused_oid;
if (!flags)
@ -1701,7 +1693,7 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
!refname_is_safe(refname)) {
errno = EINVAL;
*failure_errno = EINVAL;
return NULL;
}
@ -1719,9 +1711,11 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
unsigned int read_flags = 0;
if (refs_read_raw_ref(refs, refname,
oid, &sb_refname, &read_flags)) {
if (refs_read_raw_ref(refs, refname, oid, &sb_refname,
&read_flags, failure_errno)) {
*flags |= read_flags;
if (errno)
*failure_errno = errno;
/* In reading mode, refs must eventually resolve */
if (resolve_flags & RESOLVE_REF_READING)
@ -1732,9 +1726,9 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
* may show errors besides ENOENT if there are
* similarly-named refs.
*/
if (errno != ENOENT &&
errno != EISDIR &&
errno != ENOTDIR)
if (*failure_errno != ENOENT &&
*failure_errno != EISDIR &&
*failure_errno != ENOTDIR)
return NULL;
oidclr(oid);
@ -1761,7 +1755,7 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
!refname_is_safe(refname)) {
errno = EINVAL;
*failure_errno = EINVAL;
return NULL;
}
@ -1769,7 +1763,7 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
}
}
errno = ELOOP;
*failure_errno = ELOOP;
return NULL;
}
@ -1784,8 +1778,10 @@ int refs_init_db(struct strbuf *err)
const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
struct object_id *oid, int *flags)
{
int ignore_errno;
return refs_resolve_ref_unsafe(get_main_ref_store(the_repository), refname,
resolve_flags, oid, flags);
resolve_flags, oid, flags, &ignore_errno);
}
int resolve_gitlink_ref(const char *submodule, const char *refname,
@ -1793,14 +1789,15 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
{
struct ref_store *refs;
int flags;
int ignore_errno;
refs = get_submodule_ref_store(submodule);
if (!refs)
return -1;
if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags) ||
is_null_oid(oid))
if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags,
&ignore_errno) || is_null_oid(oid))
return -1;
return 0;
}
@ -2102,8 +2099,11 @@ static int run_transaction_hook(struct ref_transaction *transaction,
update->refname);
if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
if (errno != EPIPE)
if (errno != EPIPE) {
/* Don't leak errno outside this API */
errno = 0;
ret = -1;
}
break;
}
}
@ -2238,6 +2238,13 @@ int refs_verify_refname_available(struct ref_store *refs,
strbuf_grow(&dirname, strlen(refname) + 1);
for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
/*
* Just saying "Is a directory" when we e.g. can't
* lock some multi-level ref isn't very informative,
* the user won't be told *what* is a directory, so
* let's not use strerror() below.
*/
int ignore_errno;
/* Expand dirname to the new prefix, not including the trailing slash: */
strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
@ -2249,7 +2256,8 @@ int refs_verify_refname_available(struct ref_store *refs,
if (skip && string_list_has_string(skip, dirname.buf))
continue;
if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent, &type)) {
if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
&type, &ignore_errno)) {
strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
dirname.buf, refname);
goto cleanup;