Skip to contentSkip to conversation

Introduce GIT_INDEX_PREFIX

PATCH In review Nguyễn Thái Ngọc Duy 3 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Introduce GIT_INDEX_PREFIX

GIT_INDEX_PREFIX is used to limit write access to a specific directory. Only "important" information is protected by index prefix (those will be used to create tree objects)

When GIT_INDEX_PREFIX is set, any attempt to modify the index (refresh it is okay though) will bail out. read-tree and merge, however, can write to full index. For merge, no conflict is allowed outside index prefix.

---
 builtin-merge-recursive.c |    4 ++-
 builtin-read-tree.c       |    5 +++
 builtin-rev-parse.c       |    5 +++
 builtin-update-index.c    |    3 ++
 cache.h                   |    2 +
 environment.c             |   12 ++++++
 read-cache.c              |   84 +++++++++++++++++++++++++++++++++++++++++++-
 unpack-trees.c            |    3 ++
 unpack-trees.h            |    3 +-
 9 files changed, 117 insertions(+), 4 deletions(-)
builtin-merge-recursive.c +3 -1
index 362c290..94ca808 100644
@@ -209,8 +209,10 @@ static int git_merge_trees(int index_only,
 	memset(&opts, 0, sizeof(opts));
 	if (index_only)
 		opts.index_only = 1;
-	else
+	else {
 		opts.update = 1;
+		opts.check_index_prefix = 1;
+	}
 	opts.merge = 1;
 	opts.head_idx = 2;
 	opts.fn = threeway_merge;
builtin-read-tree.c +5 -0
index 5a09e17..ea0a929 100644
@@ -242,6 +242,11 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
 			opts.head_idx = 1;
 	}
 
+	if (get_index_prefix()) {
+		read_cache();
+		opts.check_index_prefix = 1;
+	}
+
 	for (i = 0; i < nr_trees; i++) {
 		struct tree *tree = trees[i];
 		parse_tree(tree);
builtin-rev-parse.c +5 -0
index a7860ed..1888208 100644
@@ -499,6 +499,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 					puts(prefix);
 				continue;
 			}
+			if (!strcmp(arg, "--show-index-prefix")) {
+				if (get_index_prefix())
+					puts(get_index_prefix());
+				continue;
+			}
 			if (!strcmp(arg, "--show-cdup")) {
 				const char *pfx = prefix;
 				if (!is_inside_work_tree()) {
builtin-update-index.c +3 -0
index 9e0d7ab..71c83ec 100644
@@ -242,6 +242,9 @@ static void chmod_path(int flip, const char *path)
 	struct cache_entry *ce;
 	unsigned int mode;
 
+	if (get_index_prefix() && prefixcmp(path, get_index_prefix()))
+		die("%s: cannot update index outside %s", path, get_index_prefix());
+
 	pos = cache_name_pos(path, strlen(path));
 	if (pos < 0)
 		goto fail;
cache.h +2 -0
index eab1a17..9f5a613 100644
@@ -300,6 +300,7 @@ static inline enum object_type object_type(unsigned int mode)
 #define CONFIG_ENVIRONMENT "GIT_CONFIG"
 #define CONFIG_LOCAL_ENVIRONMENT "GIT_CONFIG_LOCAL"
 #define EXEC_PATH_ENVIRONMENT "GIT_EXEC_PATH"
+#define INDEX_PREFIX_ENVIRONMENT "GIT_INDEX_PREFIX"
 #define GITATTRIBUTES_FILE ".gitattributes"
 #define INFOATTRIBUTES_FILE "info/attributes"
 #define ATTRIBUTE_MACRO_PREFIX "[attr]"
@@ -318,6 +319,7 @@ extern int set_git_dir(const char *path);
 extern const char *get_git_work_tree(void);
 extern const char *read_gitfile_gently(const char *path);
 extern void set_git_work_tree(const char *tree);
+extern const char *get_index_prefix(void);
 
 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
 
environment.c +12 -0
index 73feb2d..a8edaf5 100644
@@ -46,6 +46,8 @@ enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
 char *git_work_tree_cfg;
 static char *work_tree;
 
+static const char *index_prefix;
+
 static const char *git_dir;
 static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;
 
@@ -71,6 +73,9 @@ static void setup_git_env(void)
 	git_graft_file = getenv(GRAFT_ENVIRONMENT);
 	if (!git_graft_file)
 		git_graft_file = xstrdup(git_path("info/grafts"));
+	index_prefix = getenv(INDEX_PREFIX_ENVIRONMENT);
+	if (index_prefix && (!*index_prefix || index_prefix[strlen(index_prefix)-1] != '/'))
+		die("GIT_INDEX_PREFIX must end with a slash");
 }
 
 int is_bare_repository(void)
@@ -122,6 +127,13 @@ const char *get_git_work_tree(void)
 	return work_tree;
 }
 
