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

[RFC PATCH 07/15] cache_tree_update(): Capability to handle tree entries missing from index

From
Elijah Newren <newren@gmail.com>
Date
Sep 5, 2010, 00:13 UTC
Message-ID
<1283645647-1891-8-git-send-email-newren@gmail.com>
In-Reply-To
<1283645647-1891-1-git-send-email-newren@gmail.com>

cache_tree_update() will write trees using the index. With sparse clones, the index will only contain entries matching the sparse limits, meaning that the index does not provide enough information to write complete tree objects. Having cache_tree_update() take a tree (typically HEAD), will allow new complete trees to be constructed by using entries from the specified tree outside the sparse limits together with the index.

Of course, in the non-sparse clone case, cache_tree_update() needs no tree to be provided since the index contains all relevant information. So providing NULL as the tree is supported as another way of getting the traditional behavior.

This patch only provides the new capability without invoking it; subsequent patches will update callers of cache_tree_update() to provide a relevant tree.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 cache-tree.c |   88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 87 insertions(+), 1 deletions(-)
diff --git a/cache-tree.c b/cache-tree.c
index c60cf91..2ba6a76 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -2,6 +2,7 @@
 #include "tree.h"
 #include "tree-walk.h"
 #include "cache-tree.h"
+#include "diff.h"  /* FIXME: for tree_entry_interesting; maybe it should be in tree-walk.h? */
 
 #ifndef DEBUG
 #define DEBUG 0
@@ -232,15 +233,91 @@ int cache_tree_fully_valid(struct cache_tree *it)
 	return 1;
 }
 
