Skip to contentSkip to conversation

fast-import: allow more operations on root directory

PATCH In review Dmitry Ivankov 2 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover fast-import: allow more operations on root directory

[1/2] has been already reviewed in [1] where it didn't seem to have any effect, but for this series it does, for 'ls ""' command.
So the change is about allowing to "ls" root directory and to copy or rename it to some subdirectory. The documentation doesn't deny these, but they don't work. Make them work.

More details are in [2/2].

[1] http://thread.gmane.org/gmane.comp.version-control.git/178007/focus=178009

Dmitry Ivankov (2):
fast-import: be saner with temporary trees
fast-import: allow top directory as an argument for some commands

 fast-import.c          |   78 +++++++++++++++++++++++++++++++++++++++++------
 t/t9300-fast-import.sh |   58 +++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+), 10 deletions(-)
Signature · 1 line
1.7.3.4
#1 lore raw

PATCH 1/2 fast-import: be saner with temporary trees

new_tree_entry/release_tree_entry manage a stack of tree_entry structs to use as temporaries. Initializing them is the responsibility of the caller, both after allocation with xmalloc() when existing temporaries are exhausted and after used entries are pushed with release_tree_entry.

parse_ls doesn't set root->versions[0] fields, making root an invalid entry. The problem could arise if store_tree(root) is called. parse_ls calls store_tree on the node corresponding to the path it is called on.

Do initialize entry->versions[0]. As of now, ls command can not list the topmost tree so this change is just to avoid surprises in case things will change around ls or tree_content_get.

---
 fast-import.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
fast-import.c +3 -0
index 7cc2262..3a0aaad 100644
@@ -2951,6 +2951,9 @@ static void parse_ls(struct branch *b)
 	} else {
 		struct object_entry *e = parse_treeish_dataref(&p);
 		root = new_tree_entry();
+		hashclr(root->versions[0].sha1);
+		root->versions[0].mode = 0;
+		root->versions[1].mode = S_IFDIR;
 		hashcpy(root->versions[1].sha1, e->idx.sha1);
 		load_tree(root);
 		if (*p++ != ' ')
Signature · 1 line
1.7.3.4
#2 lore raw

Re: [PATCH 1/2] fast-import: be saner with temporary trees

Hi,

Quoted 3 lines · Dmitry Ivankov

new_tree_entry/release_tree_entry manage a stack of tree_entry structs to use as temporaries. Initializing them is the responsibility of the caller,

[...]

Quoted 6 lines

Do initialize entry->versions[0]. As of now, ls command can not list the topmost tree so this change is just to avoid surprises in case things will change around ls or tree_content_get.

[...]

Review static void parse_ls(struct branch *b) · line 2957

Quoted 9 lines

+++ b/fast-import.c

(quoted diff) +3 -0
@@ -2951,6 +2951,9 @@ static void parse_ls(struct branch *b)
 	} else {
 		struct object_entry *e = parse_treeish_dataref(&p);
 		root = new_tree_entry();
+		hashclr(root->versions[0].sha1);
+		root->versions[0].mode = 0;
+		root->versions[1].mode = S_IFDIR;
 		hashcpy(root->versions[1].sha1, e->idx.sha1);

I don't remember what this patch was about or why I acked it, which is a bad sign. (If the above explanation doesn't make me want to ack it again, how is it going to help someone in the future understand the harm in reverting this patch if some other bug bisects to it?)

I think part of the problem is that the explanation seems to be phrased in terms of implementation details (root->versions[0] fields, calling store_tree, initializing entry->versions[0]) instead of the higher-level logic that these implement.

It sounds ("avoid surprises") like this is meant to avoid scary behavior from the programmer's perspective, so: what are the symptoms without this patch, and why is this a good way to eliminate them?

On the other hand, if we squash this in with David's [1], it starts to make perfect sense. The explanation can say:

There is a pathological Subversion operation that svn-fe handles incorrectly due to an unexpected response from fast-import:

svn cp $SVN_ROOT $SVN_ROOT/subdirectory

When the following command is sent to fast-import:

'ls' SP ':1' SP LF

The expected output is:

'040000' SP 'tree' SP <dataref> HT LF

The actual output is:

'missing' SP LF

This is because tree_content_get() is called but expects a non-empty path. Instead, copy the root entry and force the mode to S_IFDIR.

In the 'ls <dataref> ""' case, this requires some care, because parse_ls currently uses an uninitialized temporary tree entry from the new_tree_entry/release_tree_entry stack in a quick and dirty way without initializing its fields well enough for functions like store_tree that might calculate its tree name.

Sensible? Alternatively, we could rush in David's [1] with the tweak below to dodge the 'ls <dataref> ""' case, and include your fix as part of a patch that just addresses that case.

Thanks very much for the patch and the reminder.

Sincerely,
Jonathan

[1] http://thread.gmane.org/gmane.comp.version-control.git/192517/focus=192520

---
 fast-import.c          |    2 +-
 t/t9300-fast-import.sh |   66 +++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 66 insertions(+), 2 deletions(-)
fast-import.c +1 -1
index 8dbfd4cc..2748ced2 100644
@@ -3019,7 +3019,7 @@ static void parse_ls(struct branch *b)
 			die("Garbage after path in: %s", command_buf.buf);
 		p = uq.buf;
 	}
