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
Родитель dea96aae4d f1da24ca5e
Коммит 96f6623ada
10 изменённых файлов: 295 добавлений и 144 удалений

126
refs.c
Просмотреть файл

@ -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;

10
refs.h
Просмотреть файл

@ -58,6 +58,11 @@ struct worktree;
* resolved. The function returns NULL for such ref names.
* Caps and underscores refers to the special refs, such as HEAD,
* FETCH_HEAD and friends, that all live outside of the refs/ directory.
*
* Callers should not inspect "errno" on failure, but rather pass in a
* "failure_errno" parameter, on failure the "errno" will indicate the
* type of failure encountered, but not necessarily one that came from
* a syscall. We might have faked it up.
*/
#define RESOLVE_REF_READING 0x01
#define RESOLVE_REF_NO_RECURSE 0x02
@ -67,7 +72,8 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
const char *refname,
int resolve_flags,
struct object_id *oid,
int *flags);
int *flags, int *failure_errno);
const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
struct object_id *oid, int *flags);
@ -77,8 +83,6 @@ char *refs_resolve_refdup(struct ref_store *refs,
char *resolve_refdup(const char *refname, int resolve_flags,
struct object_id *oid, int *flags);
int refs_read_ref_full(struct ref_store *refs, const char *refname,
int resolve_flags, struct object_id *oid, int *flags);
int read_ref_full(const char *refname, int resolve_flags,
struct object_id *oid, int *flags);
int read_ref(const char *refname, struct object_id *oid);

Просмотреть файл

@ -282,10 +282,11 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
create_dir_entry(dir->cache, refname.buf,
refname.len));
} else {
int ignore_errno;
if (!refs_resolve_ref_unsafe(&refs->base,
refname.buf,
RESOLVE_REF_READING,
&oid, &flag)) {
&oid, &flag, &ignore_errno)) {
oidclr(&oid);
flag |= REF_ISBROKEN;
} else if (is_null_oid(&oid)) {
@ -357,6 +358,7 @@ static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
int fd;
int ret = -1;
int remaining_retries = 3;
int myerr = 0;
*type = 0;
strbuf_reset(&sb_path);
@ -383,11 +385,14 @@ stat_ref:
goto out;
if (lstat(path, &st) < 0) {
if (errno != ENOENT)
int ignore_errno;
myerr = errno;
errno = 0;
if (myerr != ENOENT)
goto out;
if (refs_read_raw_ref(refs->packed_ref_store, refname,
oid, referent, type)) {
errno = ENOENT;
if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
referent, type, &ignore_errno)) {
myerr = ENOENT;
goto out;
}
ret = 0;
@ -398,7 +403,9 @@ stat_ref:
if (S_ISLNK(st.st_mode)) {
strbuf_reset(&sb_contents);
if (strbuf_readlink(&sb_contents, path, st.st_size) < 0) {
if (errno == ENOENT || errno == EINVAL)
myerr = errno;
errno = 0;
if (myerr == ENOENT || myerr == EINVAL)
/* inconsistent with lstat; retry */
goto stat_ref;
else
@ -420,14 +427,15 @@ stat_ref:
/* Is it a directory? */
if (S_ISDIR(st.st_mode)) {
int ignore_errno;
/*
* Even though there is a directory where the loose
* ref is supposed to be, there could still be a
* packed ref:
*/
if (refs_read_raw_ref(refs->packed_ref_store, refname,
oid, referent, type)) {
errno = EISDIR;
if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
referent, type, &ignore_errno)) {
myerr = EISDIR;
goto out;
}
ret = 0;
@ -440,7 +448,8 @@ stat_ref:
*/
fd = open(path, O_RDONLY);
if (fd < 0) {
if (errno == ENOENT && !S_ISLNK(st.st_mode))
myerr = errno;
if (myerr == ENOENT && !S_ISLNK(st.st_mode))
/* inconsistent with lstat; retry */
goto stat_ref;
else
@ -448,26 +457,29 @@ stat_ref:
}
strbuf_reset(&sb_contents);
if (strbuf_read(&sb_contents, fd, 256) < 0) {
int save_errno = errno;
myerr = errno;
close(fd);
errno = save_errno;
goto out;
}
close(fd);
strbuf_rtrim(&sb_contents);
buf = sb_contents.buf;
ret = parse_loose_ref_contents(buf, oid, referent, type);
ret = parse_loose_ref_contents(buf, oid, referent, type, &myerr);
out:
*failure_errno = errno;
if (ret && !myerr)
BUG("returning non-zero %d, should have set myerr!", ret);
*failure_errno = myerr;
strbuf_release(&sb_path);
strbuf_release(&sb_contents);
return ret;
}
int parse_loose_ref_contents(const char *buf, struct object_id *oid,
struct strbuf *referent, unsigned int *type)
struct strbuf *referent, unsigned int *type,
int *failure_errno)
{
const char *p;
if (skip_prefix(buf, "ref:", &buf)) {
@ -486,7 +498,7 @@ int parse_loose_ref_contents(const char *buf, struct object_id *oid,
if (parse_oid_hex(buf, oid, &p) ||
(*p != '\0' && !isspace(*p))) {
*type |= REF_ISBROKEN;
errno = EINVAL;
*failure_errno = EINVAL;
return -1;
}
return 0;
@ -995,11 +1007,12 @@ static int create_reflock(const char *path, void *cb)
* Locks a ref returning the lock on success and NULL on failure.
*/
static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
const char *refname, int *type,
const char *refname,
struct strbuf *err)
{
struct strbuf ref_file = STRBUF_INIT;
struct ref_lock *lock;
int ignore_errno;
files_assert_main_repository(refs, "lock_ref_oid_basic");
assert(err);
@ -1007,16 +1020,6 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
CALLOC_ARRAY(lock, 1);
files_ref_path(refs, &ref_file, refname);
if (!refs_resolve_ref_unsafe(&refs->base, refname,
RESOLVE_REF_NO_RECURSE,
&lock->old_oid, type)) {
if (!refs_verify_refname_available(&refs->base, refname,
NULL, NULL, err))
strbuf_addf(err, "unable to resolve reference '%s': %s",
refname, strerror(errno));
goto error_return;
}
/*
* If the ref did not exist and we are creating it, make sure
@ -1036,9 +1039,8 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
goto error_return;
}
if (refs_read_ref_full(&refs->base, lock->ref_name,
0,
&lock->old_oid, NULL))
if (!refs_resolve_ref_unsafe(&refs->base, lock->ref_name, 0,
&lock->old_oid, NULL, &ignore_errno))
oidclr(&lock->old_oid);
goto out;
@ -1358,6 +1360,35 @@ static int commit_ref_update(struct files_ref_store *refs,
const struct object_id *oid, const char *logmsg,
struct strbuf *err);
/*
* Emit a better error message than lockfile.c's
* unable_to_lock_message() would in case there is a D/F conflict with
* another existing reference. If there would be a conflict, emit an error
* message and return false; otherwise, return true.
*
* Note that this function is not safe against all races with other
* processes, and that's not its job. We'll emit a more verbose error on D/f
* conflicts if we get past it into lock_ref_oid_basic().
*/
static 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;
}
static int files_copy_or_rename_ref(struct ref_store *ref_store,
const char *oldrefname, const char *newrefname,
const char *logmsg, int copy)
@ -1373,6 +1404,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
struct strbuf tmp_renamed_log = STRBUF_INIT;
int log, ret;
struct strbuf err = STRBUF_INIT;
int ignore_errno;
files_reflog_path(refs, &sb_oldref, oldrefname);
files_reflog_path(refs, &sb_newref, newrefname);
@ -1386,7 +1418,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
&orig_oid, &flag)) {
&orig_oid, &flag, &ignore_errno)) {
ret = error("refname %s not found", oldrefname);
goto out;
}
@ -1430,9 +1462,9 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
* the safety anyway; we want to delete the reference whatever
* its current value.
*/
if (!copy && !refs_read_ref_full(&refs->base, newrefname,
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
NULL, NULL) &&
if (!copy && refs_resolve_ref_unsafe(&refs->base, newrefname,
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
NULL, NULL, &ignore_errno) &&
refs_delete_ref(&refs->base, NULL, newrefname,
NULL, REF_NO_DEREF)) {
if (errno == EISDIR) {
@ -1458,7 +1490,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
logmoved = log;
lock = lock_ref_oid_basic(refs, newrefname, NULL, &err);
lock = lock_ref_oid_basic(refs, newrefname, &err);
if (!lock) {
if (copy)
error("unable to copy '%s' to '%s': %s", oldrefname, newrefname, err.buf);
@ -1480,7 +1512,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
goto out;
rollback:
lock = lock_ref_oid_basic(refs, oldrefname, NULL, &err);
lock = lock_ref_oid_basic(refs, oldrefname, &err);
if (!lock) {
error("unable to lock %s for rollback: %s", oldrefname, err.buf);
strbuf_release(&err);
@ -1797,10 +1829,12 @@ static int commit_ref_update(struct files_ref_store *refs,
*/
int head_flag;
const char *head_ref;
int ignore_errno;
head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
RESOLVE_REF_READING,
NULL, &head_flag);
NULL, &head_flag,
&ignore_errno);
if (head_ref && (head_flag & REF_ISSYMREF) &&
!strcmp(head_ref, lock->ref_name)) {
struct strbuf log_err = STRBUF_INIT;
@ -1844,9 +1878,12 @@ static void update_symref_reflog(struct files_ref_store *refs,
{
struct strbuf err = STRBUF_INIT;
struct object_id new_oid;
int ignore_errno;
if (logmsg &&
!refs_read_ref_full(&refs->base, target,
RESOLVE_REF_READING, &new_oid, NULL) &&
refs_resolve_ref_unsafe(&refs->base, target,
RESOLVE_REF_READING, &new_oid, NULL,
&ignore_errno) &&
files_log_ref_write(refs, refname, &lock->old_oid,
&new_oid, logmsg, 0, &err)) {
error("%s", err.buf);
@ -1887,7 +1924,7 @@ static int files_create_symref(struct ref_store *ref_store,
struct ref_lock *lock;
int ret;
lock = lock_ref_oid_basic(refs, refname, NULL, &err);
lock = lock_ref_oid_basic(refs, refname, &err);
if (!lock) {
error("%s", err.buf);
strbuf_release(&err);
@ -2120,6 +2157,7 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
(struct files_reflog_iterator *)ref_iterator;
struct dir_iterator *diter = iter->dir_iterator;
int ok;
int ignore_errno;
while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
int flags;
@ -2131,9 +2169,10 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
if (ends_with(diter->basename, ".lock"))
continue;
if (refs_read_ref_full(iter->ref_store,
diter->relative_path, 0,
&iter->oid, &flags)) {
if (!refs_resolve_ref_unsafe(iter->ref_store,
diter->relative_path, 0,
&iter->oid, &flags,
&ignore_errno)) {
error("bad ref for %s", diter->path.buf);
continue;
}
@ -2477,9 +2516,11 @@ static int lock_ref_for_update(struct files_ref_store *refs,
* the transaction, so we have to read it here
* to record and possibly check old_oid:
*/
if (refs_read_ref_full(&refs->base,
referent.buf, 0,
&lock->old_oid, NULL)) {
int ignore_errno;
if (!refs_resolve_ref_unsafe(&refs->base,
referent.buf, 0,
&lock->old_oid, NULL,
&ignore_errno)) {
if (update->flags & REF_HAVE_OLD) {
strbuf_addf(err, "cannot lock ref '%s': "
"error reading reference",
@ -3091,7 +3132,6 @@ static int files_reflog_expire(struct ref_store *ref_store,
struct strbuf log_file_sb = STRBUF_INIT;
char *log_file;
int status = 0;
int type;
struct strbuf err = STRBUF_INIT;
const struct object_id *oid;
@ -3105,7 +3145,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
* reference itself, plus we might need to update the
* reference if --updateref was specified:
*/
lock = lock_ref_oid_basic(refs, refname, &type, &err);
lock = lock_ref_oid_basic(refs, refname, &err);
if (!lock) {
error("cannot lock ref '%s': %s", refname, err.buf);
strbuf_release(&err);
@ -3167,9 +3207,20 @@ static int files_reflog_expire(struct ref_store *ref_store,
* a reference if there are no remaining reflog
* entries.
*/
int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
!(type & REF_ISSYMREF) &&
!is_null_oid(&cb.last_kept_oid);
int update = 0;
if ((flags & EXPIRE_REFLOGS_UPDATE_REF) &&
!is_null_oid(&cb.last_kept_oid)) {
int ignore_errno;
int type;
const char *ref;
ref = refs_resolve_ref_unsafe(&refs->base, refname,
RESOLVE_REF_NO_RECURSE,
NULL, &type,
&ignore_errno);
update = !!(ref && !(type & REF_ISSYMREF));
}
if (close_lock_file_gently(&reflog_lock)) {
status |= error("couldn't write %s: %s", log_file,

Просмотреть файл

@ -1354,6 +1354,7 @@ int is_packed_transaction_needed(struct ref_store *ref_store,
ret = 0;
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];
int failure_errno;
unsigned int type;
struct object_id oid;
@ -1364,9 +1365,9 @@ int is_packed_transaction_needed(struct ref_store *ref_store,
*/
continue;
if (!refs_read_raw_ref(ref_store, update->refname,
&oid, &referent, &type) ||
errno != ENOENT) {
if (!refs_read_raw_ref(ref_store, update->refname, &oid,
&referent, &type, &failure_errno) ||
failure_errno != ENOENT) {
/*
* We have to actually delete that reference
* -> this transaction is needed.

Просмотреть файл

@ -150,9 +150,9 @@ struct ref_update {
const char refname[FLEX_ARRAY];
};
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);
/*
* Write an error to `err` and return a nonzero value iff the same
@ -229,20 +229,6 @@ const char *find_descendant_ref(const char *dirname,
const struct string_list *extras,
const struct string_list *skip);
/*
* Check whether an attempt to rename old_refname to new_refname would
* cause a D/F conflict with any existing reference (other than
* possibly old_refname). If there would be a conflict, emit an error
* message and return false; otherwise, return true.
*
* Note that this function is not safe against all races with other
* processes (though rename_ref() catches some races that might get by
* this check).
*/
int refs_rename_ref_available(struct ref_store *refs,
const char *old_refname,
const char *new_refname);
/* We allow "recursive" symbolic refs. Only within reason, though */
#define SYMREF_MAXDEPTH 5
@ -713,10 +699,12 @@ struct ref_store {
};
/*
* Parse contents of a loose ref file.
* Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for
* invalid contents.
*/
int parse_loose_ref_contents(const char *buf, struct object_id *oid,
struct strbuf *referent, unsigned int *type);
struct strbuf *referent, unsigned int *type,
int *failure_errno);
/*
* Fill in the generic part of refs and add it to our collection of

Просмотреть файл

@ -1284,6 +1284,8 @@ void print_commit_summary(struct repository *r,
struct pretty_print_context pctx = {0};
struct strbuf author_ident = STRBUF_INIT;
struct strbuf committer_ident = STRBUF_INIT;
struct ref_store *refs;
int resolve_errno;
commit = lookup_commit(r, oid);
if (!commit)
@ -1333,9 +1335,13 @@ void print_commit_summary(struct repository *r,
rev.diffopt.break_opt = 0;
diff_setup_done(&rev.diffopt);
head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
if (!head)
refs = get_main_ref_store(the_repository);
head = refs_resolve_ref_unsafe(refs, "HEAD", 0, NULL, NULL,
&resolve_errno);
if (!head) {
errno = resolve_errno;
die_errno(_("unable to resolve HEAD after creating commit"));
}
if (!strcmp(head, "HEAD"))
head = _("detached HEAD");
else

Просмотреть файл

@ -123,9 +123,10 @@ static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
int resolve_flags = arg_flags(*argv++, "resolve-flags");
int flags;
const char *ref;
int ignore_errno;
ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
&oid, &flags);
&oid, &flags, &ignore_errno);
printf("%s %s 0x%x\n", oid_to_hex(&oid), ref ? ref : "(null)", flags);
return ref ? 0 : 1;
}

65
t/t1417-reflog-updateref.sh Executable file
Просмотреть файл

@ -0,0 +1,65 @@
#!/bin/sh
test_description='git reflog --updateref'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success 'setup' '
git init -b main repo &&
(
cd repo &&
test_commit A &&
test_commit B &&
test_commit C &&
cp .git/logs/HEAD HEAD.old &&
git reset --hard HEAD~ &&
cp HEAD.old .git/logs/HEAD
)
'
test_reflog_updateref () {
exp=$1
shift
args="$@"
test_expect_success REFFILES "get '$exp' with '$args'" '
test_when_finished "rm -rf copy" &&
cp -R repo copy &&
(
cd copy &&
$args &&
git rev-parse $exp >expect &&
git rev-parse HEAD >actual &&
test_cmp expect actual
)
'
}
test_reflog_updateref B git reflog delete --updateref HEAD@{0}
test_reflog_updateref B git reflog delete --updateref HEAD@{1}
test_reflog_updateref C git reflog delete --updateref main@{0}
test_reflog_updateref B git reflog delete --updateref main@{1}
test_reflog_updateref B git reflog delete --updateref --rewrite HEAD@{0}
test_reflog_updateref B git reflog delete --updateref --rewrite HEAD@{1}
test_reflog_updateref C git reflog delete --updateref --rewrite main@{0}
test_reflog_updateref B git reflog delete --updateref --rewrite main@{1}
test_reflog_updateref B test_must_fail git reflog expire HEAD@{0}
test_reflog_updateref B test_must_fail git reflog expire HEAD@{1}
test_reflog_updateref B test_must_fail git reflog expire main@{0}
test_reflog_updateref B test_must_fail git reflog expire main@{1}
test_reflog_updateref B test_must_fail git reflog expire --updateref HEAD@{0}
test_reflog_updateref B test_must_fail git reflog expire --updateref HEAD@{1}
test_reflog_updateref B test_must_fail git reflog expire --updateref main@{0}
test_reflog_updateref B test_must_fail git reflog expire --updateref main@{1}
test_reflog_updateref B test_must_fail git reflog expire --updateref --rewrite HEAD@{0}
test_reflog_updateref B test_must_fail git reflog expire --updateref --rewrite HEAD@{1}
test_reflog_updateref B test_must_fail git reflog expire --updateref --rewrite main@{0}
test_reflog_updateref B test_must_fail git reflog expire --updateref --rewrite main@{1}
test_done

Просмотреть файл

@ -731,6 +731,28 @@ test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for
test_must_fail git branch -m u v
'
test_expect_success SYMLINKS 'git branch -m with symlinked .git/refs' '
test_when_finished "rm -rf subdir" &&
git init --bare subdir &&
rm -rfv subdir/refs subdir/objects subdir/packed-refs &&
ln -s ../.git/refs subdir/refs &&
ln -s ../.git/objects subdir/objects &&
ln -s ../.git/packed-refs subdir/packed-refs &&
git -C subdir rev-parse --absolute-git-dir >subdir.dir &&
git rev-parse --absolute-git-dir >our.dir &&
! test_cmp subdir.dir our.dir &&
git -C subdir log &&
git -C subdir branch rename-src &&
git rev-parse rename-src >expect &&
git -C subdir branch -m rename-src rename-dest &&
git rev-parse rename-dest >actual &&
test_cmp expect actual &&
git branch -D rename-dest
'
test_expect_success 'test tracking setup via --track' '
git config remote.local.url . &&
git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&

Просмотреть файл

@ -28,11 +28,13 @@ static void add_head_info(struct worktree *wt)
{
int flags;
const char *target;
int ignore_errno;
target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt),
"HEAD",
0,
&wt->head_oid, &flags);
&wt->head_oid, &flags,
&ignore_errno);
if (!target)
return;
@ -418,6 +420,7 @@ const struct worktree *find_shared_symref(const char *symref,
const char *symref_target;
struct ref_store *refs;
int flags;
int ignore_errno;
if (wt->is_bare)
continue;
@ -435,7 +438,8 @@ const struct worktree *find_shared_symref(const char *symref,
refs = get_worktree_ref_store(wt);
symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
NULL, &flags);
NULL, &flags,
&ignore_errno);
if ((flags & REF_ISSYMREF) &&
symref_target && !strcmp(symref_target, target)) {
existing = wt;
@ -563,16 +567,17 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
struct worktree *wt = *p;
struct object_id oid;
int flag;
int ignore_errno;
if (wt->is_current)
continue;
strbuf_reset(&refname);
strbuf_worktree_ref(wt, &refname, "HEAD");
if (!refs_read_ref_full(get_main_ref_store(the_repository),
refname.buf,
RESOLVE_REF_READING,
&oid, &flag))
if (refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
refname.buf,
RESOLVE_REF_READING,
&oid, &flag, &ignore_errno))
ret = fn(refname.buf, &oid, flag, cb_data);
if (ret)
break;