+static void setup_tree_desc(struct tree_desc *desc, struct tree* tree)
+{
+	if (git_sparse_pathspecs && tree)
+		init_tree_desc(desc, tree->buffer, tree->size);
+	else
+		init_tree_desc(desc, NULL, 0);
+}
+
+static struct tree* find_non_sparse_subtree(struct tree_desc *desc,
+					    const char *path,
+					    int pathlen)
+{
+	struct name_entry entry;
+
+	if (!git_sparse_pathspecs)
+		return NULL; /* No tree needed or useful */
+
+	while (tree_entry(desc, &entry))
+		  if (!memcmp(entry.path, path, pathlen) &&
+		      strlen(entry.path) == pathlen) {
+			struct tree *subtree = lookup_tree(entry.sha1);
+			if (parse_tree(subtree) < 0)
+				die("bad tree %s", sha1_to_hex(entry.sha1));
+			return subtree;
+		  }
+	return NULL;
+}
+
+static void add_missing_paths_before(const char *path,
+				     int pathlen,
+				     int baselen,
+				     struct tree_desc *desc,
+				     struct strbuf *buffer)
+{
+	if (!git_sparse_pathspecs)
+		return; /* No paths are missing */
+
+	for (; desc->size; update_tree_entry(desc)) {
+		struct name_entry entry = desc->entry;
+		int entlen = strlen(entry.path);
+
+		/* We only want paths before path */
+		if (path)
+			/*
+			 * FIXME: if entlen < pathlen, do I need to
+			 * use strncmp instead? Does entry.path being
+			 * NUL-terminated ensure memcmp exits
+			 * early?
+			 */
+			if (memcmp(entry.path,
+				   path+baselen,
+				   pathlen-baselen) >= 0)
+				break;
+
+		/* We only want paths "missing" from index due to sparsity */
+		if (path) {
+			int show = tree_entry_interesting(desc,
+							  path,
+							  baselen,
+							  &git_sparse_diffopts);
+			if (show == 2) {
+				desc->size = 0;
+				break;
+			}
+			else if (show > 0)
+				continue;
+		}
+
+		strbuf_grow(buffer, entlen + 100);
+		strbuf_addf(buffer, "%o %.*s%c", entry.mode, entlen, entry.path, '\0');
+		strbuf_add(buffer, entry.sha1, 20);
+	}
+}
+
 static int update_one(struct cache_tree *it,
 		      struct cache_entry **cache,
 		      int entries,
+		      struct tree *non_sparse_tree,
 		      const char *base,
 		      int baselen,
 		      int missing_ok,
 		      int dryrun)
 {
 	struct strbuf buffer;
+	struct tree_desc desc;
 	int i;
 
 	if (0 <= it->entry_count && has_sha1_file(it->sha1))
@@ -257,9 +334,11 @@ static int update_one(struct cache_tree *it,
 	/*
 	 * Find the subtrees and update them.
 	 */
+	setup_tree_desc(&desc, non_sparse_tree);
 	for (i = 0; i < entries; i++) {
 		struct cache_entry *ce = cache[i];
 		struct cache_tree_sub *sub;
+		struct tree *subtree;
 		const char *path, *slash;
 		int pathlen, sublen, subcnt;
 
@@ -280,8 +359,10 @@ static int update_one(struct cache_tree *it,
 		sub = find_subtree(it, path + baselen, sublen, 1);
 		if (!sub->cache_tree)
 			sub->cache_tree = cache_tree();
+		subtree = find_non_sparse_subtree(&desc, path+baselen, sublen);
 		subcnt = update_one(sub->cache_tree,
 				    cache + i, entries - i,
+				    subtree,
 				    path,
 				    baselen + sublen + 1,
 				    missing_ok,
@@ -298,6 +379,7 @@ static int update_one(struct cache_tree *it,
 	 * Then write out the tree object for this level.
 	 */
 	strbuf_init(&buffer, 8192);
+	setup_tree_desc(&desc, non_sparse_tree);
 
 	for (i = 0; i < entries; i++) {
 		struct cache_entry *ce = cache[i];
@@ -309,6 +391,8 @@ static int update_one(struct cache_tree *it,
 
 		path = ce->name;
 		pathlen = ce_namelen(ce);
+		add_missing_paths_before(path, pathlen, baselen,
+					 &desc, &buffer);
 		if (pathlen <= baselen || memcmp(base, path, baselen))
 			break; /* at the end of this level */
 
@@ -346,6 +430,7 @@ static int update_one(struct cache_tree *it,
 			mode, entlen, path + baselen);
 #endif
 	}
+	add_missing_paths_before(NULL, 0, 0, &desc, &buffer);
 
 	if (dryrun)
 		hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
@@ -374,7 +459,8 @@ int cache_tree_update(struct cache_tree *it,
 	i = verify_cache(cache, entries);
 	if (i)
 		return i;
-	i = update_one(it, cache, entries, "", 0, missing_ok, dryrun);
+
+	i = update_one(it, cache, entries, NULL, "", 0, missing_ok, dryrun);
 	if (i < 0)
 		return i;
 	return 0;
-- 
1.7.2.2.140.gd06af
Previous: Nguyen Thai Ngoc DuyNext: Nguyen Thai Ngoc Duy
Message 17 of 41 in “Sparse clones”
  1. 00/15 Sparse clonesElijah Newren, Sep 5, 2010
  2. 01/15 README-sparse-clone: Add a basic writeup of my ideas for sparse clonesElijah Newren, Sep 5, 2010
  3. Nguyen Thai Ngoc DuySep 5, 2010
  4. Elijah NewrenSep 5, 2010
  5. Nguyen Thai Ngoc DuySep 6, 2010
  6. 02/15 Add tests for client handling in a sparse repositoryElijah Newren, Sep 5, 2010
  7. 03/15 Read sparse limiting args from $GIT_DIR/sparse-limitElijah Newren, Sep 5, 2010
  8. 04/15 When unpacking in a sparse repository, avoid traversing missing trees/blobsElijah Newren, Sep 5, 2010
  9. 05/15 read_tree_recursive: Avoid missing blobs and trees in a sparse repositoryElijah Newren, Sep 5, 2010
  10. Nguyen Thai Ngoc DuySep 5, 2010
  11. Elijah NewrenSep 5, 2010
  12. Elijah NewrenSep 5, 2010
  13. 06/15 Automatically reuse sparse limiting arguments in revision walkingElijah Newren, Sep 5, 2010
  14. Nguyen Thai Ngoc DuySep 5, 2010
  15. Elijah NewrenSep 5, 2010
  16. Nguyen Thai Ngoc DuySep 5, 2010
  17. 07/15 cache_tree_update(): Capability to handle tree entries missing from indexElijah Newren, Sep 5, 2010
  18. Nguyen Thai Ngoc DuySep 5, 2010
  19. Elijah NewrenSep 5, 2010
  20. Elijah NewrenSep 6, 2010
  21. Nguyen Thai Ngoc DuySep 6, 2010
  22. 0/4 en/object-list-with-pathspec updateNguyễn Thái Ngọc Duy, Sep 6, 2010
  23. 1/4 Add testcases showing how pathspecs are ignored with rev-list --objectsNguyễn Thái Ngọc Duy, Sep 6, 2010
  24. 2/4 tree-walk: copy tree_entry_interesting() as is from tree-diff.cNguyễn Thái Ngọc Duy, Sep 6, 2010
  25. Elijah NewrenSep 6, 2010
  26. Nguyen Thai Ngoc DuySep 6, 2010
  27. 3/4 tree-walk: actually move tree_entry_interesting() to tree-walk.cNguyễn Thái Ngọc Duy, Sep 6, 2010
  28. Elijah NewrenSep 6, 2010
  29. Nguyen Thai Ngoc DuySep 6, 2010
  30. Junio C HamanoSep 6, 2010
  31. 4/4 Make rev-list --objects work together with pathspecsNguyễn Thái Ngọc Duy, Sep 6, 2010
  32. Nguyen Thai Ngoc DuySep 7, 2010
  33. Elijah NewrenSep 7, 2010
  34. 08/15 cache_tree_update(): Require relevant tree to be passedElijah Newren, Sep 5, 2010
  35. 09/15 Add tests for communication dealing with sparse repositoriesElijah Newren, Sep 5, 2010
  36. 10/15 sparse-repo: Provide a function to record sparse limiting argumentsElijah Newren, Sep 5, 2010
  37. 11/15 builtin-clone: Accept paths for sparse cloneElijah Newren, Sep 5, 2010
  38. 12/15 Pass extra (rev-list) args on, at least in some casesElijah Newren, Sep 5, 2010
  39. 13/15 upload-pack: Handle extra rev-list arguments being passedElijah Newren, Sep 5, 2010
  40. 14/15 EVIL COMMIT: Include all commitsElijah Newren, Sep 5, 2010
  41. 15/15 clone: Ensure sparse limiting arguments are used in subsequent operationsElijah Newren, Sep 5, 2010

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.