From cca5fa6406046c19ab5a8117fe71bf4402c00d88 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:06:57 +0000 Subject: [PATCH] refs: convert dwim_ref and expand_ref to struct object_id All of the callers of these functions just pass the hash member of a struct object_id, so convert them to use a pointer to struct object_id directly. Insert a check for NULL in expand_ref on a temporary basis; this check can be removed when resolve_ref_unsafe is converted as well. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- archive.c | 2 +- branch.c | 2 +- builtin/fast-export.c | 2 +- builtin/log.c | 2 +- builtin/merge-base.c | 2 +- builtin/merge.c | 2 +- builtin/rev-parse.c | 2 +- builtin/show-branch.c | 2 +- bundle.c | 2 +- refs.c | 15 ++++++++------- refs.h | 4 ++-- remote.c | 2 +- sha1_name.c | 6 +++--- upload-pack.c | 2 +- wt-status.c | 2 +- 15 files changed, 25 insertions(+), 24 deletions(-) diff --git a/archive.c b/archive.c index 1e41f4bbeb..0b7b62af0c 100644 --- a/archive.c +++ b/archive.c @@ -371,7 +371,7 @@ static void parse_treeish_arg(const char **argv, const char *colon = strchrnul(name, ':'); int refnamelen = colon - name; - if (!dwim_ref(name, refnamelen, oid.hash, &ref)) + if (!dwim_ref(name, refnamelen, &oid, &ref)) die("no such ref: %.*s", refnamelen, name); free(ref); } diff --git a/branch.c b/branch.c index 45029ea142..62f7b0d8c2 100644 --- a/branch.c +++ b/branch.c @@ -264,7 +264,7 @@ void create_branch(const char *name, const char *start_name, die(_("Not a valid object name: '%s'."), start_name); } - switch (dwim_ref(start_name, strlen(start_name), oid.hash, &real_ref)) { + switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref)) { case 0: /* Not branching from any existing branch */ if (explicit_tracking) diff --git a/builtin/fast-export.c b/builtin/fast-export.c index 2fb60d6d48..d74c73f777 100644 --- a/builtin/fast-export.c +++ b/builtin/fast-export.c @@ -823,7 +823,7 @@ static void get_tags_and_duplicates(struct rev_cmdline_info *info) if (e->flags & UNINTERESTING) continue; - if (dwim_ref(e->name, strlen(e->name), oid.hash, &full_name) != 1) + if (dwim_ref(e->name, strlen(e->name), &oid, &full_name) != 1) continue; if (refspecs) { diff --git a/builtin/log.c b/builtin/log.c index d81a09051e..ba9d4cd786 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -975,7 +975,7 @@ static char *find_branch_name(struct rev_info *rev) return NULL; ref = rev->cmdline.rev[positive].name; tip_oid = &rev->cmdline.rev[positive].item->oid; - if (dwim_ref(ref, strlen(ref), branch_oid.hash, &full_ref) && + if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref) && skip_prefix(full_ref, "refs/heads/", &v) && !oidcmp(tip_oid, &branch_oid)) branch = xstrdup(v); diff --git a/builtin/merge-base.c b/builtin/merge-base.c index 6dbd167d3b..e99f5405ce 100644 --- a/builtin/merge-base.c +++ b/builtin/merge-base.c @@ -156,7 +156,7 @@ static int handle_fork_point(int argc, const char **argv) struct commit_list *bases; int i, ret = 0; - switch (dwim_ref(argv[0], strlen(argv[0]), oid.hash, &refname)) { + switch (dwim_ref(argv[0], strlen(argv[0]), &oid, &refname)) { case 0: die("No such ref: '%s'", argv[0]); case 1: diff --git a/builtin/merge.c b/builtin/merge.c index 99d2df965f..6071dbfe34 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -454,7 +454,7 @@ static void merge_name(const char *remote, struct strbuf *msg) if (!remote_head) die(_("'%s' does not point to a commit"), remote); - if (dwim_ref(remote, strlen(remote), branch_head.hash, &found_ref) > 0) { + if (dwim_ref(remote, strlen(remote), &branch_head, &found_ref) > 0) { if (starts_with(found_ref, "refs/heads/")) { strbuf_addf(msg, "%s\t\tbranch '%s' of .\n", oid_to_hex(&branch_head), remote); diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index a8d7e6f7ae..74aa644cbb 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -133,7 +133,7 @@ static void show_rev(int type, const struct object_id *oid, const char *name) struct object_id discard; char *full; - switch (dwim_ref(name, strlen(name), discard.hash, &full)) { + switch (dwim_ref(name, strlen(name), &discard, &full)) { case 0: /* * Not found -- not a ref. We could diff --git a/builtin/show-branch.c b/builtin/show-branch.c index 0237be4975..722a7f4bec 100644 --- a/builtin/show-branch.c +++ b/builtin/show-branch.c @@ -720,7 +720,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) die(Q_("only %d entry can be shown at one time.", "only %d entries can be shown at one time.", MAX_REVS), MAX_REVS); - if (!dwim_ref(*av, strlen(*av), oid.hash, &ref)) + if (!dwim_ref(*av, strlen(*av), &oid, &ref)) die(_("no such ref %s"), *av); /* Has the base been specified? */ diff --git a/bundle.c b/bundle.c index 12658c5c9f..93290962c9 100644 --- a/bundle.c +++ b/bundle.c @@ -338,7 +338,7 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs) if (e->item->flags & UNINTERESTING) continue; - if (dwim_ref(e->name, strlen(e->name), oid.hash, &ref) != 1) + if (dwim_ref(e->name, strlen(e->name), &oid, &ref) != 1) goto skip_write_ref; if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag)) flag = 0; diff --git a/refs.c b/refs.c index 9c1f881328..9bb555e7ff 100644 --- a/refs.c +++ b/refs.c @@ -456,15 +456,15 @@ static char *substitute_branch_name(const char **string, int *len) return NULL; } -int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref) +int dwim_ref(const char *str, int len, struct object_id *oid, char **ref) { char *last_branch = substitute_branch_name(&str, &len); - int refs_found = expand_ref(str, len, sha1, ref); + int refs_found = expand_ref(str, len, oid, ref); free(last_branch); return refs_found; } -int expand_ref(const char *str, int len, unsigned char *sha1, char **ref) +int expand_ref(const char *str, int len, struct object_id *oid, char **ref) { const char **p, *r; int refs_found = 0; @@ -472,15 +472,16 @@ int expand_ref(const char *str, int len, unsigned char *sha1, char **ref) *ref = NULL; for (p = ref_rev_parse_rules; *p; p++) { - unsigned char sha1_from_ref[20]; - unsigned char *this_result; + struct object_id oid_from_ref; + struct object_id *this_result; int flag; - this_result = refs_found ? sha1_from_ref : sha1; + this_result = refs_found ? &oid_from_ref : oid; strbuf_reset(&fullref); strbuf_addf(&fullref, *p, len, str); r = resolve_ref_unsafe(fullref.buf, RESOLVE_REF_READING, - this_result, &flag); + this_result ? this_result->hash : NULL, + &flag); if (r) { if (!refs_found++) *ref = xstrdup(r); diff --git a/refs.h b/refs.h index 30fbd6f683..0d864b0ab1 100644 --- a/refs.h +++ b/refs.h @@ -139,8 +139,8 @@ int resolve_gitlink_ref(const char *submodule, const char *refname, */ int refname_match(const char *abbrev_name, const char *full_name); -int expand_ref(const char *str, int len, unsigned char *sha1, char **ref); -int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref); +int expand_ref(const char *str, int len, struct object_id *oid, char **ref); +int dwim_ref(const char *str, int len, struct object_id *oid, char **ref); int dwim_log(const char *str, int len, unsigned char *sha1, char **ref); /* diff --git a/remote.c b/remote.c index 698a890a83..685e776a65 100644 --- a/remote.c +++ b/remote.c @@ -1629,7 +1629,7 @@ static void set_merge(struct branch *ret) strcmp(ret->remote_name, ".")) continue; if (dwim_ref(ret->merge_name[i], strlen(ret->merge_name[i]), - oid.hash, &ref) == 1) + &oid, &ref) == 1) ret->merge[i]->dst = ref; else ret->merge[i]->dst = xstrdup(ret->merge_name[i]); diff --git a/sha1_name.c b/sha1_name.c index c7c5ab376c..d8ff831759 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -603,7 +603,7 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid, if (len == GIT_SHA1_HEXSZ && !get_oid_hex(str, oid)) { if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) { - refs_found = dwim_ref(str, len, tmp_oid.hash, &real_ref); + refs_found = dwim_ref(str, len, &tmp_oid, &real_ref); if (refs_found > 0) { warning(warn_msg, len, str); if (advice_object_name_warning) @@ -654,11 +654,11 @@ static int get_oid_basic(const char *str, int len, struct object_id *oid, if (!len && reflog_len) /* allow "@{...}" to mean the current branch reflog */ - refs_found = dwim_ref("HEAD", 4, oid->hash, &real_ref); + refs_found = dwim_ref("HEAD", 4, oid, &real_ref); else if (reflog_len) refs_found = dwim_log(str, len, oid->hash, &real_ref); else - refs_found = dwim_ref(str, len, oid->hash, &real_ref); + refs_found = dwim_ref(str, len, oid, &real_ref); if (!refs_found) return -1; diff --git a/upload-pack.c b/upload-pack.c index e25f725c0f..030eba5a0c 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -787,7 +787,7 @@ static void receive_needs(void) if (skip_prefix(line, "deepen-not ", &arg)) { char *ref = NULL; struct object_id oid; - if (expand_ref(arg, strlen(arg), oid.hash, &ref) != 1) + if (expand_ref(arg, strlen(arg), &oid, &ref) != 1) die("git upload-pack: ambiguous deepen-not: %s", line); string_list_append(&deepen_not, ref); free(ref); diff --git a/wt-status.c b/wt-status.c index 29bc64cc02..a798b7ea1e 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1449,7 +1449,7 @@ static void wt_status_get_detached_from(struct wt_status_state *state) return; } - if (dwim_ref(cb.buf.buf, cb.buf.len, oid.hash, &ref) == 1 && + if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref) == 1 && /* sha1 is a commit? match without further lookup */ (!oidcmp(&cb.noid, &oid) || /* perhaps sha1 is a tag, try to dereference to a commit */ -- 2.39.2