From 514c5fdd03b914c72a91bb420e46bdc8886940cf Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 7 Jan 2019 03:35:42 -0500 Subject: 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 Signed-off-by: Junio C Hamano --- object-store.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'object-store.h') diff --git a/object-store.h b/object-store.h index e16aa38cae..139feb75b2 100644 --- a/object-store.h +++ b/object-store.h @@ -154,11 +154,13 @@ void raw_object_store_clear(struct raw_object_store *o); /* * Put in `buf` the name of the file in the local object database that - * would be used to store a loose object with the specified sha1. + * would be used to store a loose object with the specified oid. */ -const char *loose_object_path(struct repository *r, struct strbuf *buf, const unsigned char *sha1); +const char *loose_object_path(struct repository *r, struct strbuf *buf, + const struct object_id *oid); -void *map_sha1_file(struct repository *r, const unsigned char *sha1, unsigned long *size); +void *map_loose_object(struct repository *r, const struct object_id *oid, + unsigned long *size); extern void *read_object_file_extended(const struct object_id *oid, enum object_type *type, -- cgit v1.3 From 5d3679ee023642825a5d3c0ca1eec251588a1848 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 7 Jan 2019 03:39:00 -0500 Subject: sha1-file: drop has_sha1_file() There are no callers left of has_sha1_file() or its with_flags() variant. Let's drop them, and convert has_object_file() from a wrapper into the "real" function. Ironically, the sha1 variant was just copying into an object_id internally, so the resulting code is actually shorter! We can also drop the coccinelle rules for catching has_sha1_file() callers. Since the function no longer exists, the compiler will do that for us. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- contrib/coccinelle/object_id.cocci | 32 -------------------------------- object-store.h | 12 ++++-------- sha1-file.c | 16 ++-------------- 3 files changed, 6 insertions(+), 54 deletions(-) (limited to 'object-store.h') diff --git a/contrib/coccinelle/object_id.cocci b/contrib/coccinelle/object_id.cocci index 73886ae583..6a7cf3e02d 100644 --- a/contrib/coccinelle/object_id.cocci +++ b/contrib/coccinelle/object_id.cocci @@ -147,35 +147,3 @@ expression E1, E2; - hashcmp(E1, E2) != 0 + !hasheq(E1, E2) ...>} - -@@ -struct object_id OID; -@@ -- has_sha1_file(OID.hash) -+ has_object_file(&OID) - -@@ -identifier f != has_object_file; -struct object_id *OIDPTR; -@@ - f(...) {<... -- has_sha1_file(OIDPTR->hash) -+ has_object_file(OIDPTR) - ...>} - -@@ -struct object_id OID; -expression E; -@@ -- has_sha1_file_with_flags(OID.hash, E) -+ has_object_file_with_flags(&OID, E) - -@@ -identifier f != has_object_file_with_flags; -struct object_id *OIDPTR; -expression E; -@@ - f(...) {<... -- has_sha1_file_with_flags(OIDPTR->hash, E) -+ has_object_file_with_flags(OIDPTR, E) - ...>} diff --git a/object-store.h b/object-store.h index 139feb75b2..a7808bb624 100644 --- a/object-store.h +++ b/object-store.h @@ -202,20 +202,16 @@ int read_loose_object(const char *path, void **contents); /* - * Convenience for sha1_object_info_extended() with a NULL struct + * Convenience for oid_object_info_extended() with a NULL struct * object_info. OBJECT_INFO_SKIP_CACHED is automatically set; pass * nonzero flags to also set other flags. */ -extern int has_sha1_file_with_flags(const unsigned char *sha1, int flags); -static inline int has_sha1_file(const unsigned char *sha1) +int has_object_file_with_flags(const struct object_id *oid, int flags); +static inline int has_object_file(const struct object_id *oid) { - return has_sha1_file_with_flags(sha1, 0); + return has_object_file_with_flags(oid, 0); } -/* Same as the above, except for struct object_id. */ -extern int has_object_file(const struct object_id *oid); -extern int has_object_file_with_flags(const struct object_id *oid, int flags); - /* * Return true iff an alternate object database has a loose object * with the specified name. This function does not respect replace diff --git a/sha1-file.c b/sha1-file.c index 95186c019a..0d926e3c13 100644 --- a/sha1-file.c +++ b/sha1-file.c @@ -1752,26 +1752,14 @@ int force_object_loose(const struct object_id *oid, time_t mtime) return ret; } -int has_sha1_file_with_flags(const unsigned char *sha1, int flags) +int has_object_file_with_flags(const struct object_id *oid, int flags) { - struct object_id oid; if (!startup_info->have_repository) return 0; - hashcpy(oid.hash, sha1); - return oid_object_info_extended(the_repository, &oid, NULL, + return oid_object_info_extended(the_repository, oid, NULL, flags | OBJECT_INFO_SKIP_CACHED) >= 0; } -int has_object_file(const struct object_id *oid) -{ - return has_sha1_file(oid->hash); -} - -int has_object_file_with_flags(const struct object_id *oid, int flags) -{ - return has_sha1_file_with_flags(oid->hash, flags); -} - static void check_tree(const void *buf, size_t size) { struct tree_desc desc; -- cgit v1.3