@matheusbsbs / matheus-bagliano / commits / 9c8a294a1a
Fork 0

sha1-file: remove OBJECT_INFO_SKIP_CACHED

In a partial clone, if a user provides the hash of the empty tree ("git mktree </dev/null" - for SHA-1, this is 4b825d...) to a command which requires that that object be parsed, for example: git diff-tree 4b825d <a non-empty tree> then Git will lazily fetch the empty tree, unnecessarily, because parsing of that object invokes repo_has_object_file(), which does not special-case the empty tree. Instead, teach repo_has_object_file() to consult find_cached_object() (which handles the empty tree), thus bringing it in line with the rest of the object-store-accessing functions. A cost is that repo_has_object_file() will now need to oideq upon each invocation, but that is trivial compared to the filesystem lookup or the pack index search required anyway. (And if find_cached_object() needs to do more because of previous invocations to pretend_object_file(), all the more reason to be consistent in whether we present cached objects.) As a historical note, the function now known as repo_read_object_file() was taught the empty tree in 346245a1bb ("hard-code the empty tree object", 2008-02-13), and the function now known as oid_object_info() was taught the empty tree in c4d9986f5f ("sha1_object_info: examine cached_object store too", 2011-02-07). repo_has_object_file() was never updated, perhaps due to oversight. The flag OBJECT_INFO_SKIP_CACHED, introduced later in dfdd4afcf9 ("sha1_file: teach sha1_object_info_extended more flags", 2017-06-26) and used in e83e71c5e1 ("sha1_file: refactor has_sha1_file_with_flags", 2017-06-26), was introduced to preserve this difference in empty-tree handling, but now it can be removed. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Jan 2, 2020 at 12:16 UTC 9c8a294a1ae1335511475db9c0eb8841c0ec9738
2 files changed +18 -22
object-store.h
-2
@@ -271,8 +271,6 @@ struct object_info {
271 #define OBJECT_INFO_LOOKUP_REPLACE 1
272 /* Allow reading from a loose object file of unknown/bogus type */
273 #define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
274 -/* Do not check cached storage */
275 -#define OBJECT_INFO_SKIP_CACHED 4
274 /* Do not retry packed storage after checking packed and loose storage */
275 #define OBJECT_INFO_QUICK 8
276 /* Do not check loose object */
sha1-file.c
+18 -20
@@ -1417,6 +1417,7 @@ int oid_object_info_extended(struct repository *r, const struct object_id *oid,
1417 struct object_info *oi, unsigned flags)
1418 {
1419 static struct object_info blank_oi = OBJECT_INFO_INIT;
1420 + struct cached_object *co;
1421 struct pack_entry e;
1422 int rtype;
1423 const struct object_id *real = oid;
@@ -1431,24 +1432,22 @@ int oid_object_info_extended(struct repository *r, const struct object_id *oid,
1432 if (!oi)
1433 oi = &blank_oi;
1434
1434 - if (!(flags & OBJECT_INFO_SKIP_CACHED)) {
1435 - struct cached_object *co = find_cached_object(real);
1436 - if (co) {
1437 - if (oi->typep)
1438 - *(oi->typep) = co->type;
1439 - if (oi->sizep)
1440 - *(oi->sizep) = co->size;
1441 - if (oi->disk_sizep)
1442 - *(oi->disk_sizep) = 0;
1443 - if (oi->delta_base_sha1)
1444 - hashclr(oi->delta_base_sha1);
1445 - if (oi->type_name)
1446 - strbuf_addstr(oi->type_name, type_name(co->type));
1447 - if (oi->contentp)
1448 - *oi->contentp = xmemdupz(co->buf, co->size);
1449 - oi->whence = OI_CACHED;
1450 - return 0;
1451 - }
1435 + co = find_cached_object(real);
1436 + if (co) {
1437 + if (oi->typep)
1438 + *(oi->typep) = co->type;
1439 + if (oi->sizep)
1440 + *(oi->sizep) = co->size;
1441 + if (oi->disk_sizep)
1442 + *(oi->disk_sizep) = 0;
1443 + if (oi->delta_base_sha1)
1444 + hashclr(oi->delta_base_sha1);
1445 + if (oi->type_name)
1446 + strbuf_addstr(oi->type_name, type_name(co->type));
1447 + if (oi->contentp)
1448 + *oi->contentp = xmemdupz(co->buf, co->size);
1449 + oi->whence = OI_CACHED;
1450 + return 0;
1451 }
1452
1453 while (1) {
@@ -1932,8 +1931,7 @@ int repo_has_object_file_with_flags(struct repository *r,
1931 {
1932 if (!startup_info->have_repository)
1933 return 0;
1935 - return oid_object_info_extended(r, oid, NULL,
1936 - flags | OBJECT_INFO_SKIP_CACHED) >= 0;
1934 + return oid_object_info_extended(r, oid, NULL, flags) >= 0;
1935 }
1936
1937 int repo_has_object_file(struct repository *r,