git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] sha1-file: remove OBJECT_INFO_SKIP_CACHED

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jan 4, 2020, 00:13 UTC
Message-ID
<20200104001331.GA130883@google.com>
In-Reply-To
<20200102201630.180969-1-jonathantanmy@google.com>
Jonathan Tan wrote:
Show 13 quoted lines
> 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
Lovely, thank you.
Show 5 quoted lines
> Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
> ---
>  object-store.h |  2 --
>  sha1-file.c    | 38 ++++++++++++++++++--------------------
>  2 files changed, 18 insertions(+), 22 deletions(-)
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

To follow up on Junio's hint in his review: callers can inject additional cached objects by using pretend_object_file. Junio described how this would make sense as a mechanism for building the virtual ancestor object, but we don't do that. In fact, the only caller is fake_working_tree_commit in "git blame", a read-only code path. *phew*

-- >8 --
Subject: sha1-file: document how to use pretend_object_file

Like in-memory alternates, pretend_object_file contains a trap for the unwary: careless callers can use it to create references to an object that does not exist in the on-disk object store.

Add a comment documenting how to use the function without risking such problems.

The only current caller is blame, which uses pretend_object_file to create an in-memory commit representing the working tree state. Noticed during a discussion of how to safely use this function in operations like "git merge" which, unlike blame, are not read-only.

Inspired-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 object-store.h | 8 ++++++++
 1 file changed, 8 insertions(+)
diff --git a/object-store.h b/object-store.h
index 55ee639350..d0fc7b091b 100644
--- a/object-store.h
+++ b/object-store.h
@@ -208,6 +208,14 @@ int hash_object_file_literally(const void *buf, unsigned long len,
 			       const char *type, struct object_id *oid,
 			       unsigned flags);
 
+/*
+ * Add an object file to the in-memory object store, without writing it
+ * to disk.
+ *
+ * Callers are responsible for calling write_object_file to record the
+ * object in persistent storage before writing any other new objects
+ * that reference it.
+ */
 int pretend_object_file(void *, unsigned long, enum object_type,
 			struct object_id *oid);
 
-- 
2.24.1.735.g03f4e72817
Previous: Jeff KingNext: Jeff King
Message 10 of 13 in “sha1-file: remove OBJECT_INFO_SKIP_CACHED”
  1. sha1-file: remove OBJECT_INFO_SKIP_CACHEDJonathan Tan, Dec 30, 2019
  2. Junio C HamanoDec 30, 2019
  3. Jonathan NiederDec 30, 2019
  4. Jonathan TanDec 31, 2019
  5. Jonathan NiederDec 31, 2019
  6. sha1-file: remove OBJECT_INFO_SKIP_CACHEDJonathan Tan, Jan 2, 2020
  7. sha1-file: remove OBJECT_INFO_SKIP_CACHEDJonathan Tan, Jan 2, 2020
  8. Junio C HamanoJan 2, 2020
  9. Jeff KingJan 6, 2020
  10. Jonathan NiederJan 4, 2020
  11. Jeff KingJan 6, 2020
  12. Jonathan NiederJan 6, 2020
  13. Jeff KingJan 7, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.