From 2616a5e5089814188a583572bd9bf578b18a2a40 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:06:50 +0000 Subject: refs: convert delete_ref and refs_delete_ref to struct object_id Convert delete_ref and refs_delete_ref to take a pointer to struct object_id. Update the documentation accordingly, including referring to null_oid in lowercase, as it is not a #define constant. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index c590a992f..25170d914 100644 --- a/refs.c +++ b/refs.c @@ -620,25 +620,25 @@ done: return ret; } -static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1) +static int delete_pseudoref(const char *pseudoref, const struct object_id *old_oid) { static struct lock_file lock; const char *filename; filename = git_path("%s", pseudoref); - if (old_sha1 && !is_null_sha1(old_sha1)) { + if (old_oid && !is_null_oid(old_oid)) { int fd; - unsigned char actual_old_sha1[20]; + struct object_id actual_old_oid; fd = hold_lock_file_for_update_timeout( &lock, filename, LOCK_DIE_ON_ERROR, get_files_ref_lock_timeout_ms()); if (fd < 0) die_errno(_("Could not open '%s' for writing"), filename); - if (read_ref(pseudoref, actual_old_sha1)) + if (read_ref(pseudoref, actual_old_oid.hash)) die("could not read ref '%s'", pseudoref); - if (hashcmp(actual_old_sha1, old_sha1)) { + if (oidcmp(&actual_old_oid, old_oid)) { warning("Unexpected sha1 when deleting %s", pseudoref); rollback_lock_file(&lock); return -1; @@ -655,7 +655,7 @@ static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1 int refs_delete_ref(struct ref_store *refs, const char *msg, const char *refname, - const unsigned char *old_sha1, + const struct object_id *old_oid, unsigned int flags) { struct ref_transaction *transaction; @@ -663,12 +663,13 @@ int refs_delete_ref(struct ref_store *refs, const char *msg, if (ref_type(refname) == REF_TYPE_PSEUDOREF) { assert(refs == get_main_ref_store()); - return delete_pseudoref(refname, old_sha1); + return delete_pseudoref(refname, old_oid); } transaction = ref_store_transaction_begin(refs, &err); if (!transaction || - ref_transaction_delete(transaction, refname, old_sha1, + ref_transaction_delete(transaction, refname, + old_oid ? old_oid->hash : NULL, flags, msg, &err) || ref_transaction_commit(transaction, &err)) { error("%s", err.buf); @@ -682,10 +683,10 @@ int refs_delete_ref(struct ref_store *refs, const char *msg, } int delete_ref(const char *msg, const char *refname, - const unsigned char *old_sha1, unsigned int flags) + const struct object_id *old_oid, unsigned int flags) { return refs_delete_ref(get_main_ref_store(), msg, refname, - old_sha1, flags); + old_oid, flags); } int copy_reflog_msg(char *buf, const char *msg) -- cgit v1.2.1 From ae077771b09fac4d663e3f8c039318a97eb3a15b Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:06:51 +0000 Subject: refs: convert update_ref and refs_update_ref to use struct object_id Convert update_ref, refs_update_ref, and write_pseudoref to use struct object_id. Update the existing callers as well. Remove update_ref_oid, as it is no longer needed. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 25170d914..edd20044c 100644 --- a/refs.c +++ b/refs.c @@ -574,8 +574,8 @@ long get_files_ref_lock_timeout_ms(void) return timeout_ms; } -static int write_pseudoref(const char *pseudoref, const unsigned char *sha1, - const unsigned char *old_sha1, struct strbuf *err) +static int write_pseudoref(const char *pseudoref, const struct object_id *oid, + const struct object_id *old_oid, struct strbuf *err) { const char *filename; int fd; @@ -583,7 +583,7 @@ static int write_pseudoref(const char *pseudoref, const unsigned char *sha1, struct strbuf buf = STRBUF_INIT; int ret = -1; - strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1)); + strbuf_addf(&buf, "%s\n", oid_to_hex(oid)); filename = git_path("%s", pseudoref); fd = hold_lock_file_for_update_timeout(&lock, filename, @@ -595,12 +595,12 @@ static int write_pseudoref(const char *pseudoref, const unsigned char *sha1, goto done; } - if (old_sha1) { - unsigned char actual_old_sha1[20]; + if (old_oid) { + struct object_id actual_old_oid; - if (read_ref(pseudoref, actual_old_sha1)) + if (read_ref(pseudoref, actual_old_oid.hash)) die("could not read ref '%s'", pseudoref); - if (hashcmp(actual_old_sha1, old_sha1)) { + if (oidcmp(&actual_old_oid, old_oid)) { strbuf_addf(err, "unexpected sha1 when writing '%s'", pseudoref); rollback_lock_file(&lock); goto done; @@ -985,17 +985,9 @@ int ref_transaction_verify(struct ref_transaction *transaction, flags, NULL, err); } -int update_ref_oid(const char *msg, const char *refname, - const struct object_id *new_oid, const struct object_id *old_oid, - unsigned int flags, enum action_on_err onerr) -{ - return update_ref(msg, refname, new_oid ? new_oid->hash : NULL, - old_oid ? old_oid->hash : NULL, flags, onerr); -} - int refs_update_ref(struct ref_store *refs, const char *msg, - const char *refname, const unsigned char *new_sha1, - const unsigned char *old_sha1, unsigned int flags, + const char *refname, const struct object_id *new_oid, + const struct object_id *old_oid, unsigned int flags, enum action_on_err onerr) { struct ref_transaction *t = NULL; @@ -1004,11 +996,12 @@ int refs_update_ref(struct ref_store *refs, const char *msg, if (ref_type(refname) == REF_TYPE_PSEUDOREF) { assert(refs == get_main_ref_store()); - ret = write_pseudoref(refname, new_sha1, old_sha1, &err); + ret = write_pseudoref(refname, new_oid, old_oid, &err); } else { t = ref_store_transaction_begin(refs, &err); if (!t || - ref_transaction_update(t, refname, new_sha1, old_sha1, + ref_transaction_update(t, refname, new_oid ? new_oid->hash : NULL, + old_oid ? old_oid->hash : NULL, flags, msg, &err) || ref_transaction_commit(t, &err)) { ret = 1; @@ -1038,12 +1031,12 @@ int refs_update_ref(struct ref_store *refs, const char *msg, } int update_ref(const char *msg, const char *refname, - const unsigned char *new_sha1, - const unsigned char *old_sha1, + const struct object_id *new_oid, + const struct object_id *old_oid, unsigned int flags, enum action_on_err onerr) { - return refs_update_ref(get_main_ref_store(), msg, refname, new_sha1, - old_sha1, flags, onerr); + return refs_update_ref(get_main_ref_store(), msg, refname, new_oid, + old_oid, flags, onerr); } char *shorten_unambiguous_ref(const char *refname, int strict) -- cgit v1.2.1 From 6ee18216d8d9ca8f76b01282b36bdf590a64d8fc Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:06:52 +0000 Subject: refs: prevent accidental NULL dereference in write_pseudoref Several of the refs functions take NULL to indicate that the ref is not to be updated. If refs_update_ref were called with a NULL new object ID, we could pass that NULL pointer to write_pseudoref, which would then segfault when it dereferenced it. Instead, simply return successfully, since if we don't want to update the pseudoref, there's nothing to do. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'refs.c') diff --git a/refs.c b/refs.c index edd20044c..91c2af78b 100644 --- a/refs.c +++ b/refs.c @@ -583,6 +583,9 @@ static int write_pseudoref(const char *pseudoref, const struct object_id *oid, struct strbuf buf = STRBUF_INIT; int ret = -1; + if (!oid) + return 0; + strbuf_addf(&buf, "%s\n", oid_to_hex(oid)); filename = git_path("%s", pseudoref); -- cgit v1.2.1 From 89f3bbdd3b1f46a5747aa5618b7742f7b3f2adef Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:06:53 +0000 Subject: refs: update ref transactions to use struct object_id Update the ref transaction code to use struct object_id. Remove one NULL pointer check which was previously inserted around a dereference; since we now pass a pointer to struct object_id directly through, the code we're calling handles this for us. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 91c2af78b..db4988ab3 100644 --- a/refs.c +++ b/refs.c @@ -671,8 +671,7 @@ int refs_delete_ref(struct ref_store *refs, const char *msg, transaction = ref_store_transaction_begin(refs, &err); if (!transaction || - ref_transaction_delete(transaction, refname, - old_oid ? old_oid->hash : NULL, + ref_transaction_delete(transaction, refname, old_oid, flags, msg, &err) || ref_transaction_commit(transaction, &err)) { error("%s", err.buf); @@ -898,8 +897,8 @@ void ref_transaction_free(struct ref_transaction *transaction) struct ref_update *ref_transaction_add_update( struct ref_transaction *transaction, const char *refname, unsigned int flags, - const unsigned char *new_sha1, - const unsigned char *old_sha1, + const struct object_id *new_oid, + const struct object_id *old_oid, const char *msg) { struct ref_update *update; @@ -917,23 +916,23 @@ struct ref_update *ref_transaction_add_update( update->flags = flags; if (flags & REF_HAVE_NEW) - hashcpy(update->new_oid.hash, new_sha1); + oidcpy(&update->new_oid, new_oid); if (flags & REF_HAVE_OLD) - hashcpy(update->old_oid.hash, old_sha1); + oidcpy(&update->old_oid, old_oid); update->msg = xstrdup_or_null(msg); return update; } int ref_transaction_update(struct ref_transaction *transaction, const char *refname, - const unsigned char *new_sha1, - const unsigned char *old_sha1, + const struct object_id *new_oid, + const struct object_id *old_oid, unsigned int flags, const char *msg, struct strbuf *err) { assert(err); - if ((new_sha1 && !is_null_sha1(new_sha1)) ? + if ((new_oid && !is_null_oid(new_oid)) ? check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) : !refname_is_safe(refname)) { strbuf_addf(err, "refusing to update ref with bad name '%s'", @@ -943,48 +942,48 @@ int ref_transaction_update(struct ref_transaction *transaction, flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS; - flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0); + flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0); ref_transaction_add_update(transaction, refname, flags, - new_sha1, old_sha1, msg); + new_oid, old_oid, msg); return 0; } int ref_transaction_create(struct ref_transaction *transaction, const char *refname, - const unsigned char *new_sha1, + const struct object_id *new_oid, unsigned int flags, const char *msg, struct strbuf *err) { - if (!new_sha1 || is_null_sha1(new_sha1)) - die("BUG: create called without valid new_sha1"); - return ref_transaction_update(transaction, refname, new_sha1, - null_sha1, flags, msg, err); + if (!new_oid || is_null_oid(new_oid)) + die("BUG: create called without valid new_oid"); + return ref_transaction_update(transaction, refname, new_oid, + &null_oid, flags, msg, err); } int ref_transaction_delete(struct ref_transaction *transaction, const char *refname, - const unsigned char *old_sha1, + const struct object_id *old_oid, unsigned int flags, const char *msg, struct strbuf *err) { - if (old_sha1 && is_null_sha1(old_sha1)) - die("BUG: delete called with old_sha1 set to zeros"); + if (old_oid && is_null_oid(old_oid)) + die("BUG: delete called with old_oid set to zeros"); return ref_transaction_update(transaction, refname, - null_sha1, old_sha1, + &null_oid, old_oid, flags, msg, err); } int ref_transaction_verify(struct ref_transaction *transaction, const char *refname, - const unsigned char *old_sha1, + const struct object_id *old_oid, unsigned int flags, struct strbuf *err) { - if (!old_sha1) - die("BUG: verify called with old_sha1 set to NULL"); + if (!old_oid) + die("BUG: verify called with old_oid set to NULL"); return ref_transaction_update(transaction, refname, - NULL, old_sha1, + NULL, old_oid, flags, NULL, err); } @@ -1003,8 +1002,7 @@ int refs_update_ref(struct ref_store *refs, const char *msg, } else { t = ref_store_transaction_begin(refs, &err); if (!t || - ref_transaction_update(t, refname, new_oid ? new_oid->hash : NULL, - old_oid ? old_oid->hash : NULL, + ref_transaction_update(t, refname, new_oid, old_oid, flags, msg, &err) || ref_transaction_commit(t, &err)) { ret = 1; -- cgit v1.2.1 From 0f2dc722dd01097d1e1c1dac43b2f57924594457 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:06:55 +0000 Subject: refs: convert resolve_refdup and refs_resolve_refdup to struct object_id All of the callers already pass the hash member of struct object_id, so update them to pass a pointer to the struct directly, This transformation was done with an update to declaration and definition and the following semantic patch: @@ expression E1, E2, E3, E4; @@ - resolve_refdup(E1, E2, E3.hash, E4) + resolve_refdup(E1, E2, &E3, E4) @@ expression E1, E2, E3, E4; @@ - resolve_refdup(E1, E2, E3->hash, E4) + resolve_refdup(E1, E2, E3, E4) Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index db4988ab3..3bca4c273 100644 --- a/refs.c +++ b/refs.c @@ -194,21 +194,21 @@ int ref_resolves_to_object(const char *refname, char *refs_resolve_refdup(struct ref_store *refs, const char *refname, int resolve_flags, - unsigned char *sha1, int *flags) + struct object_id *oid, int *flags) { const char *result; result = refs_resolve_ref_unsafe(refs, refname, resolve_flags, - sha1, flags); + oid->hash, flags); return xstrdup_or_null(result); } char *resolve_refdup(const char *refname, int resolve_flags, - unsigned char *sha1, int *flags) + struct object_id *oid, int *flags) { return refs_resolve_refdup(get_main_ref_store(), refname, resolve_flags, - sha1, flags); + oid, flags); } /* The argument to filter_refs */ -- cgit v1.2.1 From 34c290a6fc8b1f6705d2646d726df2260927da0f Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:06:56 +0000 Subject: refs: convert read_ref and read_ref_full to object_id All but two of the call sites already have parameters using the hash parameter of struct object_id, so convert them to take a pointer to the struct directly. Also convert refs_read_refs_full, the underlying implementation. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 3bca4c273..9c1f88132 100644 --- a/refs.c +++ b/refs.c @@ -219,22 +219,22 @@ struct ref_filter { }; int refs_read_ref_full(struct ref_store *refs, const char *refname, - int resolve_flags, unsigned char *sha1, int *flags) + int resolve_flags, struct object_id *oid, int *flags) { - if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, sha1, flags)) + if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, oid->hash, flags)) return 0; return -1; } -int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags) +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(), refname, - resolve_flags, sha1, flags); + resolve_flags, oid, flags); } -int read_ref(const char *refname, unsigned char *sha1) +int read_ref(const char *refname, struct object_id *oid) { - return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL); + return read_ref_full(refname, RESOLVE_REF_READING, oid, NULL); } int ref_exists(const char *refname) @@ -362,7 +362,7 @@ int head_ref_namespaced(each_ref_fn fn, void *cb_data) int flag; strbuf_addf(&buf, "%sHEAD", get_git_namespace()); - if (!read_ref_full(buf.buf, RESOLVE_REF_READING, oid.hash, &flag)) + if (!read_ref_full(buf.buf, RESOLVE_REF_READING, &oid, &flag)) ret = fn(buf.buf, &oid, flag, cb_data); strbuf_release(&buf); @@ -601,7 +601,7 @@ static int write_pseudoref(const char *pseudoref, const struct object_id *oid, if (old_oid) { struct object_id actual_old_oid; - if (read_ref(pseudoref, actual_old_oid.hash)) + if (read_ref(pseudoref, &actual_old_oid)) die("could not read ref '%s'", pseudoref); if (oidcmp(&actual_old_oid, old_oid)) { strbuf_addf(err, "unexpected sha1 when writing '%s'", pseudoref); @@ -639,7 +639,7 @@ static int delete_pseudoref(const char *pseudoref, const struct object_id *old_o get_files_ref_lock_timeout_ms()); if (fd < 0) die_errno(_("Could not open '%s' for writing"), filename); - if (read_ref(pseudoref, actual_old_oid.hash)) + if (read_ref(pseudoref, &actual_old_oid)) die("could not read ref '%s'", pseudoref); if (oidcmp(&actual_old_oid, old_oid)) { warning("Unexpected sha1 when deleting %s", pseudoref); @@ -1249,7 +1249,7 @@ int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) int flag; if (!refs_read_ref_full(refs, "HEAD", RESOLVE_REF_READING, - oid.hash, &flag)) + &oid, &flag)) return fn("HEAD", &oid, flag, cb_data); return 0; @@ -1699,7 +1699,7 @@ int refs_peel_ref(struct ref_store *refs, const char *refname, unsigned char *sha1) { int flag; - unsigned char base[20]; + struct object_id base; if (current_ref_iter && current_ref_iter->refname == refname) { struct object_id peeled; @@ -1711,10 +1711,10 @@ int refs_peel_ref(struct ref_store *refs, const char *refname, } if (refs_read_ref_full(refs, refname, - RESOLVE_REF_READING, base, &flag)) + RESOLVE_REF_READING, &base, &flag)) return -1; - return peel_object(base, sha1); + return peel_object(base.hash, sha1); } int peel_ref(const char *refname, unsigned char *sha1) -- cgit v1.2.1 From cca5fa6406046c19ab5a8117fe71bf4402c00d88 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:06:57 +0000 Subject: 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 --- refs.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 9c1f88132..9bb555e7f 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); -- cgit v1.2.1 From 334dc52f49ee3e56a32142d3500fe93ef79aac67 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:06:59 +0000 Subject: refs: convert dwim_log to struct object_id Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 9bb555e7f..ecb43a113 100644 --- a/refs.c +++ b/refs.c @@ -497,7 +497,7 @@ int expand_ref(const char *str, int len, struct object_id *oid, char **ref) return refs_found; } -int dwim_log(const char *str, int len, unsigned char *sha1, char **log) +int dwim_log(const char *str, int len, struct object_id *oid, char **log) { char *last_branch = substitute_branch_name(&str, &len); const char **p; @@ -506,13 +506,13 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log) *log = NULL; for (p = ref_rev_parse_rules; *p; p++) { - unsigned char hash[20]; + struct object_id hash; const char *ref, *it; strbuf_reset(&path); strbuf_addf(&path, *p, len, str); ref = resolve_ref_unsafe(path.buf, RESOLVE_REF_READING, - hash, NULL); + hash.hash, NULL); if (!ref) continue; if (reflog_exists(path.buf)) @@ -523,7 +523,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log) continue; if (!logs_found++) { *log = xstrdup(it); - hashcpy(sha1, hash); + oidcpy(oid, &hash); } if (!warn_ambiguous_refs) break; -- cgit v1.2.1 From b420d90980a31246836680b68ca15e0239a8b696 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:07:02 +0000 Subject: refs: convert peel_ref to struct object_id Convert peel_ref (and its corresponding backend) to struct object_id. This transformation was done with an update to the declaration, definition, comments, and test helper and the following semantic patch: @@ expression E1, E2; @@ - peel_ref(E1, E2.hash) + peel_ref(E1, &E2) @@ expression E1, E2; @@ - peel_ref(E1, E2->hash) + peel_ref(E1, E2) Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index ecb43a113..f8a2d9866 100644 --- a/refs.c +++ b/refs.c @@ -1697,7 +1697,7 @@ int refs_pack_refs(struct ref_store *refs, unsigned int flags) } int refs_peel_ref(struct ref_store *refs, const char *refname, - unsigned char *sha1) + struct object_id *oid) { int flag; struct object_id base; @@ -1707,7 +1707,7 @@ int refs_peel_ref(struct ref_store *refs, const char *refname, if (ref_iterator_peel(current_ref_iter, &peeled)) return -1; - hashcpy(sha1, peeled.hash); + oidcpy(oid, &peeled); return 0; } @@ -1715,12 +1715,12 @@ int refs_peel_ref(struct ref_store *refs, const char *refname, RESOLVE_REF_READING, &base, &flag)) return -1; - return peel_object(base.hash, sha1); + return peel_object(base.hash, oid->hash); } -int peel_ref(const char *refname, unsigned char *sha1) +int peel_ref(const char *refname, struct object_id *oid) { - return refs_peel_ref(get_main_ref_store(), refname, sha1); + return refs_peel_ref(get_main_ref_store(), refname, oid); } int refs_create_symref(struct ref_store *refs, -- cgit v1.2.1 From 8eb36d9422a04a30ecc54fd69b4f836eafd10637 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:07:03 +0000 Subject: refs: convert read_ref_at to struct object_id Convert the callers and internals, including struct read_ref_at_cb, of read_ref_at to use struct object_id. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index f8a2d9866..d19fae507 100644 --- a/refs.c +++ b/refs.c @@ -738,11 +738,11 @@ struct read_ref_at_cb { timestamp_t at_time; int cnt; int reccnt; - unsigned char *sha1; + struct object_id *oid; int found_it; - unsigned char osha1[20]; - unsigned char nsha1[20]; + struct object_id ooid; + struct object_id noid; int tz; timestamp_t date; char **msg; @@ -774,25 +774,25 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid, * we have not yet updated cb->[n|o]sha1 so they still * hold the values for the previous record. */ - if (!is_null_sha1(cb->osha1)) { - hashcpy(cb->sha1, noid->hash); - if (hashcmp(cb->osha1, noid->hash)) + if (!is_null_oid(&cb->ooid)) { + oidcpy(cb->oid, noid); + if (oidcmp(&cb->ooid, noid)) warning("Log for ref %s has gap after %s.", cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); } else if (cb->date == cb->at_time) - hashcpy(cb->sha1, noid->hash); - else if (hashcmp(noid->hash, cb->sha1)) + oidcpy(cb->oid, noid); + else if (oidcmp(noid, cb->oid)) warning("Log for ref %s unexpectedly ended on %s.", cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); - hashcpy(cb->osha1, ooid->hash); - hashcpy(cb->nsha1, noid->hash); + oidcpy(&cb->ooid, ooid); + oidcpy(&cb->noid, noid); cb->found_it = 1; return 1; } - hashcpy(cb->osha1, ooid->hash); - hashcpy(cb->nsha1, noid->hash); + oidcpy(&cb->ooid, ooid); + oidcpy(&cb->noid, noid); if (cb->cnt > 0) cb->cnt--; return 0; @@ -812,15 +812,15 @@ static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid *cb->cutoff_tz = tz; if (cb->cutoff_cnt) *cb->cutoff_cnt = cb->reccnt; - hashcpy(cb->sha1, ooid->hash); - if (is_null_sha1(cb->sha1)) - hashcpy(cb->sha1, noid->hash); + oidcpy(cb->oid, ooid); + if (is_null_oid(cb->oid)) + oidcpy(cb->oid, noid); /* We just want the first entry */ return 1; } int read_ref_at(const char *refname, unsigned int flags, timestamp_t at_time, int cnt, - unsigned char *sha1, char **msg, + struct object_id *oid, char **msg, timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt) { struct read_ref_at_cb cb; @@ -833,7 +833,7 @@ int read_ref_at(const char *refname, unsigned int flags, timestamp_t at_time, in cb.cutoff_time = cutoff_time; cb.cutoff_tz = cutoff_tz; cb.cutoff_cnt = cutoff_cnt; - cb.sha1 = sha1; + cb.oid = oid; for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb); -- cgit v1.2.1 From 0155f710b856970dc1dc5261e740f135c67a7b1d Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:07:04 +0000 Subject: refs: convert reflog_expire parameter to struct object_id reflog_expire already used struct object_id internally, but it did not take it as a parameter. Adjust the parameter (and the callers) to pass a pointer to struct object_id instead of a pointer to unsigned char. Remove the temporary inserted earlier as it is no longer required. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index d19fae507..37485283c 100644 --- a/refs.c +++ b/refs.c @@ -2010,19 +2010,19 @@ int delete_reflog(const char *refname) } int refs_reflog_expire(struct ref_store *refs, - const char *refname, const unsigned char *sha1, + const char *refname, const struct object_id *oid, unsigned int flags, reflog_expiry_prepare_fn prepare_fn, reflog_expiry_should_prune_fn should_prune_fn, reflog_expiry_cleanup_fn cleanup_fn, void *policy_cb_data) { - return refs->be->reflog_expire(refs, refname, sha1, flags, + return refs->be->reflog_expire(refs, refname, oid, flags, prepare_fn, should_prune_fn, cleanup_fn, policy_cb_data); } -int reflog_expire(const char *refname, const unsigned char *sha1, +int reflog_expire(const char *refname, const struct object_id *oid, unsigned int flags, reflog_expiry_prepare_fn prepare_fn, reflog_expiry_should_prune_fn should_prune_fn, @@ -2030,7 +2030,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1, void *policy_cb_data) { return refs_reflog_expire(get_main_ref_store(), - refname, sha1, flags, + refname, oid, flags, prepare_fn, should_prune_fn, cleanup_fn, policy_cb_data); } -- cgit v1.2.1 From a98e6101f01b6991e780fc0b75f2937615fa84a9 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:07:07 +0000 Subject: refs: convert resolve_gitlink_ref to struct object_id Convert the declaration and definition of resolve_gitlink_ref to use struct object_id and apply the following semantic patch: @@ expression E1, E2, E3; @@ - resolve_gitlink_ref(E1, E2, E3.hash) + resolve_gitlink_ref(E1, E2, &E3) @@ expression E1, E2, E3; @@ - resolve_gitlink_ref(E1, E2, E3->hash) + resolve_gitlink_ref(E1, E2, E3) Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 37485283c..90219d6e1 100644 --- a/refs.c +++ b/refs.c @@ -1498,7 +1498,7 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags, } int resolve_gitlink_ref(const char *submodule, const char *refname, - unsigned char *sha1) + struct object_id *oid) { struct ref_store *refs; int flags; @@ -1508,8 +1508,8 @@ int resolve_gitlink_ref(const char *submodule, const char *refname, if (!refs) return -1; - if (!refs_resolve_ref_unsafe(refs, refname, 0, sha1, &flags) || - is_null_sha1(sha1)) + if (!refs_resolve_ref_unsafe(refs, refname, 0, oid->hash, &flags) || + is_null_oid(oid)) return -1; return 0; } -- cgit v1.2.1 From 49e61479be913f67e66bb3fdf8de9475c41b58bd Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:07:09 +0000 Subject: refs: convert resolve_ref_unsafe to struct object_id Convert resolve_ref_unsafe to take a pointer to struct object_id by converting one remaining caller to use struct object_id, removing the temporary NULL pointer check in expand_ref, converting the declaration and definition, and applying the following semantic patch: @@ expression E1, E2, E3, E4; @@ - resolve_ref_unsafe(E1, E2, E3.hash, E4) + resolve_ref_unsafe(E1, E2, &E3, E4) @@ expression E1, E2, E3, E4; @@ - resolve_ref_unsafe(E1, E2, E3->hash, E4) + resolve_ref_unsafe(E1, E2, E3, E4) Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 90219d6e1..72c45a513 100644 --- a/refs.c +++ b/refs.c @@ -199,7 +199,7 @@ char *refs_resolve_refdup(struct ref_store *refs, const char *result; result = refs_resolve_ref_unsafe(refs, refname, resolve_flags, - oid->hash, flags); + oid, flags); return xstrdup_or_null(result); } @@ -221,7 +221,7 @@ struct ref_filter { 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->hash, flags)) + if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, oid, flags)) return 0; return -1; } @@ -480,8 +480,7 @@ int expand_ref(const char *str, int len, struct object_id *oid, char **ref) strbuf_reset(&fullref); strbuf_addf(&fullref, *p, len, str); r = resolve_ref_unsafe(fullref.buf, RESOLVE_REF_READING, - this_result ? this_result->hash : NULL, - &flag); + this_result, &flag); if (r) { if (!refs_found++) *ref = xstrdup(r); @@ -512,7 +511,7 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **log) strbuf_reset(&path); strbuf_addf(&path, *p, len, str); ref = resolve_ref_unsafe(path.buf, RESOLVE_REF_READING, - hash.hash, NULL); + &hash, NULL); if (!ref) continue; if (reflog_exists(path.buf)) @@ -1393,15 +1392,15 @@ int refs_read_raw_ref(struct ref_store *ref_store, const char *refs_resolve_ref_unsafe(struct ref_store *refs, const char *refname, int resolve_flags, - unsigned char *sha1, int *flags) + struct object_id *oid, 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 (!oid) + oid = &unused_oid; if (!flags) flags = &unused_flags; @@ -1429,7 +1428,7 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, unsigned int read_flags = 0; if (refs_read_raw_ref(refs, refname, - sha1, &sb_refname, &read_flags)) { + oid->hash, &sb_refname, &read_flags)) { *flags |= read_flags; /* In reading mode, refs must eventually resolve */ @@ -1446,7 +1445,7 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, errno != ENOTDIR) return NULL; - hashclr(sha1); + oidclr(oid); if (*flags & REF_BAD_NAME) *flags |= REF_ISBROKEN; return refname; @@ -1456,7 +1455,7 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, if (!(read_flags & REF_ISSYMREF)) { if (*flags & REF_BAD_NAME) { - hashclr(sha1); + oidclr(oid); *flags |= REF_ISBROKEN; } return refname; @@ -1464,7 +1463,7 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, refname = sb_refname.buf; if (resolve_flags & RESOLVE_REF_NO_RECURSE) { - hashclr(sha1); + oidclr(oid); return refname; } if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { @@ -1491,10 +1490,10 @@ int refs_init_db(struct strbuf *err) } const char *resolve_ref_unsafe(const char *refname, int resolve_flags, - unsigned char *sha1, int *flags) + struct object_id *oid, int *flags) { return refs_resolve_ref_unsafe(get_main_ref_store(), refname, - resolve_flags, sha1, flags); + resolve_flags, oid, flags); } int resolve_gitlink_ref(const char *submodule, const char *refname, @@ -1508,7 +1507,7 @@ int resolve_gitlink_ref(const char *submodule, const char *refname, if (!refs) return -1; - if (!refs_resolve_ref_unsafe(refs, refname, 0, oid->hash, &flags) || + if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags) || is_null_oid(oid)) return -1; return 0; -- cgit v1.2.1 From ac2ed0d7d51e5c0b402797ecf01f38e9bfc82e0e Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:07:10 +0000 Subject: refs: convert peel_object to struct object_id Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 72c45a513..6546346b6 100644 --- a/refs.c +++ b/refs.c @@ -252,12 +252,12 @@ static int filter_refs(const char *refname, const struct object_id *oid, return filter->fn(refname, oid, flags, filter->cb_data); } -enum peel_status peel_object(const unsigned char *name, unsigned char *sha1) +enum peel_status peel_object(const struct object_id *name, struct object_id *oid) { - struct object *o = lookup_unknown_object(name); + struct object *o = lookup_unknown_object(name->hash); if (o->type == OBJ_NONE) { - int type = sha1_object_info(name, NULL); + int type = sha1_object_info(name->hash, NULL); if (type < 0 || !object_as_type(o, type, 0)) return PEEL_INVALID; } @@ -269,7 +269,7 @@ enum peel_status peel_object(const unsigned char *name, unsigned char *sha1) if (!o) return PEEL_INVALID; - hashcpy(sha1, o->oid.hash); + oidcpy(oid, &o->oid); return PEEL_PEELED; } @@ -1714,7 +1714,7 @@ int refs_peel_ref(struct ref_store *refs, const char *refname, RESOLVE_REF_READING, &base, &flag)) return -1; - return peel_object(base.hash, oid->hash); + return peel_object(&base, oid); } int peel_ref(const char *refname, struct object_id *oid) -- cgit v1.2.1 From 99afe91a6c3a37b7bb32c824f19b1b51712fe6f3 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 15 Oct 2017 22:07:11 +0000 Subject: refs: convert read_raw_ref backends to struct object_id Convert the unsigned char * parameter to struct object_id * for files_read_raw_ref and packed_read_raw_ref. Update the documentation. Switch from using get_sha1_hex and a hard-coded 40 to using parse_oid_hex. Signed-off-by: brian m. carlson Signed-off-by: Junio C Hamano --- refs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 6546346b6..62a762102 100644 --- a/refs.c +++ b/refs.c @@ -1382,10 +1382,10 @@ int for_each_rawref(each_ref_fn fn, void *cb_data) } int refs_read_raw_ref(struct ref_store *ref_store, - const char *refname, unsigned char *sha1, + const char *refname, struct object_id *oid, struct strbuf *referent, unsigned int *type) { - return ref_store->be->read_raw_ref(ref_store, refname, sha1, referent, type); + return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, type); } /* This function needs to return a meaningful errno on failure */ @@ -1428,7 +1428,7 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, unsigned int read_flags = 0; if (refs_read_raw_ref(refs, refname, - oid->hash, &sb_refname, &read_flags)) { + oid, &sb_refname, &read_flags)) { *flags |= read_flags; /* In reading mode, refs must eventually resolve */ @@ -1879,7 +1879,7 @@ 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.hash, &referent, &type)) { + if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent, &type)) { strbuf_addf(err, "'%s' exists; cannot create '%s'", dirname.buf, refname); goto cleanup; -- cgit v1.2.1