-	if (*p) {
+	if (*p || root != &b->branch_tree) {
 		tree_content_get(root, p, &leaf);
 	} else {
 		leaf = *root;
t/t9300-fast-import.sh +65 -1

73 lines not on this page · raw message

Signature · 1 line
1.7.9.2
#3 lore raw

PATCH 2/2 fast-import: allow top directory as an argument for some commands

In reply to Dmitry Ivankov

fast-import allows to operate on the toplevel directory. This includes setting the value by sha1/mark, copying/renaming some subdirectory to a toplevel one.

But it's not possible to 'ls' it or copy/rename it so somewhere, or delete it with 'D' command.

For ls it's quite reasonable input so just handle it. The special case is empty directory, for other empty directories we report 'missing' so stick with this behaviour with toplevel too.

For copy/rename it looks like a normal operation too so handle it too. But if it's empty, do nothing - there is nothing to copy/rename and the destination is nonexistent because toplevel is empty.

For 'D ""' we already have deleteall command so probably it's not worth adding one more way to do the same thing.

---
 fast-import.c          |   75 +++++++++++++++++++++++++++++++++++++++++------
 t/t9300-fast-import.sh |   58 +++++++++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+), 10 deletions(-)
fast-import.c +65 -10
index 3a0aaad..8643e23 100644
@@ -1552,6 +1552,31 @@ static int tree_content_set(
 	return 1;
 }
 
+static int tree_content_clear(
+	struct tree_entry *root,
+	struct tree_entry *backup_leaf)
+{
+	if (backup_leaf)
+		memcpy(backup_leaf, root, sizeof(*backup_leaf));
+	else if (root->tree)
+		release_tree_content_recursive(root->tree);
+	root->tree = NULL;
+	hashclr(root->versions[1].sha1);
+	return 1;
+}
+
+static int is_empty_tree(struct tree_entry *root)
+{
+	int n;
+	if (!root->tree)
+		load_tree(root);
+	for (n = 0; n < root->tree->entry_count; n++) {
+		if (root->tree->entries[n]->versions[1].mode)
+			return 0;
+	}
+	return 1;
+}
+
 static int tree_content_remove(
 	struct tree_entry *root,
 	const char *p,
@@ -1587,11 +1612,9 @@ static int tree_content_remove(
 			if (!e->tree)
 				load_tree(e);
 			if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
-				for (n = 0; n < e->tree->entry_count; n++) {
-					if (e->tree->entries[n]->versions[1].mode) {
-						hashclr(root->versions[1].sha1);
-						return 1;
-					}
+				if (!is_empty_tree(e)) {
+					hashclr(root->versions[1].sha1);
+					return 1;
 				}
 				backup_leaf = NULL;
 				goto del_entry;
@@ -1613,6 +1636,19 @@ del_entry:
 	return 1;
 }
 
+static int tree_content_copy_root(
+	struct tree_entry *root,
+	struct tree_entry *leaf
+)
+{
+	memcpy(leaf, root, sizeof(*leaf));
+	if (root->tree && is_null_sha1(root->versions[1].sha1))
+		leaf->tree = dup_tree_content(root->tree);
+	else
+		leaf->tree = NULL;
+	return 1;
+}
+
 static int tree_content_get(
 	struct tree_entry *root,
 	const char *p,
@@ -2329,10 +2365,17 @@ static void file_change_cr(struct branch *b, int rename)
 	}
 
 	memset(&leaf, 0, sizeof(leaf));
-	if (rename)
-		tree_content_remove(&b->branch_tree, s, &leaf);
-	else
-		tree_content_get(&b->branch_tree, s, &leaf);
+	if (rename) {
+		if (!*s)
+			tree_content_clear(&b->branch_tree, &leaf);
+		else
+			tree_content_remove(&b->branch_tree, s, &leaf);
+	} else {
+		if (!*s)
+			tree_content_copy_root(&b->branch_tree, &leaf);
+		else
+			tree_content_get(&b->branch_tree, s, &leaf);
+	}
 	if (!leaf.versions[1].mode)
 		die("Path %s not in branch", s);
 	if (!*d) {	/* C "path/to/subdir" "" */
@@ -2342,6 +2385,14 @@ static void file_change_cr(struct branch *b, int rename)
 			leaf.tree);
 		return;
 	}
+	/*
+	 * Git does not track empty, non-toplevel directories.
+	 * At this point destination is non-toplevel, so if source is
+	 * toplevel and empty, leave it as is all empty.
+	 */
+	if (!*s && is_empty_tree(&leaf)) {
+		return;
+	}
 	tree_content_set(&b->branch_tree, d,
 		leaf.versions[1].sha1,
 		leaf.versions[1].mode,
@@ -2969,7 +3020,11 @@ static void parse_ls(struct branch *b)
 			die("Garbage after path in: %s", command_buf.buf);
 		p = uq.buf;
 	}
-	tree_content_get(root, p, &leaf);
+	if (!*p) {
+		if (!is_empty_tree(root))
+			tree_content_copy_root(root, &leaf);
+	} else
+		tree_content_get(root, p, &leaf);
 	/*
 	 * A directory in preparation would have a sha1 of zero
 	 * until it is saved.  Save, for simplicity.
t/t9300-fast-import.sh +58 -0

64 lines not on this page · raw message

Signature · 1 line
1.7.3.4