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

[PATCH 15/32] unpack_trees: only unpack $GIT_DIR/narrow subtree in narrow repository

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Aug 24, 2010, 22:20 UTC
Message-ID
<1282688422-7738-16-git-send-email-pclouds@gmail.com>
In-Reply-To
<1282688422-7738-1-git-send-email-pclouds@gmail.com>

By definition, narrow repository is incomplete. It does not even have enough tree for a single commit. So populating a full index is impossible.

Because of this, unpack_trees() is modified to only unpack trees within $GIT_DIR/narrow, which narrow repo has all needed trees. This makes the resulting index unsuitable for creating commits later on. This is the reason index version is increased to 4, to avoid older git from using it.

The resulting tree objects created from the index is only part of the full tree. Manipulation will be needed at commit time to create proper tree for commits.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 t/t1013-read-tree-narrow.sh |   72 +++++++++++++++++++++++++++++++++++++++++++
 unpack-trees.c              |   70 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 141 insertions(+), 1 deletions(-)
 create mode 100755 t/t1013-read-tree-narrow.sh
diff --git a/t/t1013-read-tree-narrow.sh b/t/t1013-read-tree-narrow.sh
new file mode 100755
index 0000000..1921985
--- /dev/null
+++ b/t/t1013-read-tree-narrow.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+test_description='read-tree in narrow mode'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	test_tick &&
+	test_commit 1 &&
+	mkdir t1 t2 t1/t12 &&
+	echo 0 > f0 &&
+	echo 10 > t1/f10 &&
+	echo 120 > t1/t12/f120 &&
+	echo 20 > t2/f20
+	git add t1 t2 f0 && git commit -m initial &&
+	HEAD=`git rev-parse HEAD` &&
+	git rev-parse HEAD | git pack-objects --revs --narrow-tree=t1/t12 pack &&
+	test_create_repo narrow &&
+	mv pack-* narrow/.git/objects/pack &&
+	cd narrow &&
+	echo $HEAD >.git/refs/heads/master &&
+	echo "ref: refs/heads/master" >.git/HEAD &&
+	echo t1/t12 >.git/narrow
+'
+
+test_expect_failure ls-tree '
+	git ls-tree -r HEAD &&
+	git ls-files --stage >result &&
+	echo "100644 blob 52bd8e43afb01d0c9747f1fedf2fc94684ee4cc4	t1/t12/f120" >expected &&
+	test_cmp expected result
+'
+
+test_expect_success read-tree '
+	git read-tree HEAD &&
+	git ls-files --stage >result &&
+	echo "100644 52bd8e43afb01d0c9747f1fedf2fc94684ee4cc4 0	t1/t12/f120" >expected &&
+	test_cmp expected result
+'
+
+test_expect_success checkout '
+	git checkout . &&
+	test_cmp ../t1/t12/f120 t1/t12/f120
+'
+
+cat <<EOF >diff.expected
+diff --git a/t1/t12/f120 b/t1/t12/f120
+index 52bd8e4..645fb94 100644
+--- a/t1/t12/f120
++++ b/t1/t12/f120
+@@ -1 +1,2 @@
+ 120
++modified
+EOF
+
+test_expect_success diff '
+	echo modified >> t1/t12/f120 &&
+	git diff >result &&
+	test_cmp diff.expected result
+'
+
+test_expect_success 'diff HEAD' '
+	git diff HEAD >result &&
+	test_cmp diff.expected result
+'
+
+test_expect_success 'diff --cached' '
+	git add -u . &&
+	git diff --cached >result &&
+	test_cmp diff.expected result
+'
+
+test_done
diff --git a/unpack-trees.c b/unpack-trees.c
index f561d88..661fcb7 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -713,7 +713,7 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str
  * N-way merge "len" trees.  Returns 0 on success, -1 on failure to manipulate the
  * resulting index, -2 on failure to reflect the changes to the work tree.
  */
