From 2b2a5be394bc67bed86bc009195c664dca740bd6 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Mon, 25 May 2015 18:38:28 +0000 Subject: each_ref_fn: change to take an object_id parameter Change typedef each_ref_fn to take a "const struct object_id *oid" parameter instead of "const unsigned char *sha1". To aid this transition, implement an adapter that can be used to wrap old-style functions matching the old typedef, which is now called "each_ref_sha1_fn"), and make such functions callable via the new interface. This requires the old function and its cb_data to be wrapped in a "struct each_ref_fn_sha1_adapter", and that object to be used as the cb_data for an adapter function, each_ref_fn_adapter(). This is an enormous diff, but most of it consists of simple, mechanical changes to the sites that call any of the "for_each_ref" family of functions. Subsequent to this change, the call sites can be rewritten one by one to use the new interface. Signed-off-by: Michael Haggerty Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- builtin/show-ref.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'builtin/show-ref.c') diff --git a/builtin/show-ref.c b/builtin/show-ref.c index afb10309d6..8e25536077 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -109,8 +109,10 @@ static int exclude_existing(const char *match) static struct string_list existing_refs = STRING_LIST_INIT_DUP; char buf[1024]; int matchlen = match ? strlen(match) : 0; + struct each_ref_fn_sha1_adapter wrapped_add_existing = + {add_existing, &existing_refs}; - for_each_ref(add_existing, &existing_refs); + for_each_ref(each_ref_fn_adapter, &wrapped_add_existing); while (fgets(buf, sizeof(buf), stdin)) { char *ref; int len = strlen(buf); @@ -191,6 +193,9 @@ static const struct option show_ref_options[] = { int cmd_show_ref(int argc, const char **argv, const char *prefix) { + struct each_ref_fn_sha1_adapter wrapped_show_ref = + {show_ref, NULL}; + if (argc == 2 && !strcmp(argv[1], "-h")) usage_with_options(show_ref_usage, show_ref_options); @@ -225,8 +230,8 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix) } if (show_head) - head_ref(show_ref, NULL); - for_each_ref(show_ref, NULL); + head_ref(each_ref_fn_adapter, &wrapped_show_ref); + for_each_ref(each_ref_fn_adapter, &wrapped_show_ref); if (!found_match) { if (verify && !quiet) die("No match"); -- cgit v1.2.1 From f0a011fa1f3030e49de9f316b02119b390c33e92 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Mon, 25 May 2015 18:38:51 +0000 Subject: builtin/show-ref: rewrite to use object_id Signed-off-by: Michael Haggerty Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- builtin/show-ref.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'builtin/show-ref.c') diff --git a/builtin/show-ref.c b/builtin/show-ref.c index 8e25536077..18f84fbc53 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -17,16 +17,17 @@ static int deref_tags, show_head, tags_only, heads_only, found_match, verify, static const char **pattern; static const char *exclude_existing_arg; -static void show_one(const char *refname, const unsigned char *sha1) +static void show_one(const char *refname, const struct object_id *oid) { - const char *hex = find_unique_abbrev(sha1, abbrev); + const char *hex = find_unique_abbrev(oid->hash, abbrev); if (hash_only) printf("%s\n", hex); else printf("%s %s\n", hex, refname); } -static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata) +static int show_ref(const char *refname, const struct object_id *oid, + int flag, void *cbdata) { const char *hex; unsigned char peeled[20]; @@ -69,14 +70,14 @@ match: * detect and return error if the repository is corrupt and * ref points at a nonexistent object. */ - if (!has_sha1_file(sha1)) + if (!has_sha1_file(oid->hash)) die("git show-ref: bad ref %s (%s)", refname, - sha1_to_hex(sha1)); + oid_to_hex(oid)); if (quiet) return 0; - show_one(refname, sha1); + show_one(refname, oid); if (!deref_tags) return 0; @@ -88,7 +89,8 @@ match: return 0; } -static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata) +static int add_existing(const char *refname, const struct object_id *oid, + int flag, void *cbdata) { struct string_list *list = (struct string_list *)cbdata; string_list_insert(list, refname); @@ -109,10 +111,8 @@ static int exclude_existing(const char *match) static struct string_list existing_refs = STRING_LIST_INIT_DUP; char buf[1024]; int matchlen = match ? strlen(match) : 0; - struct each_ref_fn_sha1_adapter wrapped_add_existing = - {add_existing, &existing_refs}; - for_each_ref(each_ref_fn_adapter, &wrapped_add_existing); + for_each_ref(add_existing, &existing_refs); while (fgets(buf, sizeof(buf), stdin)) { char *ref; int len = strlen(buf); @@ -193,9 +193,6 @@ static const struct option show_ref_options[] = { int cmd_show_ref(int argc, const char **argv, const char *prefix) { - struct each_ref_fn_sha1_adapter wrapped_show_ref = - {show_ref, NULL}; - if (argc == 2 && !strcmp(argv[1], "-h")) usage_with_options(show_ref_usage, show_ref_options); @@ -213,12 +210,12 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix) if (!pattern) die("--verify requires a reference"); while (*pattern) { - unsigned char sha1[20]; + struct object_id oid; if (starts_with(*pattern, "refs/") && - !read_ref(*pattern, sha1)) { + !read_ref(*pattern, oid.hash)) { if (!quiet) - show_one(*pattern, sha1); + show_one(*pattern, &oid); } else if (!quiet) die("'%s' - not a valid ref", *pattern); @@ -230,8 +227,8 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix) } if (show_head) - head_ref(each_ref_fn_adapter, &wrapped_show_ref); - for_each_ref(each_ref_fn_adapter, &wrapped_show_ref); + head_ref(show_ref, NULL); + for_each_ref(show_ref, NULL); if (!found_match) { if (verify && !quiet) die("No match"); -- cgit v1.2.1 From a0cde90ebfe22d879fd9c272afdef9301ca8b51d Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Mon, 25 May 2015 18:38:52 +0000 Subject: show_ref(): convert local variable peeled to object_id Signed-off-by: Michael Haggerty Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- builtin/show-ref.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'builtin/show-ref.c') diff --git a/builtin/show-ref.c b/builtin/show-ref.c index 18f84fbc53..dfbc314ac2 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -30,7 +30,7 @@ static int show_ref(const char *refname, const struct object_id *oid, int flag, void *cbdata) { const char *hex; - unsigned char peeled[20]; + struct object_id peeled; if (show_head && !strcmp(refname, "HEAD")) goto match; @@ -82,8 +82,8 @@ match: if (!deref_tags) return 0; - if (!peel_ref(refname, peeled)) { - hex = find_unique_abbrev(peeled, abbrev); + if (!peel_ref(refname, peeled.hash)) { + hex = find_unique_abbrev(peeled.hash, abbrev); printf("%s %s^{}\n", hex, refname); } return 0; -- cgit v1.2.1