+const char *get_index_prefix()
+{
+	if (!git_dir)
+		setup_git_env();
+	return index_prefix;
+}
+
 char *get_object_directory(void)
 {
 	if (!git_object_dir)
read-cache.c +82 -2

133 lines not on this page · raw message

unpack-trees.c +3 -0
index 0de5a31..2ef4827 100644
@@ -403,6 +403,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
 	if (o->trivial_merges_only && o->nontrivial_merge)
 		return unpack_failed(o, "Merge requires file-level merging");
 
+	if (o->check_index_prefix && check_index_prefix(&o->result, o->merge && !o->prefix))
+		return unpack_failed(o, "Merge outside index prefix");
+
 	o->src_index = NULL;
 	if (check_updates(o))
 		return -1;
unpack-trees.h +2 -1
index 94e5672..a1b46f9 100644
@@ -26,7 +26,8 @@ struct unpack_trees_options {
 		     verbose_update:1,
 		     aggressive:1,
 		     skip_unmerged:1,
-		     gently:1;
+		     gently:1,
+		     check_index_prefix:1;
 	const char *prefix;
 	int pos;
 	struct dir_struct *dir;
Signature · 1 line
1.5.5.GIT
#1 lore raw
Quoted 8 lines · Nguyễn Thái Ngọc Duy

GIT_INDEX_PREFIX is used to limit write access to a specific directory. Only "important" information is protected by index prefix (those will be used to create tree objects)

When GIT_INDEX_PREFIX is set, any attempt to modify the index (refresh it is okay though) will bail out. read-tree and merge, however, can write to full index. For merge, no conflict is allowed outside index prefix.

This is kind of hard to judge as part of "narrow checkout" series, because it is not clear how this will actually _help_ narrow checkout.

In other words, as a standalone "protect parts outside a single subdirectory" it can be reviewed and judged, but it is unclear how it would help narrow checkout if you excempted only a _single_ subdirectory. E.g. you might want to limit yourself to arch/x86 _and_ include/asm-x86.

I sympathize that this series will be hard to get right as the places _you identify_ in the code as "touching index entries" are the only places that you _could_ write test scripts for --- if you missed one codepath it would be easy for you to forget writing the test for that codepath.

Having said that...

environment.c static void setup_git_env(void) · line 78

Quoted 7 lines
environment.c +3 -0
@@ -71,6 +73,9 @@ static void setup_git_env(void)
 	git_graft_file = getenv(GRAFT_ENVIRONMENT);
 	if (!git_graft_file)
 		git_graft_file = xstrdup(git_path("info/grafts"));
+	index_prefix = getenv(INDEX_PREFIX_ENVIRONMENT);
+	if (index_prefix && (!*index_prefix || index_prefix[strlen(index_prefix)-1] != '/'))
+		die("GIT_INDEX_PREFIX must end with a slash");

Not nice (aka "why 'must'?").

read-cache.c line 29

Quoted 12 lines
read-cache.c +4 -0
index ac9a8e7..4f8d44b 100644
@@ -23,6 +23,11 @@
 
 struct index_state the_index;
 
+static int outside_index_prefix(const struct index_state *istate, const char *ce_name)
+{
+	return istate == &the_index && get_index_prefix() && prefixcmp(ce_name, get_index_prefix());
+}

The first check above needs to be justified.

If you say "outside of this path are off-limits", why do you allow a temporary index that is used during a partial commit and other index_states excempt from that rule?

read-cache.c static int check_file_directory_conflict(struct index_state *istate, · line 813

Quoted 11 lines
read-cache.c +7 -0
@@ -793,21 +804,35 @@ static int check_file_directory_conflict(struct index_state *istate,
 	return retval + has_dir_name(istate, ce, pos, ok_to_replace);
 }
 
+static int ce_compare(const struct cache_entry *ce1, const struct cache_entry *ce2)
+{
+	return ce1->ce_mode == ce2->ce_mode &&
+		((ce1->ce_flags ^ ce2->ce_flags) & ~(CE_HASHED | CE_UPDATE)) == 0 &&
+		!memcmp(ce1->sha1, ce2->sha1, 20) &&
+		!strcmp(ce1->name, ce2->name);
+}

This needs a bit of commenting to explain why HASHED and UPDATE are specifically ignored, so that people who later add their own bit to the flag can easily tell if their new bit should also be ignored.

Quoted 4 lines
read-cache.c +4 -0
+	cache1 = the_index.cache;
+	cache2 = index->cache;
+	start = 0;
+	end1 = the_index.cache_nr ? the_index.cache_nr - 1 : 0;

read-cache.c int write_index(const struct index_state *istate, int newfd) · line 1450

Quoted 10 lines
read-cache.c +10 -0
+	end2 = index->cache_nr ? index->cache_nr - 1 : 0;
+	while (start < end1 && start < end2 &&
+		ce_compare(cache1[start], cache2[start]))
+		start ++;
+
+	while (end1 > start && end2 > start &&
+		ce_compare(cache1[end1], cache2[end2])) {
+		end1 --;
+		end2 --;
+	}

Style. Drop excess SP before post-increment and -decrement.

read-cache.c int write_index(const struct index_state *istate, int newfd) · line 1459

Quoted 8 lines
read-cache.c +8 -0
+	/*
+	 * everything in start..end1 and start..end2 must
+	 * be prefixed by get_index_prefix()
+	 */
+	if (start < end1 &&
+		(prefixcmp(cache1[start]->name, index_prefix) ||
+		prefixcmp(cache1[end1]->name, index_prefix)))
+		return 1;

It is conventional to return negative or 0 for a function that checks "is this kosher". Returning 0/1 risks getting misunderstood that this returns ok for 1 and bad for 0.

#2 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Nguyen Thai Ngoc Duy

GIT_INDEX_PREFIX is used to limit write access to a specific directory. Only "important" information is protected by index prefix (those will be used to create tree objects)

Quoted 7 lines
Quoted 4 lines

When GIT_INDEX_PREFIX is set, any attempt to modify the index (refresh it is okay though) will bail out. read-tree and merge, however, can write to full index. For merge, no conflict is allowed outside index prefix.

This is kind of hard to judge as part of "narrow checkout" series, because it is not clear how this will actually _help_ narrow checkout.

When you do narrow checkout. You only have a subdirectory, so you would not want some commands to accidentally change things outside that subdirectory in index.

Quoted 4 lines

In other words, as a standalone "protect parts outside a single subdirectory" it can be reviewed and judged, but it is unclear how it would help narrow checkout if you excempted only a _single_ subdirectory. E.g. you might want to limit yourself to arch/x86 _and_ include/asm-x86.

Well it could be extended to support multiple path separated by colon later if someone needs it :)

Quoted 7 lines

I sympathize that this series will be hard to get right as the places _you identify_ in the code as "touching index entries" are the only places that you _could_ write test scripts for --- if you missed one codepath it would be easy for you to forget writing the test for that codepath.

Having said that...

Quoted 9 lines

environment.c static void setup_git_env(void) · line 78

Quoted 7 lines
environment.c +3 -0
@@ -71,6 +73,9 @@ static void setup_git_env(void)
      git_graft_file = getenv(GRAFT_ENVIRONMENT);
      if (!git_graft_file)
              git_graft_file = xstrdup(git_path("info/grafts"));
+     index_prefix = getenv(INDEX_PREFIX_ENVIRONMENT);
+     if (index_prefix && (!*index_prefix || index_prefix[strlen(index_prefix)-1] != '/'))
+             die("GIT_INDEX_PREFIX must end with a slash");

Not nice (aka "why 'must'?").

Simpler handling. Maybe it should append slash by itself if missing.

Quoted 15 lines

read-cache.c line 29

Quoted 12 lines
read-cache.c +4 -0
index ac9a8e7..4f8d44b 100644
@@ -23,6 +23,11 @@
 
 struct index_state the_index;
 
+static int outside_index_prefix(const struct index_state *istate, const char *ce_name)
+{
+     return istate == &the_index && get_index_prefix() && prefixcmp(ce_name, get_index_prefix());
+}

The first check above needs to be justified.

Quoted 3 lines

If you say "outside of this path are off-limits", why do you allow a temporary index that is used during a partial commit and other index_states excempt from that rule?

Because unpack_trees writes new index from scratch so it will always violate that. That check is IMO enough for simple index manipulation like add/remove an entry. For unpack_trees, I have check_index_prefix() to match a temporary index with current index before it gets written to disk.

Yes I did miss partial commit. Did not know it used temporary index too.

Quoted 1 line

[the rest]

OK will do.

Signature · 1 line
Duy
#3 lore raw

Hi,

Quoted 12 lines · Nguyen Thai Ngoc Duy
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Nguyen Thai Ngoc Duy

GIT_INDEX_PREFIX is used to limit write access to a specific directory. Only "important" information is protected by index prefix (those will be used to create tree objects)

When GIT_INDEX_PREFIX is set, any attempt to modify the index (refresh it is okay though) will bail out. read-tree and merge, however, can write to full index. For merge, no conflict is allowed outside index prefix.

Quoted 6 lines
Quoted 2 lines

This is kind of hard to judge as part of "narrow checkout" series, because it is not clear how this will actually _help_ narrow checkout.

When you do narrow checkout. You only have a subdirectory, so you would not want some commands to accidentally change things outside that subdirectory in index.

When they operate purely on the index? Why not?

I guess that Junio's point was that the semantics are not at all clear.

Quoted 8 lines
Quoted 5 lines

In other words, as a standalone "protect parts outside a single subdirectory" it can be reviewed and judged, but it is unclear how it would help narrow checkout if you excempted only a _single_ subdirectory. E.g. you might want to limit yourself to arch/x86 _and_ include/asm-x86.

Well it could be extended to support multiple path separated by colon later if someone needs it :)

The thing is: if the concept is sound, chances are that it is _easy_ to support that, should someone need it.

Quoted 11 lines
Quoted 9 lines

environment.c static void setup_git_env(void) · line 78

Quoted 7 lines
environment.c +3 -0
@@ -71,6 +73,9 @@ static void setup_git_env(void)
      git_graft_file = getenv(GRAFT_ENVIRONMENT);
      if (!git_graft_file)
              git_graft_file = xstrdup(git_path("info/grafts"));
+     index_prefix = getenv(INDEX_PREFIX_ENVIRONMENT);
+     if (index_prefix && (!*index_prefix || index_prefix[strlen(index_prefix)-1] != '/'))
+             die("GIT_INDEX_PREFIX must end with a slash");

Not nice (aka "why 'must'?").

Simpler handling. Maybe it should append slash by itself if missing.

Not maybe. Definitely. That was what the comment was about.

Quoted 19 lines
Quoted 15 lines

read-cache.c line 29

Quoted 12 lines
read-cache.c +4 -0
index ac9a8e7..4f8d44b 100644
@@ -23,6 +23,11 @@
 
 struct index_state the_index;
 
+static int outside_index_prefix(const struct index_state *istate, const char *ce_name)
+{
+     return istate == &the_index && get_index_prefix() && prefixcmp(ce_name, get_index_prefix());
+}

The first check above needs to be justified.

Quoted 3 lines

If you say "outside of this path are off-limits", why do you allow a temporary index that is used during a partial commit and other index_states excempt from that rule?

Quoted 5 lines

Because unpack_trees writes new index from scratch so it will always violate that. That check is IMO enough for simple index manipulation like add/remove an entry. For unpack_trees, I have check_index_prefix() to match a temporary index with current index before it gets written to disk.

unpack_trees() is what drives merges. Does that mean that the only opportunity to end up with conflicts is _not_ prevented?

Ciao,
Dscho

#4 lore raw
Quoted 16 lines · Johannes Schindelin

Hi,

Quoted 12 lines · Nguyen Thai Ngoc Duy
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Nguyen Thai Ngoc Duy

GIT_INDEX_PREFIX is used to limit write access to a specific directory. Only "important" information is protected by index prefix (those will be used to create tree objects)

When GIT_INDEX_PREFIX is set, any attempt to modify the index (refresh it is okay though) will bail out. read-tree and merge, however, can write to full index. For merge, no conflict is allowed outside index prefix.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

This is kind of hard to judge as part of "narrow checkout" series, because it is not clear how this will actually _help_ narrow checkout.

When you do narrow checkout. You only have a subdirectory, so you would not want some commands to accidentally change things outside that subdirectory in index.

When they operate purely on the index? Why not?

They don't. But if they only operate on a subdirectory only, they must operate only on that subdirectory in index (exceptions: git-merge and git-read-tree)

Quoted 2 lines

I guess that Junio's point was that the semantics are not at all clear.

On the second thought, I think this series is not a requirement for narrow checkout. But it does help to
- detect any index violation (from unsupported commands)
- draw an inititial path to index manipulation, needed for narrow
checkout (cache_refresh_ent() will be enhanced not to stat() outside subtree though)

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

In other words, as a standalone "protect parts outside a single subdirectory" it can be reviewed and judged, but it is unclear how it would help narrow checkout if you excempted only a _single_ subdirectory. E.g. you might want to limit yourself to arch/x86 _and_ include/asm-x86.

Quoted 5 lines
Quoted 2 lines

Well it could be extended to support multiple path separated by colon later if someone needs it :)

