Merge branch 'nd/resolve-ref'

* nd/resolve-ref:
  Copy resolve_ref() return value for longer use
  Convert many resolve_ref() calls to read_ref*() and ref_exists()

Conflicts:
	builtin/fmt-merge-msg.c
	builtin/merge.c
	refs.c
This commit is contained in:
Junio C Hamano 2011-12-09 13:37:14 -08:00
commit b7f7c07977
17 changed files with 98 additions and 59 deletions

View File

@ -115,8 +115,10 @@ static int branch_merged(int kind, const char *name,
branch->merge[0] && branch->merge[0] &&
branch->merge[0]->dst && branch->merge[0]->dst &&
(reference_name = (reference_name =
resolve_ref(branch->merge[0]->dst, sha1, 1, NULL)) != NULL) resolve_ref(branch->merge[0]->dst, sha1, 1, NULL)) != NULL) {
reference_name = xstrdup(reference_name);
reference_rev = lookup_commit_reference(sha1); reference_rev = lookup_commit_reference(sha1);
}
} }
if (!reference_rev) if (!reference_rev)
reference_rev = head_rev; reference_rev = head_rev;
@ -141,6 +143,7 @@ static int branch_merged(int kind, const char *name,
" '%s', even though it is merged to HEAD."), " '%s', even though it is merged to HEAD."),
name, reference_name); name, reference_name);
} }
free((char *)reference_name);
return merged; return merged;
} }
@ -186,7 +189,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
free(name); free(name);
name = xstrdup(mkpath(fmt, bname.buf)); name = xstrdup(mkpath(fmt, bname.buf));
if (!resolve_ref(name, sha1, 1, NULL)) { if (read_ref(name, sha1)) {
error(_("%sbranch '%s' not found."), error(_("%sbranch '%s' not found."),
remote, bname.buf); remote, bname.buf);
ret = 1; ret = 1;
@ -565,7 +568,6 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
static void rename_branch(const char *oldname, const char *newname, int force) static void rename_branch(const char *oldname, const char *newname, int force)
{ {
struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT; struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
unsigned char sha1[20];
struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT; struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
int recovery = 0; int recovery = 0;
@ -577,7 +579,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
* Bad name --- this could be an attempt to rename a * Bad name --- this could be an attempt to rename a
* ref that we used to allow to be created by accident. * ref that we used to allow to be created by accident.
*/ */
if (resolve_ref(oldref.buf, sha1, 1, NULL)) if (ref_exists(oldref.buf))
recovery = 1; recovery = 1;
else else
die(_("Invalid branch name: '%s'"), oldname); die(_("Invalid branch name: '%s'"), oldname);

View File

@ -288,7 +288,7 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
commit_locked_index(lock_file)) commit_locked_index(lock_file))
die(_("unable to write new index file")); die(_("unable to write new index file"));
resolve_ref("HEAD", rev, 0, &flag); read_ref_full("HEAD", rev, 0, &flag);
head = lookup_commit_reference_gently(rev, 1); head = lookup_commit_reference_gently(rev, 1);
errs |= post_checkout_hook(head, head, 0); errs |= post_checkout_hook(head, head, 0);
@ -699,7 +699,9 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
unsigned char rev[20]; unsigned char rev[20];
int flag; int flag;
memset(&old, 0, sizeof(old)); memset(&old, 0, sizeof(old));
old.path = xstrdup(resolve_ref("HEAD", rev, 0, &flag)); old.path = resolve_ref("HEAD", rev, 0, &flag);
if (old.path)
old.path = xstrdup(old.path);
old.commit = lookup_commit_reference_gently(rev, 1); old.commit = lookup_commit_reference_gently(rev, 1);
if (!(flag & REF_ISSYMREF)) { if (!(flag & REF_ISSYMREF)) {
free((char *)old.path); free((char *)old.path);
@ -866,7 +868,7 @@ static int parse_branchname_arg(int argc, const char **argv,
setup_branch_path(new); setup_branch_path(new);
if (!check_refname_format(new->path, 0) && if (!check_refname_format(new->path, 0) &&
resolve_ref(new->path, branch_rev, 1, NULL)) !read_ref(new->path, branch_rev))
hashcpy(rev, branch_rev); hashcpy(rev, branch_rev);
else else
new->path = NULL; /* not an existing branch */ new->path = NULL; /* not an existing branch */

View File

@ -1259,7 +1259,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
struct commit *commit; struct commit *commit;
struct strbuf format = STRBUF_INIT; struct strbuf format = STRBUF_INIT;
unsigned char junk_sha1[20]; unsigned char junk_sha1[20];
const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL); const char *head;
struct pretty_print_context pctx = {0}; struct pretty_print_context pctx = {0};
struct strbuf author_ident = STRBUF_INIT; struct strbuf author_ident = STRBUF_INIT;
struct strbuf committer_ident = STRBUF_INIT; struct strbuf committer_ident = STRBUF_INIT;
@ -1304,6 +1304,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
rev.diffopt.break_opt = 0; rev.diffopt.break_opt = 0;
diff_setup_done(&rev.diffopt); diff_setup_done(&rev.diffopt);
head = resolve_ref("HEAD", junk_sha1, 0, NULL);
printf("[%s%s ", printf("[%s%s ",
!prefixcmp(head, "refs/heads/") ? !prefixcmp(head, "refs/heads/") ?
head + 11 : head + 11 :

View File

@ -379,6 +379,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
die("No current branch"); die("No current branch");
if (!prefixcmp(current_branch, "refs/heads/")) if (!prefixcmp(current_branch, "refs/heads/"))
current_branch += 11; current_branch += 11;
current_branch = xstrdup(current_branch);
/* get a line */ /* get a line */
while (pos < in->len) { while (pos < in->len) {
@ -420,6 +421,7 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
} }
strbuf_complete_line(out); strbuf_complete_line(out);
free((char *)current_branch);
return 0; return 0;
} }

View File

@ -413,7 +413,7 @@ static void finish(struct commit *head_commit,
static void merge_name(const char *remote, struct strbuf *msg) static void merge_name(const char *remote, struct strbuf *msg)
{ {
struct commit *remote_head; struct commit *remote_head;
unsigned char branch_head[20], buf_sha[20]; unsigned char branch_head[20];
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct strbuf bname = STRBUF_INIT; struct strbuf bname = STRBUF_INIT;
const char *ptr; const char *ptr;
@ -477,7 +477,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
strbuf_addstr(&truname, "refs/heads/"); strbuf_addstr(&truname, "refs/heads/");
strbuf_addstr(&truname, remote); strbuf_addstr(&truname, remote);
strbuf_setlen(&truname, truname.len - len); strbuf_setlen(&truname, truname.len - len);
if (resolve_ref(truname.buf, buf_sha, 1, NULL)) { if (ref_exists(truname.buf)) {
strbuf_addf(msg, strbuf_addf(msg,
"%s\t\tbranch '%s'%s of .\n", "%s\t\tbranch '%s'%s of .\n",
sha1_to_hex(remote_head->object.sha1), sha1_to_hex(remote_head->object.sha1),
@ -1091,7 +1091,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
struct commit *head_commit; struct commit *head_commit;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
const char *head_arg; const char *head_arg;
int flag, i; int flag, i, ret = 0;
int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0; int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
struct commit_list *common = NULL; struct commit_list *common = NULL;
const char *best_strategy = NULL, *wt_strategy = NULL; const char *best_strategy = NULL, *wt_strategy = NULL;
@ -1105,8 +1105,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
* current branch. * current branch.
*/ */
branch = resolve_ref("HEAD", head_sha1, 0, &flag); branch = resolve_ref("HEAD", head_sha1, 0, &flag);
if (branch && !prefixcmp(branch, "refs/heads/")) if (branch) {
branch += 11; if (!prefixcmp(branch, "refs/heads/"))
branch += 11;
branch = xstrdup(branch);
}
if (!branch || is_null_sha1(head_sha1)) if (!branch || is_null_sha1(head_sha1))
head_commit = NULL; head_commit = NULL;
else else
@ -1132,7 +1135,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
die(_("There is no merge to abort (MERGE_HEAD missing).")); die(_("There is no merge to abort (MERGE_HEAD missing)."));
/* Invoke 'git reset --merge' */ /* Invoke 'git reset --merge' */
return cmd_reset(nargc, nargv, prefix); ret = cmd_reset(nargc, nargv, prefix);
goto done;
} }
if (read_cache_unmerged()) if (read_cache_unmerged())
@ -1219,7 +1223,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
read_empty(remote_head->object.sha1, 0); read_empty(remote_head->object.sha1, 0);
update_ref("initial pull", "HEAD", remote_head->object.sha1, update_ref("initial pull", "HEAD", remote_head->object.sha1,
NULL, 0, DIE_ON_ERR); NULL, 0, DIE_ON_ERR);
return 0; goto done;
} else { } else {
struct strbuf merge_names = STRBUF_INIT; struct strbuf merge_names = STRBUF_INIT;
@ -1308,7 +1312,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
* but first the most common case of merging one remote. * but first the most common case of merging one remote.
*/ */
finish_up_to_date("Already up-to-date."); finish_up_to_date("Already up-to-date.");
return 0; goto done;
} else if (allow_fast_forward && !remoteheads->next && } else if (allow_fast_forward && !remoteheads->next &&
!common->next && !common->next &&
!hashcmp(common->item->object.sha1, head_commit->object.sha1)) { !hashcmp(common->item->object.sha1, head_commit->object.sha1)) {
@ -1329,16 +1333,20 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
strbuf_addstr(&msg, strbuf_addstr(&msg,
" (no commit created; -m option ignored)"); " (no commit created; -m option ignored)");
commit = remoteheads->item; commit = remoteheads->item;
if (!commit) if (!commit) {
return 1; ret = 1;
goto done;
}
if (checkout_fast_forward(head_commit->object.sha1, if (checkout_fast_forward(head_commit->object.sha1,
commit->object.sha1)) commit->object.sha1)) {
return 1; ret = 1;
goto done;
}
finish(head_commit, commit->object.sha1, msg.buf); finish(head_commit, commit->object.sha1, msg.buf);
drop_save(); drop_save();
return 0; goto done;
} else if (!remoteheads->next && common->next) } else if (!remoteheads->next && common->next)
; ;
/* /*
@ -1356,8 +1364,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
git_committer_info(IDENT_ERROR_ON_NO_NAME); git_committer_info(IDENT_ERROR_ON_NO_NAME);
printf(_("Trying really trivial in-index merge...\n")); printf(_("Trying really trivial in-index merge...\n"));
if (!read_tree_trivial(common->item->object.sha1, if (!read_tree_trivial(common->item->object.sha1,
head_commit->object.sha1, remoteheads->item->object.sha1)) head_commit->object.sha1,
return merge_trivial(head_commit); remoteheads->item->object.sha1)) {
ret = merge_trivial(head_commit);
goto done;
}
printf(_("Nope.\n")); printf(_("Nope.\n"));
} }
} else { } else {
@ -1385,7 +1396,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
} }
if (up_to_date) { if (up_to_date) {
finish_up_to_date("Already up-to-date. Yeeah!"); finish_up_to_date("Already up-to-date. Yeeah!");
return 0; goto done;
} }
} }
@ -1467,9 +1478,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
* If we have a resulting tree, that means the strategy module * If we have a resulting tree, that means the strategy module
* auto resolved the merge cleanly. * auto resolved the merge cleanly.
*/ */
if (automerge_was_ok) if (automerge_was_ok) {
return finish_automerge(head_commit, common, result_tree, ret = finish_automerge(head_commit, common, result_tree,
wt_strategy); wt_strategy);
goto done;
}
/* /*
* Pick the result from the best strategy and have the user fix * Pick the result from the best strategy and have the user fix
@ -1483,7 +1496,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
else else
fprintf(stderr, _("Merge with strategy %s failed.\n"), fprintf(stderr, _("Merge with strategy %s failed.\n"),
use_strategies[0]->name); use_strategies[0]->name);
return 2; ret = 2;
goto done;
} else if (best_strategy == wt_strategy) } else if (best_strategy == wt_strategy)
; /* We already have its result in the working tree. */ ; /* We already have its result in the working tree. */
else { else {
@ -1499,10 +1513,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
else else
write_merge_state(); write_merge_state();
if (merge_was_ok) { if (merge_was_ok)
fprintf(stderr, _("Automatic merge went well; " fprintf(stderr, _("Automatic merge went well; "
"stopped before committing as requested\n")); "stopped before committing as requested\n"));
return 0; else
} else ret = suggest_conflicts(option_renormalize);
return suggest_conflicts(option_renormalize);
done:
free((char *)branch);
return ret;
} }

View File

@ -804,6 +804,7 @@ static int merge_commit(struct notes_merge_options *o)
struct notes_tree *t; struct notes_tree *t;
struct commit *partial; struct commit *partial;
struct pretty_print_context pretty_ctx; struct pretty_print_context pretty_ctx;
int ret;
/* /*
* Read partial merge result from .git/NOTES_MERGE_PARTIAL, * Read partial merge result from .git/NOTES_MERGE_PARTIAL,
@ -828,6 +829,7 @@ static int merge_commit(struct notes_merge_options *o)
o->local_ref = resolve_ref("NOTES_MERGE_REF", sha1, 0, NULL); o->local_ref = resolve_ref("NOTES_MERGE_REF", sha1, 0, NULL);
if (!o->local_ref) if (!o->local_ref)
die("Failed to resolve NOTES_MERGE_REF"); die("Failed to resolve NOTES_MERGE_REF");
o->local_ref = xstrdup(o->local_ref);
if (notes_merge_commit(o, t, partial, sha1)) if (notes_merge_commit(o, t, partial, sha1))
die("Failed to finalize notes merge"); die("Failed to finalize notes merge");
@ -843,7 +845,9 @@ static int merge_commit(struct notes_merge_options *o)
free_notes(t); free_notes(t);
strbuf_release(&msg); strbuf_release(&msg);
return merge_abort(o); ret = merge_abort(o);
free((char *)o->local_ref);
return ret;
} }
static int merge(int argc, const char **argv, const char *prefix) static int merge(int argc, const char **argv, const char *prefix)

View File

@ -695,7 +695,10 @@ static void execute_commands(struct command *commands, const char *unpacker_erro
check_aliased_updates(commands); check_aliased_updates(commands);
free((char *)head_name);
head_name = resolve_ref("HEAD", sha1, 0, NULL); head_name = resolve_ref("HEAD", sha1, 0, NULL);
if (head_name)
head_name = xstrdup(head_name);
for (cmd = commands; cmd; cmd = cmd->next) for (cmd = commands; cmd; cmd = cmd->next)
if (!cmd->skip_update) if (!cmd->skip_update)

View File

@ -343,8 +343,7 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
states->tracked.strdup_strings = 1; states->tracked.strdup_strings = 1;
states->stale.strdup_strings = 1; states->stale.strdup_strings = 1;
for (ref = fetch_map; ref; ref = ref->next) { for (ref = fetch_map; ref; ref = ref->next) {
unsigned char sha1[20]; if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
string_list_append(&states->new, abbrev_branch(ref->name)); string_list_append(&states->new, abbrev_branch(ref->name));
else else
string_list_append(&states->tracked, abbrev_branch(ref->name)); string_list_append(&states->tracked, abbrev_branch(ref->name));
@ -710,7 +709,7 @@ static int mv(int argc, const char **argv)
int flag = 0; int flag = 0;
unsigned char sha1[20]; unsigned char sha1[20];
resolve_ref(item->string, sha1, 1, &flag); read_ref_full(item->string, sha1, 1, &flag);
if (!(flag & REF_ISSYMREF)) if (!(flag & REF_ISSYMREF))
continue; continue;
if (delete_ref(item->string, NULL, REF_NODEREF)) if (delete_ref(item->string, NULL, REF_NODEREF))
@ -1220,10 +1219,9 @@ static int set_head(int argc, const char **argv)
usage_with_options(builtin_remote_sethead_usage, options); usage_with_options(builtin_remote_sethead_usage, options);
if (head_name) { if (head_name) {
unsigned char sha1[20];
strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name); strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
/* make sure it's valid */ /* make sure it's valid */
if (!resolve_ref(buf2.buf, sha1, 1, NULL)) if (!ref_exists(buf2.buf))
result |= error("Not a valid ref: %s", buf2.buf); result |= error("Not a valid ref: %s", buf2.buf);
else if (create_symref(buf.buf, buf2.buf, "remote set-head")) else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
result |= error("Could not setup %s", buf.buf); result |= error("Could not setup %s", buf.buf);

View File

@ -58,7 +58,7 @@ static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
had_error = 1; had_error = 1;
continue; continue;
} }
if (!resolve_ref(ref, sha1, 1, NULL)) { if (read_ref(ref, sha1)) {
error("replace ref '%s' not found.", *p); error("replace ref '%s' not found.", *p);
had_error = 1; had_error = 1;
continue; continue;
@ -97,7 +97,7 @@ static int replace_object(const char *object_ref, const char *replace_ref,
if (check_refname_format(ref, 0)) if (check_refname_format(ref, 0))
die("'%s' is not a valid ref name.", ref); die("'%s' is not a valid ref name.", ref);
if (!resolve_ref(ref, prev, 1, NULL)) if (read_ref(ref, prev))
hashclr(prev); hashclr(prev);
else if (!force) else if (!force)
die("replace ref '%s' already exists", ref); die("replace ref '%s' already exists", ref);

View File

@ -225,7 +225,7 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
unsigned char sha1[20]; unsigned char sha1[20];
if (!prefixcmp(*pattern, "refs/") && if (!prefixcmp(*pattern, "refs/") &&
resolve_ref(*pattern, sha1, 1, NULL)) { !read_ref(*pattern, sha1)) {
if (!quiet) if (!quiet)
show_one(*pattern, sha1); show_one(*pattern, sha1);
} }

View File

@ -173,7 +173,7 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
had_error = 1; had_error = 1;
continue; continue;
} }
if (!resolve_ref(ref, sha1, 1, NULL)) { if (read_ref(ref, sha1)) {
error(_("tag '%s' not found."), *p); error(_("tag '%s' not found."), *p);
had_error = 1; had_error = 1;
continue; continue;
@ -454,7 +454,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
if (strbuf_check_tag_ref(&ref, tag)) if (strbuf_check_tag_ref(&ref, tag))
die(_("'%s' is not a valid tag name."), tag); die(_("'%s' is not a valid tag name."), tag);
if (!resolve_ref(ref.buf, prev, 1, NULL)) if (read_ref(ref.buf, prev))
hashclr(prev); hashclr(prev);
else if (!force) else if (!force)
die(_("tag '%s' already exists"), tag); die(_("tag '%s' already exists"), tag);

View File

@ -320,7 +320,7 @@ int create_bundle(struct bundle_header *header, const char *path,
continue; continue;
if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1) if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
continue; continue;
if (!resolve_ref(e->name, sha1, 1, &flag)) if (read_ref_full(e->name, sha1, 1, &flag))
flag = 0; flag = 0;
display_ref = (flag & REF_ISSYMREF) ? e->name : ref; display_ref = (flag & REF_ISSYMREF) ? e->name : ref;

View File

@ -831,6 +831,8 @@ static inline int get_sha1_with_context(const char *str, unsigned char *sha1, st
extern int get_sha1_hex(const char *hex, unsigned char *sha1); extern int get_sha1_hex(const char *hex, unsigned char *sha1);
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */ extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
extern int read_ref_full(const char *filename, unsigned char *sha1,
int reading, int *flags);
extern int read_ref(const char *filename, unsigned char *sha1); extern int read_ref(const char *filename, unsigned char *sha1);
/* /*

View File

@ -573,7 +573,7 @@ int notes_merge(struct notes_merge_options *o,
o->local_ref, o->remote_ref); o->local_ref, o->remote_ref);
/* Dereference o->local_ref into local_sha1 */ /* Dereference o->local_ref into local_sha1 */
if (!resolve_ref(o->local_ref, local_sha1, 0, NULL)) if (read_ref_full(o->local_ref, local_sha1, 0, NULL))
die("Failed to resolve local notes ref '%s'", o->local_ref); die("Failed to resolve local notes ref '%s'", o->local_ref);
else if (!check_refname_format(o->local_ref, 0) && else if (!check_refname_format(o->local_ref, 0) &&
is_null_sha1(local_sha1)) is_null_sha1(local_sha1))

View File

@ -51,8 +51,11 @@ static struct complete_reflogs *read_complete_reflog(const char *ref)
if (reflogs->nr == 0) { if (reflogs->nr == 0) {
unsigned char sha1[20]; unsigned char sha1[20];
const char *name = resolve_ref(ref, sha1, 1, NULL); const char *name = resolve_ref(ref, sha1, 1, NULL);
if (name) if (name) {
name = xstrdup(name);
for_each_reflog_ent(name, read_one_reflog, reflogs); for_each_reflog_ent(name, read_one_reflog, reflogs);
free((char *)name);
}
} }
if (reflogs->nr == 0) { if (reflogs->nr == 0) {
int len = strlen(ref); int len = strlen(ref);

27
refs.c
View File

@ -334,7 +334,7 @@ static void get_ref_dir(const char *submodule, const char *base,
hashclr(sha1); hashclr(sha1);
flag |= REF_ISBROKEN; flag |= REF_ISBROKEN;
} }
} else if (!resolve_ref(ref, sha1, 1, &flag)) { } else if (read_ref_full(ref, sha1, 1, &flag)) {
hashclr(sha1); hashclr(sha1);
flag |= REF_ISBROKEN; flag |= REF_ISBROKEN;
} }
@ -612,13 +612,18 @@ struct ref_filter {
void *cb_data; void *cb_data;
}; };
int read_ref(const char *ref, unsigned char *sha1) int read_ref_full(const char *ref, unsigned char *sha1, int reading, int *flags)
{ {
if (resolve_ref(ref, sha1, 1, NULL)) if (resolve_ref(ref, sha1, reading, flags))
return 0; return 0;
return -1; return -1;
} }
int read_ref(const char *ref, unsigned char *sha1)
{
return read_ref_full(ref, sha1, 1, NULL);
}
#define DO_FOR_EACH_INCLUDE_BROKEN 01 #define DO_FOR_EACH_INCLUDE_BROKEN 01
static int do_one_ref(const char *base, each_ref_fn fn, int trim, static int do_one_ref(const char *base, each_ref_fn fn, int trim,
int flags, void *cb_data, struct ref_entry *entry) int flags, void *cb_data, struct ref_entry *entry)
@ -663,7 +668,7 @@ int peel_ref(const char *ref, unsigned char *sha1)
goto fallback; goto fallback;
} }
if (!resolve_ref(ref, base, 1, &flag)) if (read_ref_full(ref, base, 1, &flag))
return -1; return -1;
if ((flag & REF_ISPACKED)) { if ((flag & REF_ISPACKED)) {
@ -746,7 +751,7 @@ static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
return 0; return 0;
} }
if (resolve_ref("HEAD", sha1, 1, &flag)) if (!read_ref_full("HEAD", sha1, 1, &flag))
return fn("HEAD", sha1, flag, cb_data); return fn("HEAD", sha1, flag, cb_data);
return 0; return 0;
@ -826,7 +831,7 @@ int head_ref_namespaced(each_ref_fn fn, void *cb_data)
int flag; int flag;
strbuf_addf(&buf, "%sHEAD", get_git_namespace()); strbuf_addf(&buf, "%sHEAD", get_git_namespace());
if (resolve_ref(buf.buf, sha1, 1, &flag)) if (!read_ref_full(buf.buf, sha1, 1, &flag))
ret = fn(buf.buf, sha1, flag, cb_data); ret = fn(buf.buf, sha1, flag, cb_data);
strbuf_release(&buf); strbuf_release(&buf);
@ -1015,7 +1020,7 @@ int refname_match(const char *abbrev_name, const char *full_name, const char **r
static struct ref_lock *verify_lock(struct ref_lock *lock, static struct ref_lock *verify_lock(struct ref_lock *lock,
const unsigned char *old_sha1, int mustexist) const unsigned char *old_sha1, int mustexist)
{ {
if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist, NULL)) { if (read_ref_full(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
error("Can't verify ref %s", lock->ref_name); error("Can't verify ref %s", lock->ref_name);
unlock_ref(lock); unlock_ref(lock);
return NULL; return NULL;
@ -1370,7 +1375,8 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
goto rollback; goto rollback;
} }
if (resolve_ref(newref, sha1, 1, &flag) && delete_ref(newref, sha1, REF_NODEREF)) { if (!read_ref_full(newref, sha1, 1, &flag) &&
delete_ref(newref, sha1, REF_NODEREF)) {
if (errno==EISDIR) { if (errno==EISDIR) {
if (remove_empty_directories(git_path("%s", newref))) { if (remove_empty_directories(git_path("%s", newref))) {
error("Directory not empty: %s", newref); error("Directory not empty: %s", newref);
@ -1922,7 +1928,7 @@ static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
retval = do_for_each_reflog(log, fn, cb_data); retval = do_for_each_reflog(log, fn, cb_data);
} else { } else {
unsigned char sha1[20]; unsigned char sha1[20];
if (!resolve_ref(log, sha1, 0, NULL)) if (read_ref_full(log, sha1, 0, NULL))
retval = error("bad ref for %s", log); retval = error("bad ref for %s", log);
else else
retval = fn(log, sha1, 0, cb_data); retval = fn(log, sha1, 0, cb_data);
@ -2065,7 +2071,6 @@ char *shorten_unambiguous_ref(const char *ref, int strict)
*/ */
for (j = 0; j < rules_to_fail; j++) { for (j = 0; j < rules_to_fail; j++) {
const char *rule = ref_rev_parse_rules[j]; const char *rule = ref_rev_parse_rules[j];
unsigned char short_objectname[20];
char refname[PATH_MAX]; char refname[PATH_MAX];
/* skip matched rule */ /* skip matched rule */
@ -2079,7 +2084,7 @@ char *shorten_unambiguous_ref(const char *ref, int strict)
*/ */
mksnpath(refname, sizeof(refname), mksnpath(refname, sizeof(refname),
rule, short_name_len, short_name); rule, short_name_len, short_name);
if (!read_ref(refname, short_objectname)) if (ref_exists(refname))
break; break;
} }

View File

@ -1507,13 +1507,13 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
* nothing to report. * nothing to report.
*/ */
base = branch->merge[0]->dst; base = branch->merge[0]->dst;
if (!resolve_ref(base, sha1, 1, NULL)) if (read_ref(base, sha1))
return 0; return 0;
theirs = lookup_commit_reference(sha1); theirs = lookup_commit_reference(sha1);
if (!theirs) if (!theirs)
return 0; return 0;
if (!resolve_ref(branch->refname, sha1, 1, NULL)) if (read_ref(branch->refname, sha1))
return 0; return 0;
ours = lookup_commit_reference(sha1); ours = lookup_commit_reference(sha1);
if (!ours) if (!ours)