Merge branch 'rs/resolve-ref-optional-result'

Code clean-up.

* rs/resolve-ref-optional-result:
  refs: pass NULL to resolve_ref_unsafe() if hash is not needed
  refs: pass NULL to refs_resolve_ref_unsafe() if hash is not needed
  refs: make sha1 output parameter of refs_resolve_ref_unsafe() optional
This commit is contained in:
Junio C Hamano 2017-09-28 14:47:56 +09:00
Родитель 2812ca7f0e 744c040b19
Коммит 73ecdc606e
17 изменённых файлов: 29 добавлений и 46 удалений

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

@ -191,9 +191,8 @@ int validate_new_branchname(const char *name, struct strbuf *ref,
if (!attr_only) {
const char *head;
struct object_id oid;
head = resolve_ref_unsafe("HEAD", 0, oid.hash, NULL);
head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
if (!is_bare_repository() && head && !strcmp(head, ref->buf))
die(_("Cannot force update the current branch."));
}

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

@ -1431,7 +1431,6 @@ static void print_summary(const char *prefix, const struct object_id *oid,
struct rev_info rev;
struct commit *commit;
struct strbuf format = STRBUF_INIT;
struct object_id junk_oid;
const char *head;
struct pretty_print_context pctx = {0};
struct strbuf author_ident = STRBUF_INIT;
@ -1484,7 +1483,7 @@ static void print_summary(const char *prefix, const struct object_id *oid,
rev.diffopt.break_opt = 0;
diff_setup_done(&rev.diffopt);
head = resolve_ref_unsafe("HEAD", 0, junk_oid.hash, NULL);
head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
if (!strcmp(head, "HEAD"))
head = _("detached HEAD");
else

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

@ -1660,10 +1660,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
check_head = 1;
if (check_head) {
struct object_id oid;
const char *ref, *v;
ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
oid.hash, NULL);
NULL, NULL);
if (ref && skip_prefix(ref, "refs/heads/", &v))
branch_name = xstrdup(v);
else

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

@ -1207,11 +1207,10 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
const char *dst_name;
struct string_list_item *item;
struct command *dst_cmd;
unsigned char sha1[GIT_MAX_RAWSZ];
int flag;
strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
dst_name = resolve_ref_unsafe(buf.buf, 0, sha1, &flag);
dst_name = resolve_ref_unsafe(buf.buf, 0, NULL, &flag);
strbuf_release(&buf);
if (!(flag & REF_ISSYMREF))

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

@ -558,14 +558,13 @@ static int read_remote_branches(const char *refname,
struct strbuf buf = STRBUF_INIT;
struct string_list_item *item;
int flag;
struct object_id orig_oid;
const char *symref;
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, RESOLVE_REF_READING,
orig_oid.hash, &flag);
NULL, &flag);
if (flag & REF_ISSYMREF)
item->util = xstrdup(symref);
else

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

@ -17,9 +17,8 @@
static char *get_default_remote(void)
{
char *dest = NULL, *ret;
unsigned char sha1[20];
struct strbuf sb = STRBUF_INIT;
const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
if (!refname)
die(_("No such ref: %s"), "HEAD");
@ -1089,8 +1088,7 @@ static const char *remote_submodule_branch(const char *path)
return "master";
if (!strcmp(branch, ".")) {
unsigned char sha1[20];
const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
if (!refname)
die(_("No such ref: %s"), "HEAD");

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

@ -12,9 +12,8 @@ static const char * const git_symbolic_ref_usage[] = {
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, 0, sha1, &flag);
const char *refname = resolve_ref_unsafe(HEAD, 0, NULL, &flag);
if (!refname)
die("No such ref: %s", HEAD);

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

@ -486,10 +486,9 @@ static int show_head_ref(const char *refname, const struct object_id *oid,
struct strbuf *buf = cb_data;
if (flag & REF_ISSYMREF) {
struct object_id unused;
const char *target = resolve_ref_unsafe(refname,
RESOLVE_REF_READING,
unused.hash, NULL);
NULL, NULL);
if (target)
strbuf_addf(buf, "ref: %s\n", strip_namespace(target));

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

@ -185,7 +185,6 @@ static const struct name_decoration *current_pointed_by_HEAD(const struct name_d
{
const struct name_decoration *list, *head = NULL;
const char *branch_name = NULL;
struct object_id unused;
int rru_flags;
/* First find HEAD */
@ -198,7 +197,7 @@ static const struct name_decoration *current_pointed_by_HEAD(const struct name_d
return NULL;
/* Now resolve and find the matching current branch */
branch_name = resolve_ref_unsafe("HEAD", 0, unused.hash, &rru_flags);
branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
if (!(rru_flags & REF_ISSYMREF))
return NULL;

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

@ -239,8 +239,7 @@ int read_ref(const char *refname, unsigned char *sha1)
int ref_exists(const char *refname)
{
unsigned char sha1[20];
return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, NULL, NULL);
}
static int filter_refs(const char *refname, const struct object_id *oid,
@ -286,12 +285,11 @@ static int warn_if_dangling_symref(const char *refname, const struct object_id *
{
struct warn_if_dangling_data *d = cb_data;
const char *resolves_to;
struct object_id junk;
if (!(flags & REF_ISSYMREF))
return 0;
resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL);
resolves_to = resolve_ref_unsafe(refname, 0, NULL, NULL);
if (!resolves_to
|| (d->refname
? strcmp(resolves_to, d->refname)
@ -1398,9 +1396,12 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
unsigned char *sha1, int *flags)
{
static struct strbuf sb_refname = STRBUF_INIT;
struct object_id unused_oid;
int unused_flags;
int symref_count;
if (!sha1)
sha1 = unused_oid.hash;
if (!flags)
flags = &unused_flags;

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

@ -10,10 +10,11 @@ struct worktree;
/*
* Resolve a reference, recursively following symbolic refererences.
*
* Store the referred-to object's name in sha1 and return the name of
* the non-symbolic reference that ultimately pointed at it. The
* return value, if not NULL, is a pointer into either a static buffer
* or the input ref.
* Return the name of the non-symbolic reference that ultimately pointed
* at the resolved object name. The return value, if not NULL, is a
* pointer into either a static buffer or the input ref.
*
* If sha1 is non-NULL, store the referred-to object's name in it.
*
* If the reference cannot be resolved to an object, the behavior
* depends on the RESOLVE_REF_READING flag:

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

@ -1676,13 +1676,12 @@ static int commit_ref_update(struct files_ref_store *refs,
* check with HEAD only which should cover 99% of all usage
* scenarios (even 100% of the default ones).
*/
struct object_id head_oid;
int head_flag;
const char *head_ref;
head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
RESOLVE_REF_READING,
head_oid.hash, &head_flag);
NULL, &head_flag);
if (head_ref && (head_flag & REF_ISSYMREF) &&
!strcmp(head_ref, lock->ref_name)) {
struct strbuf log_err = STRBUF_INIT;

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

@ -466,7 +466,6 @@ static void alias_all_urls(void)
static void read_config(void)
{
static int loaded;
struct object_id oid;
int flag;
if (loaded)
@ -475,7 +474,7 @@ static void read_config(void)
current_branch = NULL;
if (startup_info->have_repository) {
const char *head_ref = resolve_ref_unsafe("HEAD", 0, oid.hash, &flag);
const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag);
if (head_ref && (flag & REF_ISSYMREF) &&
skip_prefix(head_ref, "refs/heads/", &head_ref)) {
current_branch = make_branch(head_ref, 0);
@ -1105,10 +1104,9 @@ static struct ref *make_linked_ref(const char *name, struct ref ***tail)
static char *guess_ref(const char *name, struct ref *peer)
{
struct strbuf buf = STRBUF_INIT;
struct object_id oid;
const char *r = resolve_ref_unsafe(peer->name, RESOLVE_REF_READING,
oid.hash, NULL);
NULL, NULL);
if (!r)
return NULL;
@ -1166,12 +1164,11 @@ static int match_explicit(struct ref *src, struct ref *dst,
return -1;
if (!dst_value) {
struct object_id oid;
int flag;
dst_value = resolve_ref_unsafe(matched_src->name,
RESOLVE_REF_READING,
oid.hash, &flag);
NULL, &flag);
if (!dst_value ||
((flag & REF_ISSYMREF) &&
!starts_with(dst_value, "refs/heads/")))
@ -1792,10 +1789,9 @@ const char *branch_get_push(struct branch *branch, struct strbuf *err)
static int ignore_symref_update(const char *refname)
{
struct object_id oid;
int flag;
if (!resolve_ref_unsafe(refname, 0, oid.hash, &flag))
if (!resolve_ref_unsafe(refname, 0, NULL, &flag))
return 0; /* non-existing refs are OK */
return (flag & REF_ISSYMREF);
}

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

@ -2248,11 +2248,10 @@ static int handle_revision_pseudo_opt(const char *submodule,
static void NORETURN diagnose_missing_default(const char *def)
{
unsigned char sha1[20];
int flags;
const char *refname;
refname = resolve_ref_unsafe(def, 0, sha1, &flags);
refname = resolve_ref_unsafe(def, 0, NULL, &flags);
if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
die(_("your current branch appears to be broken"));

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

@ -26,7 +26,6 @@ static void set_upstreams(struct transport *transport, struct ref *refs,
const char *localname;
const char *tmp;
const char *remotename;
unsigned char sha[20];
int flag = 0;
/*
* Check suitability for tracking. Must be successful /
@ -44,7 +43,7 @@ static void set_upstreams(struct transport *transport, struct ref *refs,
localname = ref->peer_ref->name;
remotename = ref->name;
tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
sha, &flag);
NULL, &flag);
if (tmp && flag & REF_ISSYMREF &&
starts_with(tmp, "refs/heads/"))
localname = tmp;

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

@ -965,11 +965,10 @@ static int find_symref(const char *refname, const struct object_id *oid,
{
const char *symref_target;
struct string_list_item *item;
struct object_id unused;
if ((flag & REF_ISSYMREF) == 0)
return 0;
symref_target = resolve_ref_unsafe(refname, 0, unused.hash, &flag);
symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
if (!symref_target || (flag & REF_ISSYMREF) == 0)
die("'%s' is a symref but it is not?", refname);
item = string_list_append(cb_data, refname);

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

@ -307,7 +307,6 @@ const struct worktree *find_shared_symref(const char *symref,
for (i = 0; worktrees[i]; i++) {
struct worktree *wt = worktrees[i];
const char *symref_target;
unsigned char sha1[20];
struct ref_store *refs;
int flags;
@ -327,7 +326,7 @@ const struct worktree *find_shared_symref(const char *symref,
refs = get_worktree_ref_store(wt);
symref_target = refs_resolve_ref_unsafe(refs, symref, 0,
sha1, &flags);
NULL, &flags);
if ((flags & REF_ISSYMREF) && !strcmp(symref_target, target)) {
existing = wt;
break;