The thing is: if the concept is sound, chances are that it is _easy_ to support that, should someone need it.

It should be easy except check_index_prefix() for multiple index prefixes.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

environment.c static void setup_git_env(void) · line 78

Quoted 8 lines
environment.c +3 -0
@@ -71,6 +73,9 @@ static void setup_git_env(void)
      git_graft_file = getenv(GRAFT_ENVIRONMENT);
      if (!git_graft_file)
              git_graft_file = xstrdup(git_path("info/grafts"));
+     index_prefix = getenv(INDEX_PREFIX_ENVIRONMENT);
+     if (index_prefix && (!*index_prefix || index_prefix[strlen(index_prefix)-1] != '/'))
+             die("GIT_INDEX_PREFIX must end with a slash");

Not nice (aka "why 'must'?").

Quoted 3 lines
Quoted 1 line

Simpler handling. Maybe it should append slash by itself if missing.

Not maybe. Definitely. That was what the comment was about.

OK. Definitetly :)

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines

read-cache.c line 29

Quoted 13 lines
read-cache.c +4 -0
index ac9a8e7..4f8d44b 100644
@@ -23,6 +23,11 @@
 
 struct index_state the_index;
 
+static int outside_index_prefix(const struct index_state *istate, const char *ce_name)
+{
+     return istate == &the_index && get_index_prefix() && prefixcmp(ce_name, get_index_prefix());
+}

The first check above needs to be justified.

If you say "outside of this path are off-limits", why do you allow a temporary index that is used during a partial commit and other index_states excempt from that rule?

Quoted 8 lines
Quoted 5 lines

Because unpack_trees writes new index from scratch so it will always violate that. That check is IMO enough for simple index manipulation like add/remove an entry. For unpack_trees, I have check_index_prefix() to match a temporary index with current index before it gets written to disk.

unpack_trees() is what drives merges. Does that mean that the only opportunity to end up with conflicts is _not_ prevented?

I am not sure I understand this. In my opinion, due to unpack_trees() behavior, it is free to write to temporary indexes. I don't know if any channce conflicts will be generated without unpack_trees(). But then if an index is to be written to .git/index, it must be checked against .git/index plus index prefix. Any opportunity that ends up with conflicts does not matter, as long as it will not be written to .git/index or conflicts are visible in narrow checkout. Otherwise it must be checked. What I care is that .git/index is controlled, so that people won't be able to change anything outside index prefix by accident.

Quoted 4 lines

Ciao,
Dscho

Signature · 1 line
Duy