-int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
+static int unpack_trees_1(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
 {
 	int i, ret;
 	static struct cache_entry *dfc;
@@ -839,6 +839,74 @@ return_failed:
 	goto done;
 }
 
+static int find_tree_desc(struct tree_desc *desc, unsigned char *newsha1, const char *path)
+{
+	struct name_entry entry;
+	const char *slash;
+	int len;
+
+	slash = strchr(path, '/');
+	len = slash ? slash - path : strlen(path);
+
+	while (tree_entry(desc, &entry)) {
+		if (!S_ISDIR(entry.mode))
+			continue;
+		if (!strncmp(entry.path, path, len)) {
+			if (slash)
+				return find_tree(entry.sha1, newsha1, slash+1);
+			else {
+				hashcpy(newsha1, entry.sha1);
+				return 1;
+			}
+		}
+	}
+	return 0;
+}
+
+int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
+{
+	if (!get_narrow_prefix())
+		return unpack_trees_1(len, t, o);
+	else {
+		struct tree_desc *t2;
+		struct unpack_trees_options o2;
+		char *prefix = NULL;
+		void **buf;
+		int i, ret;
+		t2 = xmalloc(sizeof(*t2)*len);
+		buf = xmalloc(sizeof(*buf)*len);
+		for (i = 0; i < len; i++) {
+			unsigned char sha1[20];
+			unsigned long size;
+			enum object_type type;
+
+			if (!find_tree_desc(t+i, sha1, get_narrow_prefix()))
+				return -1;
+			buf[i] = read_sha1_file(sha1, &type, &size);
+			if (type != OBJ_TREE)
+				return -1;
+			init_tree_desc(t2+i, buf[i], size);
+		}
+		o2 = *o;
+		if (!o->prefix)
+			o2.prefix = xstrdup(get_narrow_prefix());
+		else {
+			prefix = xmalloc(strlen(get_narrow_prefix()) + strlen(o->prefix)+1);
+			strcpy(prefix, o->prefix);
+			strcat(prefix, get_narrow_prefix());
+			o2.prefix = prefix;
+		}
+		ret = unpack_trees_1(len, t2, &o2);
+		for (i = 0; i < len; i++) {
+			free(buf[i]);
+		}
+		free(prefix);
+		free(t2);
+		free(buf);
+		return ret;
+	}
+}
+
 /* Here come the merge functions */
 
 static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
-- 
1.7.1.rc1.69.g24c2f7
Previous: Nguyễn Thái Ngọc DuyNext: Elijah Newren
Message 30 of 66 in “subtree clone v2”
  1. 00/32 subtree clone v2Nguyễn Thái Ngọc Duy, Aug 24, 2010
  2. 01/32 add const to ce_write()Nguyễn Thái Ngọc Duy, Aug 24, 2010
  3. 02/32 cache-tree: abstract out write_sha1_file from cache_tree_update()Nguyễn Thái Ngọc Duy, Aug 24, 2010
  4. Jonathan NiederAug 24, 2010
  5. 03/32 cache-tree: ignore CE_REMOVE entries in verify_cache()Nguyễn Thái Ngọc Duy, Aug 24, 2010
  6. Jonathan NiederAug 24, 2010
  7. Nguyen Thai Ngoc DuyAug 25, 2010
  8. Jonathan NiederAug 25, 2010
  9. 04/32 move do_compress() from pack-objects.c to pack-write.cNguyễn Thái Ngọc Duy, Aug 24, 2010
  10. Jonathan NiederAug 24, 2010
  11. Nguyen Thai Ngoc DuyAug 25, 2010
  12. 05/32 pack-write: add functions for creating simple packsNguyễn Thái Ngọc Duy, Aug 24, 2010
  13. 06/32 tree.c: Add {set,clear}_tree_marksNguyễn Thái Ngọc Duy, Aug 24, 2010
  14. 07/32 tree.c: find_subtree() to search for a treeNguyễn Thái Ngọc Duy, Aug 24, 2010
  15. Elijah NewrenAug 25, 2010
  16. Nguyen Thai Ngoc DuyAug 25, 2010
  17. Elijah NewrenAug 25, 2010
  18. 08/32 Add $GIT_DIR/narrow checkNguyễn Thái Ngọc Duy, Aug 24, 2010
  19. 09/32 index: make narrow index incompatible with older gitNguyễn Thái Ngọc Duy, Aug 24, 2010
  20. Jonathan NiederAug 24, 2010
  21. Nguyen Thai Ngoc DuyAug 25, 2010
  22. 10/32 rev-list: support traversing in narrow repository modeNguyễn Thái Ngọc Duy, Aug 24, 2010
  23. Elijah NewrenAug 25, 2010
  24. 11/32 rev-list: support --narrow-treeNguyễn Thái Ngọc Duy, Aug 24, 2010
  25. Elijah NewrenAug 25, 2010
  26. Nguyen Thai Ngoc DuyAug 25, 2010
  27. 12/32 pack-objects: support --narrow-treeNguyễn Thái Ngọc Duy, Aug 24, 2010
  28. 13/32 upload-pack: support narrow-tree capabilityNguyễn Thái Ngọc Duy, Aug 24, 2010
  29. 14/32 fetch-pack: support --narrow-treeNguyễn Thái Ngọc Duy, Aug 24, 2010
  30. 15/32 unpack_trees: only unpack $GIT_DIR/narrow subtree in narrow repositoryNguyễn Thái Ngọc Duy, Aug 24, 2010
  31. Elijah NewrenAug 25, 2010
  32. Nguyen Thai Ngoc DuyAug 25, 2010
  33. 16/32 cache-tree: only cache tree within narrow areaNguyễn Thái Ngọc Duy, Aug 24, 2010
  34. 17/32 tree-diff: add narrow versions of diff_{root_,}tree_sha1Nguyễn Thái Ngọc Duy, Aug 24, 2010
  35. 18/32 log-tree: use narrow version of diff_tree_sha1Nguyễn Thái Ngọc Duy, Aug 24, 2010
  36. 19/32 clone: support --narrow optionNguyễn Thái Ngọc Duy, Aug 24, 2010
  37. 20/32 narrow-tree: add join_narrow_tree to do tree fixup for commitsNguyễn Thái Ngọc Duy, Aug 24, 2010
  38. 21/32 commit: add narrow's commit_tree versionNguyễn Thái Ngọc Duy, Aug 24, 2010
  39. 22/32 commit: use commit_narrow_tree() to support narrow repoNguyễn Thái Ngọc Duy, Aug 24, 2010
  40. 23/32 commit-tree: require --narrow-base in narrow repoNguyễn Thái Ngọc Duy, Aug 24, 2010
  41. 24/32 merge: refuse to merge if narrow bases are differentNguyễn Thái Ngọc Duy, Aug 24, 2010
  42. 25/32 merge: prepare commit properly in narrow modeNguyễn Thái Ngọc Duy, Aug 24, 2010
  43. 26/32 Add upload-narrow-base commandNguyễn Thái Ngọc Duy, Aug 24, 2010
  44. 27/32 rev-list: traverse some more trees to make upload-narrow-base happyNguyễn Thái Ngọc Duy, Aug 24, 2010
  45. 28/32 narrow-tree: add oldest_narrow_base()Nguyễn Thái Ngọc Duy, Aug 24, 2010
  46. 29/32 Add command fetch-narrow-baseNguyễn Thái Ngọc Duy, Aug 24, 2010
  47. 30/32 merge: support merging when narrow bases are differentNguyễn Thái Ngọc Duy, Aug 24, 2010
  48. 31/32 send-pack: do not use thin pack in narrow modeNguyễn Thái Ngọc Duy, Aug 24, 2010
  49. 32/32 daemon: support upload-narrow-baseNguyễn Thái Ngọc Duy, Aug 24, 2010
  50. Jonathan NiederAug 24, 2010
  51. Nguyen Thai Ngoc DuyAug 24, 2010
  52. Jonathan NiederAug 24, 2010
  53. Nguyen Thai Ngoc DuyAug 25, 2010
  54. Elijah NewrenAug 25, 2010
  55. Nguyen Thai Ngoc DuyAug 25, 2010
  56. Elijah NewrenAug 25, 2010
  57. Nguyen Thai Ngoc DuyAug 25, 2010
  58. Elijah NewrenAug 25, 2010
  59. Nguyen Thai Ngoc DuyAug 25, 2010
  60. Elijah NewrenAug 26, 2010
  61. Nguyen Thai Ngoc DuyAug 26, 2010
  62. Elijah NewrenAug 26, 2010
  63. Nguyen Thai Ngoc DuyAug 26, 2010
  64. Elijah NewrenAug 25, 2010
  65. Junio C HamanoAug 25, 2010
  66. Elijah NewrenAug 25, 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.