refs/files-backend: convert many internals to struct object_id

Convert many of the internals of the files backend to use struct object_id. Avoid converting public APIs (except one change to refs/ref-cache.c) to limit the scope of the changes. Convert one use of get_sha1_hex to parse_oid_hex, and rely on the fact that a strbuf will be NUL-terminated and that parse_oid_hex will fail on truncated input to avoid the need to check the length. This is a requirement to convert parse_object later on. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed May 6, 2017 at 22:10 UTC 4417df8c492489613d031478ba2e3604f65a5f84
3 files changed +60 -72
refs/files-backend.c
+57 -69
@@ -195,27 +195,15 @@ static const char PACKED_REFS_HEADER[] =
195 * Return a pointer to the refname within the line (null-terminated),
196 * or NULL if there was a problem.
197 */
198 -static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
198 +static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
199 {
200 const char *ref;
201
202 - /*
203 - * 42: the answer to everything.
204 - *
205 - * In this case, it happens to be the answer to
206 - * 40 (length of sha1 hex representation)
207 - * +1 (space in between hex and name)
208 - * +1 (newline at the end of the line)
209 - */
210 - if (line->len <= 42)
211 - return NULL;
212 -
213 - if (get_sha1_hex(line->buf, sha1) < 0)
202 + if (parse_oid_hex(line->buf, oid, &ref) < 0)
203 return NULL;
215 - if (!isspace(line->buf[40]))
204 + if (!isspace(*ref++))
205 return NULL;
206
218 - ref = line->buf + 41;
207 if (isspace(*ref))
208 return NULL;
209
@@ -260,7 +248,7 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
248 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
249
250 while (strbuf_getwholeline(&line, f, '\n') != EOF) {
263 - unsigned char sha1[20];
251 + struct object_id oid;
252 const char *refname;
253 const char *traits;
254
@@ -273,17 +261,17 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
261 continue;
262 }
263
276 - refname = parse_ref_line(&line, sha1);
264 + refname = parse_ref_line(&line, &oid);
265 if (refname) {
266 int flag = REF_ISPACKED;
267
268 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
269 if (!refname_is_safe(refname))
270 die("packed refname is dangerous: %s", refname);
283 - hashclr(sha1);
271 + oidclr(&oid);
272 flag |= REF_BAD_NAME | REF_ISBROKEN;
273 }
286 - last = create_ref_entry(refname, sha1, flag, 0);
274 + last = create_ref_entry(refname, &oid, flag, 0);
275 if (peeled == PEELED_FULLY ||
276 (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
277 last->flag |= REF_KNOWS_PEELED;
@@ -294,8 +282,8 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
282 line.buf[0] == '^' &&
283 line.len == PEELED_LINE_LENGTH &&
284 line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
297 - !get_sha1_hex(line.buf + 1, sha1)) {
298 - hashcpy(last->u.value.peeled.hash, sha1);
285 + !get_oid_hex(line.buf + 1, &oid)) {
286 + oidcpy(&last->u.value.peeled, &oid);
287 /*
288 * Regardless of what the file header said,
289 * we definitely know the value of *this*
@@ -404,14 +392,14 @@ static struct ref_dir *get_packed_refs(struct files_ref_store *refs)
392 * commit_packed_refs().
393 */
394 static void add_packed_ref(struct files_ref_store *refs,
407 - const char *refname, const unsigned char *sha1)
395 + const char *refname, const struct object_id *oid)
396 {
397 struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
398
399 if (!packed_ref_cache->lock)
400 die("internal error: packed refs not locked");
401 add_ref_entry(get_packed_ref_dir(packed_ref_cache),
414 - create_ref_entry(refname, sha1, REF_ISPACKED, 1));
402 + create_ref_entry(refname, oid, REF_ISPACKED, 1));
403 }
404
405 /*
@@ -444,7 +432,7 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
432 strbuf_add(&refname, dirname, dirnamelen);
433
434 while ((de = readdir(d)) != NULL) {
447 - unsigned char sha1[20];
435 + struct object_id oid;
436 struct stat st;
437 int flag;
438
@@ -465,10 +453,10 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
453 if (!refs_resolve_ref_unsafe(&refs->base,
454 refname.buf,
455 RESOLVE_REF_READING,
468 - sha1, &flag)) {
469 - hashclr(sha1);
456 + oid.hash, &flag)) {
457 + oidclr(&oid);
458 flag |= REF_ISBROKEN;
471 - } else if (is_null_sha1(sha1)) {
459 + } else if (is_null_oid(&oid)) {
460 /*
461 * It is so astronomically unlikely
462 * that NULL_SHA1 is the SHA-1 of an
@@ -484,11 +472,11 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
472 REFNAME_ALLOW_ONELEVEL)) {
473 if (!refname_is_safe(refname.buf))
474 die("loose refname is dangerous: %s", refname.buf);
487 - hashclr(sha1);
475 + oidclr(&oid);
476 flag |= REF_BAD_NAME | REF_ISBROKEN;
477 }
478 add_entry_to_dir(dir,
491 - create_ref_entry(refname.buf, sha1, flag, 0));
479 + create_ref_entry(refname.buf, &oid, flag, 0));
480 }
481 strbuf_setlen(&refname, dirnamelen);
482 strbuf_setlen(&path, path_baselen);
@@ -1526,7 +1514,7 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
1514 packed_entry->flag = REF_ISPACKED;
1515 oidcpy(&packed_entry->u.value.oid, iter->oid);
1516 } else {
1529 - packed_entry = create_ref_entry(iter->refname, iter->oid->hash,
1517 + packed_entry = create_ref_entry(iter->refname, iter->oid,
1518 REF_ISPACKED, 0);
1519 add_ref_entry(packed_refs, packed_entry);
1520 }
@@ -1709,10 +1697,10 @@ static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
1697 }
1698
1699 static int write_ref_to_lockfile(struct ref_lock *lock,
1712 - const unsigned char *sha1, struct strbuf *err);
1700 + const struct object_id *oid, struct strbuf *err);
1701 static int commit_ref_update(struct files_ref_store *refs,
1702 struct ref_lock *lock,
1715 - const unsigned char *sha1, const char *logmsg,
1703 + const struct object_id *oid, const char *logmsg,
1704 struct strbuf *err);
1705
1706 static int files_rename_ref(struct ref_store *ref_store,
@@ -1721,7 +1709,7 @@ static int files_rename_ref(struct ref_store *ref_store,
1709 {
1710 struct files_ref_store *refs =
1711 files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
1724 - unsigned char sha1[20], orig_sha1[20];
1712 + struct object_id oid, orig_oid;
1713 int flag = 0, logmoved = 0;
1714 struct ref_lock *lock;
1715 struct stat loginfo;
@@ -1743,7 +1731,7 @@ static int files_rename_ref(struct ref_store *ref_store,
1731
1732 if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
1733 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1746 - orig_sha1, &flag)) {
1734 + orig_oid.hash, &flag)) {
1735 ret = error("refname %s not found", oldrefname);
1736 goto out;
1737 }
@@ -1765,21 +1753,21 @@ static int files_rename_ref(struct ref_store *ref_store,
1753 }
1754
1755 if (refs_delete_ref(&refs->base, logmsg, oldrefname,
1768 - orig_sha1, REF_NODEREF)) {
1756 + orig_oid.hash, REF_NODEREF)) {
1757 error("unable to delete old %s", oldrefname);
1758 goto rollback;
1759 }
1760
1761 /*
1774 - * Since we are doing a shallow lookup, sha1 is not the
1775 - * correct value to pass to delete_ref as old_sha1. But that
1776 - * doesn't matter, because an old_sha1 check wouldn't add to
1762 + * Since we are doing a shallow lookup, oid is not the
1763 + * correct value to pass to delete_ref as old_oid. But that
1764 + * doesn't matter, because an old_oid check wouldn't add to
1765 * the safety anyway; we want to delete the reference whatever
1766 * its current value.
1767 */
1768 if (!refs_read_ref_full(&refs->base, newrefname,
1769 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1782 - sha1, NULL) &&
1770 + oid.hash, NULL) &&
1771 refs_delete_ref(&refs->base, NULL, newrefname,
1772 NULL, REF_NODEREF)) {
1773 if (errno == EISDIR) {
@@ -1812,10 +1800,10 @@ static int files_rename_ref(struct ref_store *ref_store,
1800 strbuf_release(&err);
1801 goto rollback;
1802 }
1815 - hashcpy(lock->old_oid.hash, orig_sha1);
1803 + oidcpy(&lock->old_oid, &orig_oid);
1804
1817 - if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
1818 - commit_ref_update(refs, lock, orig_sha1, logmsg, &err)) {
1805 + if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1806 + commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
1807 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1808 strbuf_release(&err);
1809 goto rollback;
@@ -1835,8 +1823,8 @@ static int files_rename_ref(struct ref_store *ref_store,
1823
1824 flag = log_all_ref_updates;
1825 log_all_ref_updates = LOG_REFS_NONE;
1838 - if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
1839 - commit_ref_update(refs, lock, orig_sha1, NULL, &err)) {
1826 + if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1827 + commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
1828 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1829 strbuf_release(&err);
1830 }
@@ -1986,8 +1974,8 @@ static int files_create_reflog(struct ref_store *ref_store,
1974 return 0;
1975 }
1976
1989 -static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
1990 - const unsigned char *new_sha1,
1977 +static int log_ref_write_fd(int fd, const struct object_id *old_oid,
1978 + const struct object_id *new_oid,
1979 const char *committer, const char *msg)
1980 {
1981 int msglen, written;
@@ -1998,8 +1986,8 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
1986 maxlen = strlen(committer) + msglen + 100;
1987 logrec = xmalloc(maxlen);
1988 len = xsnprintf(logrec, maxlen, "%s %s %s\n",
2001 - sha1_to_hex(old_sha1),
2002 - sha1_to_hex(new_sha1),
1989 + oid_to_hex(old_oid),
1990 + oid_to_hex(new_oid),
1991 committer);
1992 if (msglen)
1993 len += copy_reflog_msg(logrec + len - 1, msg) - 1;
@@ -2013,8 +2001,8 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
2001 }
2002
2003 static int files_log_ref_write(struct files_ref_store *refs,
2016 - const char *refname, const unsigned char *old_sha1,
2017 - const unsigned char *new_sha1, const char *msg,
2004 + const char *refname, const struct object_id *old_oid,
2005 + const struct object_id *new_oid, const char *msg,
2006 int flags, struct strbuf *err)
2007 {
2008 int logfd, result;
@@ -2031,7 +2019,7 @@ static int files_log_ref_write(struct files_ref_store *refs,
2019
2020 if (logfd < 0)
2021 return 0;
2034 - result = log_ref_write_fd(logfd, old_sha1, new_sha1,
2022 + result = log_ref_write_fd(logfd, old_oid, new_oid,
2023 git_committer_info(0), msg);
2024 if (result) {
2025 struct strbuf sb = STRBUF_INIT;
@@ -2063,29 +2051,29 @@ static int files_log_ref_write(struct files_ref_store *refs,
2051 * return -1.
2052 */
2053 static int write_ref_to_lockfile(struct ref_lock *lock,
2066 - const unsigned char *sha1, struct strbuf *err)
2054 + const struct object_id *oid, struct strbuf *err)
2055 {
2056 static char term = '\n';
2057 struct object *o;
2058 int fd;
2059
2072 - o = parse_object(sha1);
2060 + o = parse_object(oid->hash);
2061 if (!o) {
2062 strbuf_addf(err,
2063 "trying to write ref '%s' with nonexistent object %s",
2076 - lock->ref_name, sha1_to_hex(sha1));
2064 + lock->ref_name, oid_to_hex(oid));
2065 unlock_ref(lock);
2066 return -1;
2067 }
2068 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
2069 strbuf_addf(err,
2070 "trying to write non-commit object %s to branch '%s'",
2083 - sha1_to_hex(sha1), lock->ref_name);
2071 + oid_to_hex(oid), lock->ref_name);
2072 unlock_ref(lock);
2073 return -1;
2074 }
2075 fd = get_lock_file_fd(lock->lk);
2088 - if (write_in_full(fd, sha1_to_hex(sha1), 40) != 40 ||
2076 + if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
2077 write_in_full(fd, &term, 1) != 1 ||
2078 close_ref(lock) < 0) {
2079 strbuf_addf(err,
@@ -2103,14 +2091,14 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
2091 */
2092 static int commit_ref_update(struct files_ref_store *refs,
2093 struct ref_lock *lock,
2106 - const unsigned char *sha1, const char *logmsg,
2094 + const struct object_id *oid, const char *logmsg,
2095 struct strbuf *err)
2096 {
2097 files_assert_main_repository(refs, "commit_ref_update");
2098
2099 clear_loose_ref_cache(refs);
2100 if (files_log_ref_write(refs, lock->ref_name,
2113 - lock->old_oid.hash, sha1,
2101 + &lock->old_oid, oid,
2102 logmsg, 0, err)) {
2103 char *old_msg = strbuf_detach(err, NULL);
2104 strbuf_addf(err, "cannot update the ref '%s': %s",
@@ -2133,18 +2121,18 @@ static int commit_ref_update(struct files_ref_store *refs,
2121 * check with HEAD only which should cover 99% of all usage
2122 * scenarios (even 100% of the default ones).
2123 */
2136 - unsigned char head_sha1[20];
2124 + struct object_id head_oid;
2125 int head_flag;
2126 const char *head_ref;
2127
2128 head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
2129 RESOLVE_REF_READING,
2142 - head_sha1, &head_flag);
2130 + head_oid.hash, &head_flag);
2131 if (head_ref && (head_flag & REF_ISSYMREF) &&
2132 !strcmp(head_ref, lock->ref_name)) {
2133 struct strbuf log_err = STRBUF_INIT;
2134 if (files_log_ref_write(refs, "HEAD",
2147 - lock->old_oid.hash, sha1,
2135 + &lock->old_oid, oid,
2136 logmsg, 0, &log_err)) {
2137 error("%s", log_err.buf);
2138 strbuf_release(&log_err);
@@ -2182,12 +2170,12 @@ static void update_symref_reflog(struct files_ref_store *refs,
2170 const char *target, const char *logmsg)
2171 {
2172 struct strbuf err = STRBUF_INIT;
2185 - unsigned char new_sha1[20];
2173 + struct object_id new_oid;
2174 if (logmsg &&
2175 !refs_read_ref_full(&refs->base, target,
2188 - RESOLVE_REF_READING, new_sha1, NULL) &&
2189 - files_log_ref_write(refs, refname, lock->old_oid.hash,
2190 - new_sha1, logmsg, 0, &err)) {
2176 + RESOLVE_REF_READING, new_oid.hash, NULL) &&
2177 + files_log_ref_write(refs, refname, &lock->old_oid,
2178 + &new_oid, logmsg, 0, &err)) {
2179 error("%s", err.buf);
2180 strbuf_release(&err);
2181 }
@@ -2866,7 +2854,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
2854 * The reference already has the desired
2855 * value, so we don't need to write it.
2856 */
2869 - } else if (write_ref_to_lockfile(lock, update->new_oid.hash,
2857 + } else if (write_ref_to_lockfile(lock, &update->new_oid,
2858 err)) {
2859 char *write_err = strbuf_detach(err, NULL);
2860
@@ -3001,8 +2989,8 @@ static int files_transaction_commit(struct ref_store *ref_store,
2989 update->flags & REF_LOG_ONLY) {
2990 if (files_log_ref_write(refs,
2991 lock->ref_name,
3004 - lock->old_oid.hash,
3005 - update->new_oid.hash,
2992 + &lock->old_oid,
2993 + &update->new_oid,
2994 update->msg, update->flags,
2995 err)) {
2996 char *old_msg = strbuf_detach(err, NULL);
@@ -3174,7 +3162,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
3162 if ((update->flags & REF_HAVE_NEW) &&
3163 !is_null_oid(&update->new_oid))
3164 add_packed_ref(refs, update->refname,
3177 - update->new_oid.hash);
3165 + &update->new_oid);
3166 }
3167
3168 if (commit_packed_refs(refs)) {
refs/ref-cache.c
+2 -2
@@ -32,7 +32,7 @@ struct ref_dir *get_ref_dir(struct ref_entry *entry)
32 }
33
34 struct ref_entry *create_ref_entry(const char *refname,
35 - const unsigned char *sha1, int flag,
35 + const struct object_id *oid, int flag,
36 int check_name)
37 {
38 struct ref_entry *ref;
@@ -41,7 +41,7 @@ struct ref_entry *create_ref_entry(const char *refname,
41 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
42 die("Reference has invalid format: '%s'", refname);
43 FLEX_ALLOC_STR(ref, name, refname);
44 - hashcpy(ref->u.value.oid.hash, sha1);
44 + oidcpy(&ref->u.value.oid, oid);
45 oidclr(&ref->u.value.peeled);
46 ref->flag = flag;
47 return ref;
refs/ref-cache.h
+1 -1
@@ -185,7 +185,7 @@ struct ref_entry *create_dir_entry(struct ref_cache *cache,
185 int incomplete);
186
187 struct ref_entry *create_ref_entry(const char *refname,
188 - const unsigned char *sha1, int flag,
188 + const struct object_id *oid, int flag,
189 int check_name);
190
191 /*