[PATCH 04/17] index: make narrow index incompatible with older git

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Index in narrow repos is also narrowed and should not be used to
create commits straight away (to be explained later on). On the other
hand, while normal index can still be used in narrow repos, it should
be narrowed when the repo becomes narrow. Disallow that case too.

Add non-optional extension "narw" so that older git will refuse to
read it. Also check narrow prefix in index against $GIT_DIR/narrow,
just in case.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx>
---
 cache.h                |    1 +
 read-cache.c           |   32 ++++++++++++++++++++++++++++++--
 t/t0063-narrow-repo.sh |   28 ++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/cache.h b/cache.h
index d09c4fc..88a2ec6 100644
--- a/cache.h
+++ b/cache.h
@@ -292,6 +292,7 @@ struct index_state {
 	struct string_list *resolve_undo;
 	struct cache_tree *cache_tree;
 	struct cache_time timestamp;
+	char *narrow_prefix;
 	void *alloc;
 	unsigned name_hash_initialized : 1,
 		 initialized : 1;
diff --git a/read-cache.c b/read-cache.c
index 1f42473..250013c 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -12,6 +12,7 @@
 #include "commit.h"
 #include "blob.h"
 #include "resolve-undo.h"
+#include "narrow-tree.h"
 
 static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
 
@@ -25,8 +26,9 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int reall
  */
 
 #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
-#define CACHE_EXT_TREE 0x54524545	/* "TREE" */
-#define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
+#define CACHE_EXT_TREE		0x54524545	/* "TREE" */
+#define CACHE_EXT_RESOLVE_UNDO	0x52455543	/* "REUC" */
+#define CACHE_EXT_NARROW	0x6e617277	/* "narw" */
 
 struct index_state the_index;
 
@@ -1188,6 +1190,9 @@ static int read_index_extension(struct index_state *istate,
 	case CACHE_EXT_RESOLVE_UNDO:
 		istate->resolve_undo = resolve_undo_read(data, sz);
 		break;
+	case CACHE_EXT_NARROW:
+		istate->narrow_prefix = xstrdup(data);
+		break;
 	default:
 		if (*ext < 'A' || 'Z' < *ext)
 			return error("index uses %.4s extension, which we do not understand",
@@ -1352,6 +1357,18 @@ int read_index_from(struct index_state *istate, const char *path)
 		src_offset += extsize;
 	}
 	munmap(mmap, mmap_size);
+
+	if ((!get_narrow_prefix() && !istate->narrow_prefix))
+		;		/* good */
+	else if (get_narrow_prefix() && istate->narrow_prefix) {
+		char *buf = get_narrow_string();
+		if (strcmp(buf, istate->narrow_prefix))
+			die("Invalid index, narrow prefix does not match $GIT_DIR/narrow");
+		free(buf);
+	}
+	else
+		die("Invalid index, not suitable for narrow repository");
+
 	return istate->cache_nr;
 
 unmap:
@@ -1378,6 +1395,8 @@ int discard_index(struct index_state *istate)
 	free(istate->alloc);
 	istate->alloc = NULL;
 	istate->initialized = 0;
+	free(istate->narrow_prefix);
+	istate->narrow_prefix = NULL;
 
 	/* no need to throw away allocated active_cache */
 	return 0;
@@ -1607,6 +1626,15 @@ int write_index(struct index_state *istate, int newfd)
 		if (err)
 			return -1;
 	}
+	if (get_narrow_prefix()) {
+		char *buf = get_narrow_string();
+		int len = strlen(buf)+1;
+		err = write_index_ext_header(&c, newfd, CACHE_EXT_NARROW, len) < 0 ||
+			ce_write(&c, newfd, buf, len) < 0;
+		free(buf);
+		if (err)
+			return -1;
+	}
 
 	if (ce_flush(&c, newfd) || fstat(newfd, &st))
 		return -1;
diff --git a/t/t0063-narrow-repo.sh b/t/t0063-narrow-repo.sh
index 51b753d..926802c 100755
--- a/t/t0063-narrow-repo.sh
+++ b/t/t0063-narrow-repo.sh
@@ -43,4 +43,32 @@ test_expect_success 'unsorted multiple prefix' '
 	test_must_fail git rev-parse --narrow-prefix
 '
 
+test_expect_success 'create narrow index' '
+	echo a >.git/narrow &&
+	: >foo
+	git add foo &&
+	test -f .git/index
+'
+
+test_expect_success '$GIT_DIR/narrow and index do not match' '
+	echo b >.git/narrow &&
+	test_must_fail git add foo
+'
+
+test_expect_success 'narrow index and normal repo' '
+	rm .git/narrow &&
+	test_must_fail git add foo
+'
+
+test_expect_success 'turn to normal index again' '
+	rm .git/index &&
+	git add foo &&
+	test -f .git/index
+'
+
+test_expect_success 'normal index and narrow repo' '
+	echo a >.git/narrow &&
+	test_must_fail git add foo
+'
+
 test_done
-- 
1.7.1.rc1.69.g24c2f7

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]