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

[PATCH v4] fast-import: treat filemodify with empty tree as delete

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jan 27, 2011, 06:07 UTC
Message-ID
<20110127060749.GA5586@burratino>
In-Reply-To
<7vd3nji54o.fsf@alter.siamese.dyndns.org>

Normal git processes do not allow one to build a tree with an empty subtree entry without trying hard at it. This is in keeping with the general UI philosophy: git tracks content, not empty directories.

v1.7.3-rc0~75^2 (2010-06-30) changed that by making it easy to include an empty subtree in fast-import's active commit:

	M 040000 4b825dc642cb6eb9a060e54bf8d69288fbee4904 subdir

One can trigger this by reading an empty tree (for example, the tree corresponding to an empty root commit) and trying to move it to a subtree. It is better and more closely analogous to 'git read-tree --prefix' to treat such commands as requests to remove the subtree.

Noticed-by: David Barr <david.barr@cordelta.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Junio C Hamano wrote:
> Hmm, why not on top of v1.7.3-rc0~75^2 aka 334fba6 (Teach fast-import to
> import subtrees named by tree id, 2010-06-30) then?

Okay, I found time to try it. Some other small simplifications while at it.

 fast-import.c          |    6 ++++++
 t/t9300-fast-import.sh |   42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 0 deletions(-)
diff --git a/fast-import.c b/fast-import.c
index ad6843a..cd9310d 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -2163,6 +2163,12 @@ static void file_change_m(struct branch *b)
 		p = uq.buf;
 	}
 
+	/* Git does not track empty, non-toplevel directories. */
+	if (S_ISDIR(mode) && !memcmp(sha1, EMPTY_TREE_SHA1_BIN, 20) && *p) {
+		tree_content_remove(&b->branch_tree, p, NULL);
+		return;
+	}
+
 	if (S_ISGITLINK(mode)) {
 		if (inline_data)
 			die("Git links cannot be specified 'inline': %s",
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 50d5913..8487734 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -818,6 +818,48 @@ test_expect_success \
 	 compare_diff_raw expect actual'
 
+test_expect_success \
+	'N: delete directory by copying' \
+	'cat >expect <<-\EOF &&
+	OBJID
+	:100644 000000 OBJID OBJID D	foo/bar/qux
+	OBJID
+	:000000 100644 OBJID OBJID A	foo/bar/baz
+	:000000 100644 OBJID OBJID A	foo/bar/qux
+	EOF
+	 empty_tree=$(git mktree </dev/null) &&
+	 cat >input <<-INPUT_END &&
+	commit refs/heads/N-delete
+	committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+	data <<COMMIT
+	collect data to be deleted
+	COMMIT
+
+	deleteall
+	M 100644 inline foo/bar/baz
+	data <<DATA_END
+	hello
+	DATA_END
+	C "foo/bar/baz" "foo/bar/qux"
+	C "foo/bar/baz" "foo/bar/quux/1"
+	C "foo/bar/baz" "foo/bar/quuux"
+	M 040000 $empty_tree foo/bar/quux
+	M 040000 $empty_tree foo/bar/quuux
+
+	commit refs/heads/N-delete
+	committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+	data <<COMMIT
+	delete subdirectory
+	COMMIT
+
+	M 040000 $empty_tree foo/bar/qux
+	INPUT_END
+	 git fast-import <input &&
+	 git rev-list N-delete |
+		git diff-tree -r --stdin --root --always |
+		sed -e "s/$_x40/OBJID/g" >actual &&
+	 test_cmp expect actual'
+
 test_expect_success \
 	'N: modify copied tree' \
 	'cat >expect <<-\EOF &&
 	:100644 100644 fcf778cda181eaa1cbc9e9ce3a2e15ee9f9fe791 fcf778cda181eaa1cbc9e9ce3a2e15ee9f9fe791 C100	newdir/interesting	file3/file5
-- 
1.7.4.rc3
Previous: Jonathan NiederNext: Peter Baumann
Message 16 of 24 in “fast-import: add 'ls' command”
  1. fast-import: add 'ls' commandDavid Barr, Dec 2, 2010
  2. fast-import: add 'ls' commandDavid Barr, Dec 2, 2010
  3. Sverre RabbelierDec 2, 2010
  4. David Michael BarrDec 2, 2010
  5. Jonathan NiederDec 2, 2010
  6. Junio C HamanoDec 2, 2010
  7. David BarrDec 2, 2010
  8. 0/3 fast-import: add 'ls' commandJonathan Nieder, Jan 3, 2011
  9. 1/3 fast-import: clarify handling of cat-blob featureJonathan Nieder, Jan 3, 2011
  10. 2/3 fast-import: treat filemodify with empty tree as deleteJonathan Nieder, Jan 3, 2011
  11. fast-import: treat filemodify with empty tree as deleteJonathan Nieder, Jan 26, 2011
  12. Sverre RabbelierJan 26, 2011
  13. fast-import: treat filemodify with empty tree as deleteJonathan Nieder, Jan 26, 2011
  14. Junio C HamanoJan 27, 2011
  15. Jonathan NiederJan 27, 2011
  16. fast-import: treat filemodify with empty tree as deleteJonathan Nieder, Jan 27, 2011
  17. Peter BaumannJan 27, 2011
  18. Jonathan NiederJan 27, 2011
  19. Peter BaumannJan 27, 2011
  20. Peter BaumannJan 27, 2011
  21. Jonathan NiederJan 28, 2011
  22. 3/3 fast-import: add 'ls' commandJonathan Nieder, Jan 3, 2011
  23. [RFC] fast-import: 'cat-blob' and 'ls' commandsJonathan Nieder, Jan 26, 2011
  24. Sam VilainJan 26, 2011

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.