sha1-file: modernize loose object file functions

The loose object access code in sha1-file.c is some of the oldest in Git, and could use some modernizing. It mostly uses "unsigned char *" for object ids, which these days should be "struct object_id". It also uses the term "sha1_file" in many functions, which is confusing. The term "loose_objects" is much better. It clearly distinguishes them from packed objects (which didn't even exist back when the name "sha1_file" came into being). And it also distinguishes it from the checksummed-file concept in csum-file.c (which until recently was actually called "struct sha1file"!). This patch converts the functions {open,close,map,stat}_sha1_file() into open_loose_object(), etc, and switches their sha1 arguments for object_id structs. Similarly, path functions like fill_sha1_path() become fill_loose_path() and use object_ids. The function sha1_loose_object_info() already says "loose", so we can just drop the "sha1" (and teach it to use object_id). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 7, 2019 at 03:35 UTC 514c5fdd03b914c72a91bb420e46bdc8886940cf
5 files changed +51 -48
http-walker.c
+1 -1
@@ -547,7 +547,7 @@ static int fetch_object(struct walker *walker, unsigned char *sha1)
547 ret = error("File %s has bad hash", hex);
548 } else if (req->rename < 0) {
549 struct strbuf buf = STRBUF_INIT;
550 - loose_object_path(the_repository, &buf, req->oid.hash);
550 + loose_object_path(the_repository, &buf, &req->oid);
551 ret = error("unable to write sha1 filename %s", buf.buf);
552 strbuf_release(&buf);
553 }
http.c
+2 -2
@@ -2353,7 +2353,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
2353 oidcpy(&freq->oid, oid);
2354 freq->localfile = -1;
2355
2356 - loose_object_path(the_repository, &filename, oid->hash);
2356 + loose_object_path(the_repository, &filename, oid);
2357 strbuf_addf(&freq->tmpfile, "%s.temp", filename.buf);
2358
2359 strbuf_addf(&prevfile, "%s.prev", filename.buf);
@@ -2504,7 +2504,7 @@ int finish_http_object_request(struct http_object_request *freq)
2504 unlink_or_warn(freq->tmpfile.buf);
2505 return -1;
2506 }
2507 - loose_object_path(the_repository, &filename, freq->oid.hash);
2507 + loose_object_path(the_repository, &filename, &freq->oid);
2508 freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
2509 strbuf_release(&filename);
2510
object-store.h
+5 -3
@@ -154,11 +154,13 @@ void raw_object_store_clear(struct raw_object_store *o);
154
155 /*
156 * Put in `buf` the name of the file in the local object database that
157 - * would be used to store a loose object with the specified sha1.
157 + * would be used to store a loose object with the specified oid.
158 */
159 -const char *loose_object_path(struct repository *r, struct strbuf *buf, const unsigned char *sha1);
159 +const char *loose_object_path(struct repository *r, struct strbuf *buf,
160 + const struct object_id *oid);
161
161 -void *map_sha1_file(struct repository *r, const unsigned char *sha1, unsigned long *size);
162 +void *map_loose_object(struct repository *r, const struct object_id *oid,
163 + unsigned long *size);
164
165 extern void *read_object_file_extended(const struct object_id *oid,
166 enum object_type *type,
sha1-file.c
+41 -40
@@ -333,12 +333,12 @@ out:
333 return ret;
334 }
335
336 -static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
336 +static void fill_loose_path(struct strbuf *buf, const struct object_id *oid)
337 {
338 int i;
339 for (i = 0; i < the_hash_algo->rawsz; i++) {
340 static char hex[] = "0123456789abcdef";
341 - unsigned int val = sha1[i];
341 + unsigned int val = oid->hash[i];
342 strbuf_addch(buf, hex[val >> 4]);
343 strbuf_addch(buf, hex[val & 0xf]);
344 if (!i)
@@ -348,19 +348,19 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
348
349 static const char *odb_loose_path(struct object_directory *odb,
350 struct strbuf *buf,
351 - const unsigned char *sha1)
351 + const struct object_id *oid)
352 {
353 strbuf_reset(buf);
354 strbuf_addstr(buf, odb->path);
355 strbuf_addch(buf, '/');
356 - fill_sha1_path(buf, sha1);
356 + fill_loose_path(buf, oid);
357 return buf->buf;
358 }
359
360 const char *loose_object_path(struct repository *r, struct strbuf *buf,
361 - const unsigned char *sha1)
361 + const struct object_id *oid)
362 {
363 - return odb_loose_path(r->objects->odb, buf, sha1);
363 + return odb_loose_path(r->objects->odb, buf, oid);
364 }
365
366 /*
@@ -721,7 +721,7 @@ static int check_and_freshen_odb(struct object_directory *odb,
721 int freshen)
722 {
723 static struct strbuf path = STRBUF_INIT;
724 - odb_loose_path(odb, &path, oid->hash);
724 + odb_loose_path(odb, &path, oid);
725 return check_and_freshen_file(path.buf, freshen);
726 }
727
@@ -872,22 +872,22 @@ int git_open_cloexec(const char *name, int flags)
872 }
873
874 /*
875 - * Find "sha1" as a loose object in the local repository or in an alternate.
875 + * Find "oid" as a loose object in the local repository or in an alternate.
876 * Returns 0 on success, negative on failure.
877 *
878 * The "path" out-parameter will give the path of the object we found (if any).
879 * Note that it may point to static storage and is only valid until another
880 - * call to stat_sha1_file().
880 + * call to stat_loose_object().
881 */
882 -static int stat_sha1_file(struct repository *r, const unsigned char *sha1,
883 - struct stat *st, const char **path)
882 +static int stat_loose_object(struct repository *r, const struct object_id *oid,
883 + struct stat *st, const char **path)
884 {
885 struct object_directory *odb;
886 static struct strbuf buf = STRBUF_INIT;
887
888 prepare_alt_odb(r);
889 for (odb = r->objects->odb; odb; odb = odb->next) {
890 - *path = odb_loose_path(odb, &buf, sha1);
890 + *path = odb_loose_path(odb, &buf, oid);
891 if (!lstat(*path, st))
892 return 0;
893 }
@@ -896,11 +896,11 @@ static int stat_sha1_file(struct repository *r, const unsigned char *sha1,
896 }
897
898 /*
899 - * Like stat_sha1_file(), but actually open the object and return the
899 + * Like stat_loose_object(), but actually open the object and return the
900 * descriptor. See the caveats on the "path" parameter above.
901 */
902 -static int open_sha1_file(struct repository *r,
903 - const unsigned char *sha1, const char **path)
902 +static int open_loose_object(struct repository *r,
903 + const struct object_id *oid, const char **path)
904 {
905 int fd;
906 struct object_directory *odb;
@@ -909,7 +909,7 @@ static int open_sha1_file(struct repository *r,
909
910 prepare_alt_odb(r);
911 for (odb = r->objects->odb; odb; odb = odb->next) {
912 - *path = odb_loose_path(odb, &buf, sha1);
912 + *path = odb_loose_path(odb, &buf, oid);
913 fd = git_open(*path);
914 if (fd >= 0)
915 return fd;
@@ -939,10 +939,10 @@ static int quick_has_loose(struct repository *r,
939
940 /*
941 * Map the loose object at "path" if it is not NULL, or the path found by
942 - * searching for a loose object named "sha1".
942 + * searching for a loose object named "oid".
943 */
944 -static void *map_sha1_file_1(struct repository *r, const char *path,
945 - const unsigned char *sha1, unsigned long *size)
944 +static void *map_loose_object_1(struct repository *r, const char *path,
945 + const struct object_id *oid, unsigned long *size)
946 {
947 void *map;
948 int fd;
@@ -950,7 +950,7 @@ static void *map_sha1_file_1(struct repository *r, const char *path,
950 if (path)
951 fd = git_open(path);
952 else
953 - fd = open_sha1_file(r, sha1, &path);
953 + fd = open_loose_object(r, oid, &path);
954 map = NULL;
955 if (fd >= 0) {
956 struct stat st;
@@ -969,10 +969,11 @@ static void *map_sha1_file_1(struct repository *r, const char *path,
969 return map;
970 }
971
972 -void *map_sha1_file(struct repository *r,
973 - const unsigned char *sha1, unsigned long *size)
972 +void *map_loose_object(struct repository *r,
973 + const struct object_id *oid,
974 + unsigned long *size)
975 {
975 - return map_sha1_file_1(r, NULL, sha1, size);
976 + return map_loose_object_1(r, NULL, oid, size);
977 }
978
979 static int unpack_sha1_short_header(git_zstream *stream,
@@ -1161,9 +1162,9 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep)
1162 return parse_sha1_header_extended(hdr, &oi, 0);
1163 }
1164
1164 -static int sha1_loose_object_info(struct repository *r,
1165 - const unsigned char *sha1,
1166 - struct object_info *oi, int flags)
1165 +static int loose_object_info(struct repository *r,
1166 + const struct object_id *oid,
1167 + struct object_info *oi, int flags)
1168 {
1169 int status = 0;
1170 unsigned long mapsize;
@@ -1188,15 +1189,15 @@ static int sha1_loose_object_info(struct repository *r,
1189 const char *path;
1190 struct stat st;
1191 if (!oi->disk_sizep && (flags & OBJECT_INFO_QUICK))
1191 - return quick_has_loose(r, sha1) ? 0 : -1;
1192 - if (stat_sha1_file(r, sha1, &st, &path) < 0)
1192 + return quick_has_loose(r, oid->hash) ? 0 : -1;
1193 + if (stat_loose_object(r, oid, &st, &path) < 0)
1194 return -1;
1195 if (oi->disk_sizep)
1196 *oi->disk_sizep = st.st_size;
1197 return 0;
1198 }
1199
1199 - map = map_sha1_file(r, sha1, &mapsize);
1200 + map = map_loose_object(r, oid, &mapsize);
1201 if (!map)
1202 return -1;
1203
@@ -1208,22 +1209,22 @@ static int sha1_loose_object_info(struct repository *r,
1209 if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) {
1210 if (unpack_sha1_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
1211 status = error(_("unable to unpack %s header with --allow-unknown-type"),
1211 - sha1_to_hex(sha1));
1212 + oid_to_hex(oid));
1213 } else if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1214 status = error(_("unable to unpack %s header"),
1214 - sha1_to_hex(sha1));
1215 + oid_to_hex(oid));
1216 if (status < 0)
1217 ; /* Do nothing */
1218 else if (hdrbuf.len) {
1219 if ((status = parse_sha1_header_extended(hdrbuf.buf, oi, flags)) < 0)
1220 status = error(_("unable to parse %s header with --allow-unknown-type"),
1220 - sha1_to_hex(sha1));
1221 + oid_to_hex(oid));
1222 } else if ((status = parse_sha1_header_extended(hdr, oi, flags)) < 0)
1222 - status = error(_("unable to parse %s header"), sha1_to_hex(sha1));
1223 + status = error(_("unable to parse %s header"), oid_to_hex(oid));
1224
1225 if (status >= 0 && oi->contentp) {
1226 *oi->contentp = unpack_sha1_rest(&stream, hdr,
1226 - *oi->sizep, sha1);
1227 + *oi->sizep, oid->hash);
1228 if (!*oi->contentp) {
1229 git_inflate_end(&stream);
1230 status = -1;
@@ -1289,7 +1290,7 @@ int oid_object_info_extended(struct repository *r, const struct object_id *oid,
1290 return -1;
1291
1292 /* Most likely it's a loose object. */
1292 - if (!sha1_loose_object_info(r, real->hash, oi, flags))
1293 + if (!loose_object_info(r, real, oi, flags))
1294 return 0;
1295
1296 /* Not a loose object; someone else may have just packed it. */
@@ -1417,7 +1418,7 @@ void *read_object_file_extended(const struct object_id *oid,
1418 die(_("replacement %s not found for %s"),
1419 oid_to_hex(repl), oid_to_hex(oid));
1420
1420 - if (!stat_sha1_file(the_repository, repl->hash, &st, &path))
1421 + if (!stat_loose_object(the_repository, repl, &st, &path))
1422 die(_("loose object %s (stored in %s) is corrupt"),
1423 oid_to_hex(repl), path);
1424
@@ -1552,7 +1553,7 @@ int hash_object_file(const void *buf, unsigned long len, const char *type,
1553 }
1554
1555 /* Finalize a file on disk, and close it. */
1555 -static void close_sha1_file(int fd)
1556 +static void close_loose_object(int fd)
1557 {
1558 if (fsync_object_files)
1559 fsync_or_die(fd, "sha1 file");
@@ -1617,7 +1618,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
1618 static struct strbuf tmp_file = STRBUF_INIT;
1619 static struct strbuf filename = STRBUF_INIT;
1620
1620 - loose_object_path(the_repository, &filename, oid->hash);
1621 + loose_object_path(the_repository, &filename, oid);
1622
1623 fd = create_tmpfile(&tmp_file, filename.buf);
1624 if (fd < 0) {
@@ -1665,7 +1666,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
1666 die(_("confused by unstable object source data for %s"),
1667 oid_to_hex(oid));
1668
1668 - close_sha1_file(fd);
1669 + close_loose_object(fd);
1670
1671 if (mtime) {
1672 struct utimbuf utb;
@@ -2255,7 +2256,7 @@ int read_loose_object(const char *path,
2256
2257 *contents = NULL;
2258
2258 - map = map_sha1_file_1(the_repository, path, NULL, &mapsize);
2259 + map = map_loose_object_1(the_repository, path, NULL, &mapsize);
2260 if (!map) {
2261 error_errno(_("unable to mmap %s"), path);
2262 goto out;
streaming.c
+2 -2
@@ -338,8 +338,8 @@ static struct stream_vtbl loose_vtbl = {
338
339 static open_method_decl(loose)
340 {
341 - st->u.loose.mapped = map_sha1_file(the_repository,
342 - oid->hash, &st->u.loose.mapsize);
341 + st->u.loose.mapped = map_loose_object(the_repository,
342 + oid, &st->u.loose.mapsize);
343 if (!st->u.loose.mapped)
344 return -1;
345 if ((unpack_sha1_header(&st->z,