threads / patch / 36919

patch, 14 partsAdd submodule test harness

Subject: [PATCH 00/14] Add submodule test harness

## tl;dr

65 messages between Jun 15, 2014 and Jul 14, 2014. Diffs are folded; open one to read it.

replies: 64people: 10as markdown or json

Jens Lehmann· Jun 15, 2014, 16:56 UTC · lore

This is the series I sought feedback for some time ago in gmane/$245048. It took longer than I expected to finish this series because I had to diagnose the test failures I saw with some of the ten commands I added since then. They boiled down to four different inconsistencies in Git's handling of new or removed submodules. Sometimes no empty directory is created for a new submodule. stash and cherry-pick seem to use wrong submodule ignore settings ("all" instead of "dirty"), so they overlook submodule changes. And merge behaves strange when a submodule is removed, as it tries to merge new tracked files with those remaining from the submodule. This series only documents these issues (which most probably didn't cause any real world problems until now because adding and removing submodules wasn't well supported anyway).

The first patch adds a simple helper function to the test lib which makes it easier to test for an empty submodule directory.

The second patch contains the heavy lifting, it adds the test framework for switching submodules. Currently only transitions without merge conflicts are tested for, I intend to add others producing merge conflicts in a follow-up series.

The other twelve patches use the framework to test all relevant work tree manipulating commands. In addition to the two general problems documented in the second patch some patches add known failures for problems of some commands/actions to be fixed in later patches.

The only work tree changing command I didn't cover here is checkout-index, as that updates the work tree to the content of the index but doesn't remove any formerly tracked files, which means it doesn't do a full transition from one commit to another. If I overlooked another command, please speak up so I can include it too.

Jens Lehmann (14):
  test-lib: add test_dir_is_empty()
  submodules: Add the lib-submodule-update.sh test library
  checkout: call the new submodule update test framework
  apply: add t4137 for submodule updates
  read-tree: add t1013 for submodule updates
  reset: add t7112 for submodule updates
  bisect: add t6041 for submodule updates
  merge: add t7613 for submodule updates
  rebase: add t3426 for submodule updates
  pull: add t5572 for submodule updates
  cherry-pick: add t3512 for submodule updates
  am: add t4255 for submodule updates
  stash: add t3906 for submodule updates
  revert: add t3513 for submodule updates
 t/lib-submodule-update.sh        | 670 +++++++++++++++++++++++++++++++++++++++
 t/t1013-read-tree-submodule.sh   |  12 +
 t/t2013-checkout-submodule.sh    |   5 +
 t/t3426-rebase-submodule.sh      |  46 +++
 t/t3512-cherry-pick-submodule.sh |  13 +
 t/t3513-revert-submodule.sh      |  32 ++
 t/t3906-stash-submodule.sh       |  24 ++
 t/t4137-apply-submodule.sh       |  20 ++
 t/t4255-am-submodule.sh          |  21 ++
 t/t5572-pull-submodule.sh        |  74 +++++
 t/t6041-bisect-submodule.sh      |  32 ++
 t/t7112-reset-submodule.sh       |  14 +
 t/t7613-merge-submodule.sh       |  19 ++
 t/test-lib-functions.sh          |  11 +
 14 files changed, 993 insertions(+)
 create mode 100755 t/lib-submodule-update.sh
 create mode 100755 t/t1013-read-tree-submodule.sh
 create mode 100755 t/t3426-rebase-submodule.sh
 create mode 100755 t/t3512-cherry-pick-submodule.sh
 create mode 100755 t/t3513-revert-submodule.sh
 create mode 100755 t/t3906-stash-submodule.sh
 create mode 100755 t/t4137-apply-submodule.sh
 create mode 100755 t/t4255-am-submodule.sh
 create mode 100755 t/t5572-pull-submodule.sh
 create mode 100755 t/t6041-bisect-submodule.sh
 create mode 100755 t/t7112-reset-submodule.sh
 create mode 100755 t/t7613-merge-submodule.sh
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 15, 2014, 16:57 UTC · re: Jens Lehmann · lore

[PATCH 01/14] test-lib: add test_dir_is_empty()

For the upcoming submodule test framework we often need to assert that an empty directory exists in the work tree. Add the test_dir_is_empty() function which asserts that the given argument is an empty directory.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/test-lib-functions.sh | 11 +++++++++++
 1 file changed, 11 insertions(+)
Show changes to t/test-lib-functions.sh +11 −0
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 158e10a..546f0a6 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -489,6 +489,17 @@ test_path_is_dir () {
 	fi
 }

+# Check if the directory exists and is empty as expected, barf otherwise.
+test_dir_is_empty () {
+	test_path_is_dir "$1" &&
+	if test "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
+	then
+		echo "Directory '$1' is not empty, it contains:"
+		ls -la "$1"
+		return 1
+	fi
+}
+
 test_path_is_missing () {
 	if [ -e "$1" ]
 	then
-- 
2.0.0.275.gc479268
Junio C Hamano· Jun 16, 2014, 22:05 UTC · re: Jens Lehmann · lore

Re: [PATCH 01/14] test-lib: add test_dir_is_empty()

Jens Lehmann <Jens.Lehmann@web.de> writes:
Show 21 quoted lines
> For the upcoming submodule test framework we often need to assert that an
> empty directory exists in the work tree. Add the test_dir_is_empty()
> function which asserts that the given argument is an empty directory.
>
> Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
> ---
>  t/test-lib-functions.sh | 11 +++++++++++
>  1 file changed, 11 insertions(+)
>
> diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
> index 158e10a..546f0a6 100644
> --- a/t/test-lib-functions.sh
> +++ b/t/test-lib-functions.sh
> @@ -489,6 +489,17 @@ test_path_is_dir () {
>  	fi
>  }
>
> +# Check if the directory exists and is empty as expected, barf otherwise.
> +test_dir_is_empty () {
> +	test_path_is_dir "$1" &&
> +	if test "$(ls -a1 "$1" | egrep -v '^\.\.?$')"

It is safer to say "test -n" when testing an emptyness of a string whose contents you do not know or control (e.g. the string may begin with "-something").

Show 10 quoted lines
> +	then
> +		echo "Directory '$1' is not empty, it contains:"
> +		ls -la "$1"
> +		return 1
> +	fi
> +}
> +
>  test_path_is_missing () {
>  	if [ -e "$1" ]
>  	then
Jens Lehmann· Jun 17, 2014, 16:47 UTC · re: Junio C Hamano · lore

Re: [PATCH 01/14] test-lib: add test_dir_is_empty()

Am 17.06.2014 00:05, schrieb Junio C Hamano:
Show 27 quoted lines
> Jens Lehmann <Jens.Lehmann@web.de> writes:
> 
>> For the upcoming submodule test framework we often need to assert that an
>> empty directory exists in the work tree. Add the test_dir_is_empty()
>> function which asserts that the given argument is an empty directory.
>>
>> Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
>> ---
>>  t/test-lib-functions.sh | 11 +++++++++++
>>  1 file changed, 11 insertions(+)
>>
>> diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
>> index 158e10a..546f0a6 100644
>> --- a/t/test-lib-functions.sh
>> +++ b/t/test-lib-functions.sh
>> @@ -489,6 +489,17 @@ test_path_is_dir () {
>>  	fi
>>  }
>>
>> +# Check if the directory exists and is empty as expected, barf otherwise.
>> +test_dir_is_empty () {
>> +	test_path_is_dir "$1" &&
>> +	if test "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
> 
> It is safer to say "test -n" when testing an emptyness of a string
> whose contents you do not know or control (e.g. the string may begin
> with "-something").
Yup, will do so in v2.
Show 11 quoted lines
>> +	then
>> +		echo "Directory '$1' is not empty, it contains:"
>> +		ls -la "$1"
>> +		return 1
>> +	fi
>> +}
>> +
>>  test_path_is_missing () {
>>  	if [ -e "$1" ]
>>  	then
> 
Jens Lehmann· Jun 19, 2014, 20:12 UTC · re: Jens Lehmann · lore

[PATCH v2 01/14] test-lib: add test_dir_is_empty()

For the upcoming submodule test framework we often need to assert that an empty directory exists in the work tree. Add the test_dir_is_empty() function which asserts that the given argument is an empty directory.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
Changes to v1:
*) Added "-n" to test expression (you have this as "SQUASH???" commit in pu)
 t/test-lib-functions.sh | 11 +++++++++++
 1 file changed, 11 insertions(+)
Show changes to t/test-lib-functions.sh +11 −0
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index c617c82..acd9a55 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -489,6 +489,17 @@ test_path_is_dir () {
 	fi
 }

+# Check if the directory exists and is empty as expected, barf otherwise.
+test_dir_is_empty () {
+	test_path_is_dir "$1" &&
+	if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')"
+	then
+		echo "Directory '$1' is not empty, it contains:"
+		ls -la "$1"
+		return 1
+	fi
+}
+
 test_path_is_missing () {
 	if [ -e "$1" ]
 	then
-- 
2.0.0.406.gf4dce28
Jens Lehmann· Jun 15, 2014, 16:58 UTC · re: Jens Lehmann · lore

[PATCH 02/14] submodules: Add the lib-submodule-update.sh test library

Add this test library to simplify covering all combinations of submodule update scenarios without having to add those to a test of each work tree manipulating command over and over again.

The functions test_submodule_switch() and test_submodule_forced_switch() are intended to be called from a test script with a single argument. This argument is either a work tree manipulating command (including any command line options) or a function (when more than a single git command is needed to switch work trees from the current HEAD to another commit). This command (or function) is passed a target branch as argument. The two new functions check that each submodule transition is handled as expected, which currently means that submodule work trees are not affected until "git submodule update" is called. The "forced" variant is for commands using their '-f' or '--hard' option and expects them to overwrite local modifications as a result. Each of these two functions contains 14 tests_expect_* calls.

Calling one of these test functions the first time creates a repository named "submodule_update_repo". At first it contains two files, then a single submodule is added in another commit followed by commits covering all relevant submodule modifications. This repository is newly cloned into the "submodule_update" for each test_expect_* to avoid interference between different parts of the test functions (some to-be-tested commands also manipulate refs along with the work tree, e.g. "git reset").

Follow-up commits will then call these two test functions for all work tree manipulating commands (with a combination of all their options relevant to what they do with the work tree) making sure they work as expected. Later this test library will be extended to cover merges resulting in conflicts too. Also it is intended to be easily extendable for the recursive update functionality, where even more combinations of submodule modifications have to be tested for.

This version documents two bugs in current Git with expected failures:
*) When a submodule is replaced with a tracked file of the same name the
   submodule work tree including any local modifications (and even the
   whole history if it uses a .git directory instead of a gitfile!) is
   silently removed.
*) Forced work tree updates happily manipulate files in the directory of a
   submodule that has just been removed in the superproject (but is of
   course still present in the work tree due to the way submodules are
   currently handled). This becomes dangerous when files in the submodule
   directory are overwritten by files from the new superproject commit, as
   any modifications to the submodule files will be lost) and is expected
   to also destroy history in the - admittedly unlikely case - the new
   commit adds a file named ".git" to the submodule directory.
Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/lib-submodule-update.sh | 630 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 630 insertions(+)
 create mode 100755 t/lib-submodule-update.sh
Show changes to t/lib-submodule-update.sh +630 −0
diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
new file mode 100755
index 0000000..c6c842a
--- /dev/null
+++ b/t/lib-submodule-update.sh
@@ -0,0 +1,630 @@
+# Create a submodule layout used for all tests below.
+#
+# The following use cases are covered:
+# - New submodule (no_submodule => add_sub1)
+# - Removed submodule (add_sub1 => remove_sub1)
+# - Updated submodule (add_sub1 => modify_sub1)
+# - Submodule updated to invalid commit (add_sub1 => invalid_sub1)
+# - Submodule updated from invalid commit (invalid_sub1 => valid_sub1)
+# - Submodule replaced by tracked files in directory (add_sub1 =>
+#   replace_sub1_with_directory)
+# - Directory containing tracked files replaced by submodule
+#   (replace_sub1_with_directory => replace_directory_with_sub1)
+# - Submodule replaced by tracked file with the same name (add_sub1 =>
+#   replace_sub1_with_file)
+# - Tracked file replaced by submodule (replace_sub1_with_file =>
+#   replace_file_with_sub1)
+#
+#                   --O-----O
+#                  /  ^     replace_directory_with_sub1
+#                 /   replace_sub1_with_directory
+#                /----O
+#               /     ^
+#              /      modify_sub1
+#      O------O-------O
+#      ^      ^\      ^
+#      |      | \     remove_sub1
+#      |      |  -----O-----O
+#      |      |   \   ^     replace_file_with_sub1
+#      |      |    \  replace_sub1_with_file
+#      |   add_sub1 --O-----O
+# no_submodule        ^     valid_sub1
+#                     invalid_sub1
+#
+create_lib_submodule_repo () {
+	git init submodule_update_repo &&
+	(
+		cd submodule_update_repo &&
+		echo "expect" >>.gitignore &&
+		echo "actual" >>.gitignore &&
+		echo "x" >file1 &&
+		echo "y" >file2 &&
+		git add .gitignore file1 file2 &&
+		git commit -m "Base" &&
+		git branch "no_submodule" &&
+
+		git checkout -b "add_sub1" &&
+		git submodule add ./. sub1 &&
+		git config -f .gitmodules submodule.sub1.ignore all &&
+		git config submodule.sub1.ignore all &&
+		git add .gitmodules &&
+		git commit -m "Add sub1" &&
+		git checkout -b remove_sub1 &&
+		git revert HEAD &&
+
+		git checkout -b "modify_sub1" "add_sub1" &&
+		git submodule update &&
+		(
+			cd sub1 &&
+			git fetch &&
+			git checkout -b "modifications" &&
+			echo "z" >file2 &&
+			echo "x" >file3 &&
+			git add file2 file3 &&
+			git commit -m "modified file2 and added file3" &&
+			git push origin modifications
+		) &&
+		git add sub1 &&
+		git commit -m "Modify sub1" &&
+
+		git checkout -b "replace_sub1_with_directory" "add_sub1" &&
+		git submodule update &&
+		(
+			cd sub1 &&
+			git checkout modifications
+		) &&
+		git rm --cached sub1 &&
+		rm sub1/.git* &&
+		git config -f .gitmodules --remove-section "submodule.sub1" &&
+		git add .gitmodules sub1/* &&
+		git commit -m "Replace sub1 with directory" &&
+		git checkout -b replace_directory_with_sub1 &&
+		git revert HEAD &&
+
+		git checkout -b "replace_sub1_with_file" "add_sub1" &&
+		git rm sub1 &&
+		echo "content" >sub1 &&
+		git add sub1 &&
+		git commit -m "Replace sub1 with file" &&
+		git checkout -b replace_file_with_sub1 &&
+		git revert HEAD &&
+
+		git checkout -b "invalid_sub1" "add_sub1" &&
+		git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 sub1 &&
+		git commit -m "Invalid sub1 commit" &&
+		git checkout -b valid_sub1 &&
+		git revert HEAD &&
+		git checkout master
+	)
+}
+
+# Helper function to replace gitfile with .git directory
+replace_gitfile_with_git_dir () {
+	(
+		cd "$1" &&
+		git_dir="$(git rev-parse --git-dir)" &&
+		rm -f .git &&
+		cp -a "$git_dir" .git &&
+		GIT_WORK_TREE=. git config --unset core.worktree
+	)
+}
+
+# Test that the .git directory in the submodule is unchanged (except for the
+# core.worktree setting)
+test_git_directory_is_unchanged () {
+	(
+		cd "$1" &&
+		git config core.worktree "../../../$1"
+	) &&
+	git diff -r ".git/modules/$1" "$1/.git" &&
+	(
+		cd "$1" &&
+		GIT_WORK_TREE=. git config --unset core.worktree
+	)
+}
+
+# Helper function to be executed at the start of every test below, it sets up
+# the submodule repo if it doesn't exist and configures the most problematic
+# settings for diff.ignoreSubmodules.
+prolog () {
+	(test -d submodule_update_repo || create_lib_submodule_repo) &&
+	test_config_global diff.ignoreSubmodules all &&
+	test_config diff.ignoreSubmodules all
+}
+
+# Helper function to bring work tree back into the state given by the
+# commit. This includes trying to populate sub1 accordingly if it exists and
+# should be updated to an existing commit.
+reset_work_tree_to () {
+	rm -rf submodule_update &&
+	git clone submodule_update_repo submodule_update &&
+	(
+		cd submodule_update &&
+		rm -rf sub1 &&
+		git checkout -f "$1" &&
+		git status -u -s >actual &&
+		test_must_be_empty actual &&
+		sha1=$(git ls-tree HEAD "sub1" 2>/dev/null | grep 160000 | tr '\t' ' ' | cut -d ' ' -f3) &&
+		if test -n "$sha1" &&
+		   test $(cd "sub1" && git rev-parse --verify "$sha1^{commit}")
+		then
+			git submodule update --init --recursive "sub1"
+		fi
+	)
+}
+
+# Test that the superproject contains the content according to commit "$1"
+# (the work tree must match the index for everything but submodules but the
+# index must exactly match the given commit including any submodule SHA-1s).
+test_superproject_content () {
+	git diff-index --cached "$1" >actual &&
+	test_must_be_empty actual &&
+	git diff-files --ignore-submodules >actual &&
+	test_must_be_empty actual
+}
+
+# Test that the given submodule at path "$1" contains the content according
+# to the submodule commit recorded in the superproject's commit "$2"
+test_submodule_content () {
+	if test $# != 2
+	then
+		echo "test_submodule_content needs two arguments"
+		return 1
+	fi &&
+	submodule="$1" &&
+	commit="$2" &&
+	test -d "$submodule"/ &&
+	if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
+	then
+		echo "Submodule $submodule is not populated"
+		return 1
+	fi &&
+	sha1=$(git ls-tree "$commit" "$submodule" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f3) &&
+	if test -z "$sha1"
+	then
+		echo "Couldn't retrieve SHA-1 of $submodule for $commit"
+		return 1
+	fi &&
+	(
+		cd "$submodule" &&
+		git status -u -s >actual &&
+		test_must_be_empty actual &&
+		git diff "$sha1" >actual &&
+		test_must_be_empty actual
+	)
+}
+
+# Test that the following transitions are correctly handled:
+# - Updated submodule
+# - New submodule
+# - Removed submodule
+# - Directory containing tracked files replaced by submodule
+# - Submodule replaced by tracked files in directory
+# - Submodule replaced by tracked file with the same name
+# - tracked file replaced by submodule
+#
+# The default is that submodule contents aren't changed until "git submodule
+# update" is run. And even then that command doesn't delete the work tree of
+# a removed submodule.
+#
+# Removing a submodule containing a .git directory must fail even when forced
+# to protect the history!
+#
+
+# Test that submodule contents are currently not updated when switching
+# between commits that change a submodule.
+test_submodule_switch () {
+	command="$1"
+	######################### Appearing submodule #########################
+	# Switching to a commit letting a submodule appear creates empty dir ...
+	test_expect_success "$command: added submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... and doesn't care if it already exists ...
+	test_expect_success "$command: added submodule leaves existing empty directory alone" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			mkdir sub1 &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... unless there is an untracked file in its place.
+	test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			echo -n >sub1 &&
+			test_must_fail $command add_sub1 &&
+			test_superproject_content origin/no_submodule &&
+			test_must_be_empty sub1
+		)
+	'
+	# Replacing a tracked file with a submodule produces an empty
+	# directory ...
+	test_expect_success "$command: replace tracked file with submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_file &&
+		(
+			cd submodule_update &&
+			git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
+			$command replace_file_with_sub1 &&
+			test_superproject_content origin/replace_file_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_file_with_sub1
+		)
+	'
+	# ... as does removing a directory with tracked files with a
+	# submodule.
+	test_expect_success "$command: replace directory with submodule" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_directory &&
+		(
+			cd submodule_update &&
+			git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
+			$command replace_directory_with_sub1 &&
+			test_superproject_content origin/replace_directory_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_directory_with_sub1
+		)
+	'
+
+	######################## Disappearing submodule #######################
+	# Removing a submodule doesn't remove its work tree ...
+	test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			replace_gitfile_with_git_dir sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1
+		)
+	'
+	# Replacing a submodule with files in a directory must fail as the
+	# submodule work tree isn't removed ...
+	test_expect_success "$command: replace submodule with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1
+		)
+	'
+	# Replacing it with a file must fail as it could throw away any local
+	# work tree changes ...
+	test_expect_failure "$command: replace submodule with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... or even destroy unpushed parts of submodule history if that
+	# still uses a .git directory.
+	test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1
+		)
+	'
+
+	########################## Modified submodule #########################
+	# Updating a submodule sha1 doesn't update the submodule's work tree
+	test_expect_success "$command: modified submodule does not update submodule work tree" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t modify_sub1 origin/modify_sub1 &&
+			$command modify_sub1 &&
+			test_superproject_content origin/modify_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			git submodule update &&
+			test_submodule_content sub1 origin/modify_sub1
+		)
+	'
+
+	# Updating a submodule to an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will fail
+	test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t invalid_sub1 origin/invalid_sub1 &&
+			$command invalid_sub1 &&
+			test_superproject_content origin/invalid_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_must_fail git submodule update &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Updating a submodule from an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will succeed
+	test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
+		prolog &&
+		reset_work_tree_to invalid_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t valid_sub1 origin/valid_sub1 &&
+			$command valid_sub1 &&
+			test_superproject_content origin/valid_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/valid_sub1
+		)
+	'
+}
+
+# Test that submodule contents are currently not updated when switching
+# between commits that change a submodule, but throwing away local changes in
+# the superproject is allowed.
+test_submodule_forced_switch () {
+	command="$1"
+	######################### Appearing submodule #########################
+	# Switching to a commit letting a submodule appear creates empty dir ...
+	test_expect_success "$command: added submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... and doesn't care if it already exists ...
+	test_expect_success "$command: added submodule leaves existing empty directory alone" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			mkdir sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... unless there is an untracked file in its place.
+	test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			echo -n >sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1
+		)
+	'
+	# Replacing a tracked file with a submodule produces an empty
+	# directory ...
+	test_expect_success "$command: replace tracked file with submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_file &&
+		(
+			cd submodule_update &&
+			git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
+			$command replace_file_with_sub1 &&
+			test_superproject_content origin/replace_file_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_file_with_sub1
+		)
+	'
+	# ... as does removing a directory with tracked files with a
+	# submodule.
+	test_expect_success "$command: replace directory with submodule" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_directory &&
+		(
+			cd submodule_update &&
+			git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
+			$command replace_directory_with_sub1 &&
+			test_superproject_content origin/replace_directory_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_directory_with_sub1
+		)
+	'
+
+	######################## Disappearing submodule #######################
+	# Removing a submodule doesn't remove its work tree ...
+	test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			replace_gitfile_with_git_dir sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1
+		)
+	'
+	# Replacing a submodule with files in a directory must fail as the
+	# submodule work tree isn't removed ...
+	test_expect_failure "$command: replace submodule with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_failure "$command: replace submodule containing a .git directory with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1
+		)
+	'
+	# Replacing it with a file must fail as it could throw away any local
+	# work tree changes ...
+	test_expect_failure "$command: replace submodule with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... or even destroy unpushed parts of submodule history if that
+	# still uses a .git directory.
+	test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1
+		)
+	'
+
+	########################## Modified submodule #########################
+	# Updating a submodule sha1 doesn't update the submodule's work tree
+	test_expect_success "$command: modified submodule does not update submodule work tree" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t modify_sub1 origin/modify_sub1 &&
+			$command modify_sub1 &&
+			test_superproject_content origin/modify_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			git submodule update &&
+			test_submodule_content sub1 origin/modify_sub1
+		)
+	'
+	# Updating a submodule to an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will fail
+	test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t invalid_sub1 origin/invalid_sub1 &&
+			$command invalid_sub1 &&
+			test_superproject_content origin/invalid_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_must_fail git submodule update &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Updating a submodule from an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will succeed
+	test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
+		prolog &&
+		reset_work_tree_to invalid_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t valid_sub1 origin/valid_sub1 &&
+			$command valid_sub1 &&
+			test_superproject_content origin/valid_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/valid_sub1
+		)
+	'
+}
-- 
2.0.0.275.gc479268
Junio C Hamano· Jun 16, 2014, 22:49 UTC · re: Jens Lehmann · lore

Re: [PATCH 02/14] submodules: Add the lib-submodule-update.sh test library

Jens Lehmann <Jens.Lehmann@web.de> writes:
Show 108 quoted lines
> Add this test library to simplify covering all combinations of submodule
> update scenarios without having to add those to a test of each work tree
> manipulating command over and over again.
>
> The functions test_submodule_switch() and test_submodule_forced_switch()
> are intended to be called from a test script with a single argument. This
> argument is either a work tree manipulating command (including any command
> line options) or a function (when more than a single git command is needed
> to switch work trees from the current HEAD to another commit). This
> command (or function) is passed a target branch as argument. The two new
> functions check that each submodule transition is handled as expected,
> which currently means that submodule work trees are not affected until
> "git submodule update" is called. The "forced" variant is for commands
> using their '-f' or '--hard' option and expects them to overwrite local
> modifications as a result. Each of these two functions contains 14
> tests_expect_* calls.
>
> Calling one of these test functions the first time creates a repository
> named "submodule_update_repo". At first it contains two files, then a
> single submodule is added in another commit followed by commits covering
> all relevant submodule modifications. This repository is newly cloned into
> the "submodule_update" for each test_expect_* to avoid interference
> between different parts of the test functions (some to-be-tested commands
> also manipulate refs along with the work tree, e.g. "git reset").
>
> Follow-up commits will then call these two test functions for all work
> tree manipulating commands (with a combination of all their options
> relevant to what they do with the work tree) making sure they work as
> expected. Later this test library will be extended to cover merges
> resulting in conflicts too. Also it is intended to be easily extendable
> for the recursive update functionality, where even more combinations of
> submodule modifications have to be tested for.
>
> This version documents two bugs in current Git with expected failures:
>
> *) When a submodule is replaced with a tracked file of the same name the
>    submodule work tree including any local modifications (and even the
>    whole history if it uses a .git directory instead of a gitfile!) is
>    silently removed.
>
> *) Forced work tree updates happily manipulate files in the directory of a
>    submodule that has just been removed in the superproject (but is of
>    course still present in the work tree due to the way submodules are
>    currently handled). This becomes dangerous when files in the submodule
>    directory are overwritten by files from the new superproject commit, as
>    any modifications to the submodule files will be lost) and is expected
>    to also destroy history in the - admittedly unlikely case - the new
>    commit adds a file named ".git" to the submodule directory.
>
> Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
> ---
>  t/lib-submodule-update.sh | 630 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 630 insertions(+)
>  create mode 100755 t/lib-submodule-update.sh
>
> diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
> new file mode 100755
> index 0000000..c6c842a
> --- /dev/null
> +++ b/t/lib-submodule-update.sh
> @@ -0,0 +1,630 @@
> +# Create a submodule layout used for all tests below.
> +#
> +# The following use cases are covered:
> +# - New submodule (no_submodule => add_sub1)
> +# - Removed submodule (add_sub1 => remove_sub1)
> +# - Updated submodule (add_sub1 => modify_sub1)
> +# - Submodule updated to invalid commit (add_sub1 => invalid_sub1)
> +# - Submodule updated from invalid commit (invalid_sub1 => valid_sub1)
> +# - Submodule replaced by tracked files in directory (add_sub1 =>
> +#   replace_sub1_with_directory)
> +# - Directory containing tracked files replaced by submodule
> +#   (replace_sub1_with_directory => replace_directory_with_sub1)
> +# - Submodule replaced by tracked file with the same name (add_sub1 =>
> +#   replace_sub1_with_file)
> +# - Tracked file replaced by submodule (replace_sub1_with_file =>
> +#   replace_file_with_sub1)
> +#
> +#                   --O-----O
> +#                  /  ^     replace_directory_with_sub1
> +#                 /   replace_sub1_with_directory
> +#                /----O
> +#               /     ^
> +#              /      modify_sub1
> +#      O------O-------O
> +#      ^      ^\      ^
> +#      |      | \     remove_sub1
> +#      |      |  -----O-----O
> +#      |      |   \   ^     replace_file_with_sub1
> +#      |      |    \  replace_sub1_with_file
> +#      |   add_sub1 --O-----O
> +# no_submodule        ^     valid_sub1
> +#                     invalid_sub1
> +#
> +create_lib_submodule_repo () {
> +	git init submodule_update_repo &&
> +	(
> +		cd submodule_update_repo &&
> +		echo "expect" >>.gitignore &&
> +		echo "actual" >>.gitignore &&
> +		echo "x" >file1 &&
> +		echo "y" >file2 &&
> +		git add .gitignore file1 file2 &&
> +		git commit -m "Base" &&
> +		git branch "no_submodule" &&
> +
> +		git checkout -b "add_sub1" &&
> +		git submodule add ./. sub1 &&

This is not technically wrong per-se, but having the project's history itself as its own submodule *is* something nobody sane would do in the real life. Do we really have to do it this unusual way?

Show 60 quoted lines
> +		git config -f .gitmodules submodule.sub1.ignore all &&
> +		git config submodule.sub1.ignore all &&
> +		git add .gitmodules &&
> +		git commit -m "Add sub1" &&
> +		git checkout -b remove_sub1 &&
> +		git revert HEAD &&
> +
> +		git checkout -b "modify_sub1" "add_sub1" &&
> +		git submodule update &&
> +		(
> +			cd sub1 &&
> +			git fetch &&
> +			git checkout -b "modifications" &&
> +			echo "z" >file2 &&
> +			echo "x" >file3 &&
> +			git add file2 file3 &&
> +			git commit -m "modified file2 and added file3" &&
> +			git push origin modifications
> +		) &&
> +		git add sub1 &&
> +		git commit -m "Modify sub1" &&
> +
> +		git checkout -b "replace_sub1_with_directory" "add_sub1" &&
> +		git submodule update &&
> +		(
> +			cd sub1 &&
> +			git checkout modifications
> +		) &&
> +		git rm --cached sub1 &&
> +		rm sub1/.git* &&
> +		git config -f .gitmodules --remove-section "submodule.sub1" &&
> +		git add .gitmodules sub1/* &&
> +		git commit -m "Replace sub1 with directory" &&
> +		git checkout -b replace_directory_with_sub1 &&
> +		git revert HEAD &&
> +
> +		git checkout -b "replace_sub1_with_file" "add_sub1" &&
> +		git rm sub1 &&
> +		echo "content" >sub1 &&
> +		git add sub1 &&
> +		git commit -m "Replace sub1 with file" &&
> +		git checkout -b replace_file_with_sub1 &&
> +		git revert HEAD &&
> +
> +		git checkout -b "invalid_sub1" "add_sub1" &&
> +		git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 sub1 &&
> +		git commit -m "Invalid sub1 commit" &&
> +		git checkout -b valid_sub1 &&
> +		git revert HEAD &&
> +		git checkout master
> +	)
> +}
> +
> +# Helper function to replace gitfile with .git directory
> +replace_gitfile_with_git_dir () {
> +	(
> +		cd "$1" &&
> +		git_dir="$(git rev-parse --git-dir)" &&
> +		rm -f .git &&
> +		cp -a "$git_dir" .git &&
We avoid "cp -a" for portability, don't we?
> +		GIT_WORK_TREE=. git config --unset core.worktree

Hmph. What does GIT_WORK_TREE=. alone without GIT_DIR=<somewhere> do? It's not like it is a workaround for "git config" that complains when you do not have a working tree, right? Puzzled...

Show 11 quoted lines
> +	)
> +}
> +
> +# Test that the .git directory in the submodule is unchanged (except for the
> +# core.worktree setting)
> +test_git_directory_is_unchanged () {
> +	(
> +		cd "$1" &&
> +		git config core.worktree "../../../$1"
> +	) &&
> +	git diff -r ".git/modules/$1" "$1/.git" &&

I'd prefer to see "--no-index" spelled out, if that is what is going on.

Show 28 quoted lines
> +	(
> +		cd "$1" &&
> +		GIT_WORK_TREE=. git config --unset core.worktree
> +	)
> +}
> +
> +# Helper function to be executed at the start of every test below, it sets up
> +# the submodule repo if it doesn't exist and configures the most problematic
> +# settings for diff.ignoreSubmodules.
> +prolog () {
> +	(test -d submodule_update_repo || create_lib_submodule_repo) &&
> +	test_config_global diff.ignoreSubmodules all &&
> +	test_config diff.ignoreSubmodules all
> +}
> +
> +# Helper function to bring work tree back into the state given by the
> +# commit. This includes trying to populate sub1 accordingly if it exists and
> +# should be updated to an existing commit.
> +reset_work_tree_to () {
> +	rm -rf submodule_update &&
> +	git clone submodule_update_repo submodule_update &&
> +	(
> +		cd submodule_update &&
> +		rm -rf sub1 &&
> +		git checkout -f "$1" &&
> +		git status -u -s >actual &&
> +		test_must_be_empty actual &&
> +		sha1=$(git ls-tree HEAD "sub1" 2>/dev/null | grep 160000 | tr '\t' ' ' | cut -d ' ' -f3) &&
Why discard the standard error stream?

grep|tr|cut looks somewhat stupid. Can't we do that with a single sed?

	sha1=$(git ls-tree HEAD sub1 | sed -ne "s/^160000 commit \($_x40\)     .*/\1/p")
or better yet, perhaps
	sha1=$(git rev-parse HEAD:sub1)
Show 12 quoted lines
> +# Test that the given submodule at path "$1" contains the content according
> +# to the submodule commit recorded in the superproject's commit "$2"
> +test_submodule_content () {
> +	if test $# != 2
> +	then
> +		echo "test_submodule_content needs two arguments"
> +		return 1
> +	fi &&
> +	submodule="$1" &&
> +	commit="$2" &&
> +	test -d "$submodule"/ &&
> +	if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git

I wonder if we can get away with a single "test -e" (we do not expect us to be creating device nodes or fifos there, do we?).

Show 5 quoted lines
> +	then
> +		echo "Submodule $submodule is not populated"
> +		return 1
> +	fi &&
> +	sha1=$(git ls-tree "$commit" "$submodule" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f3) &&
Likewise.
Jens Lehmann· Jun 17, 2014, 17:33 UTC · re: Junio C Hamano · lore

Re: [PATCH 02/14] submodules: Add the lib-submodule-update.sh test library

Am 17.06.2014 00:49, schrieb Junio C Hamano:
Show 114 quoted lines
> Jens Lehmann <Jens.Lehmann@web.de> writes:
> 
>> Add this test library to simplify covering all combinations of submodule
>> update scenarios without having to add those to a test of each work tree
>> manipulating command over and over again.
>>
>> The functions test_submodule_switch() and test_submodule_forced_switch()
>> are intended to be called from a test script with a single argument. This
>> argument is either a work tree manipulating command (including any command
>> line options) or a function (when more than a single git command is needed
>> to switch work trees from the current HEAD to another commit). This
>> command (or function) is passed a target branch as argument. The two new
>> functions check that each submodule transition is handled as expected,
>> which currently means that submodule work trees are not affected until
>> "git submodule update" is called. The "forced" variant is for commands
>> using their '-f' or '--hard' option and expects them to overwrite local
>> modifications as a result. Each of these two functions contains 14
>> tests_expect_* calls.
>>
>> Calling one of these test functions the first time creates a repository
>> named "submodule_update_repo". At first it contains two files, then a
>> single submodule is added in another commit followed by commits covering
>> all relevant submodule modifications. This repository is newly cloned into
>> the "submodule_update" for each test_expect_* to avoid interference
>> between different parts of the test functions (some to-be-tested commands
>> also manipulate refs along with the work tree, e.g. "git reset").
>>
>> Follow-up commits will then call these two test functions for all work
>> tree manipulating commands (with a combination of all their options
>> relevant to what they do with the work tree) making sure they work as
>> expected. Later this test library will be extended to cover merges
>> resulting in conflicts too. Also it is intended to be easily extendable
>> for the recursive update functionality, where even more combinations of
>> submodule modifications have to be tested for.
>>
>> This version documents two bugs in current Git with expected failures:
>>
>> *) When a submodule is replaced with a tracked file of the same name the
>>    submodule work tree including any local modifications (and even the
>>    whole history if it uses a .git directory instead of a gitfile!) is
>>    silently removed.
>>
>> *) Forced work tree updates happily manipulate files in the directory of a
>>    submodule that has just been removed in the superproject (but is of
>>    course still present in the work tree due to the way submodules are
>>    currently handled). This becomes dangerous when files in the submodule
>>    directory are overwritten by files from the new superproject commit, as
>>    any modifications to the submodule files will be lost) and is expected
>>    to also destroy history in the - admittedly unlikely case - the new
>>    commit adds a file named ".git" to the submodule directory.
>>
>> Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
>> ---
>>  t/lib-submodule-update.sh | 630 ++++++++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 630 insertions(+)
>>  create mode 100755 t/lib-submodule-update.sh
>>
>> diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
>> new file mode 100755
>> index 0000000..c6c842a
>> --- /dev/null
>> +++ b/t/lib-submodule-update.sh
>> @@ -0,0 +1,630 @@
>> +# Create a submodule layout used for all tests below.
>> +#
>> +# The following use cases are covered:
>> +# - New submodule (no_submodule => add_sub1)
>> +# - Removed submodule (add_sub1 => remove_sub1)
>> +# - Updated submodule (add_sub1 => modify_sub1)
>> +# - Submodule updated to invalid commit (add_sub1 => invalid_sub1)
>> +# - Submodule updated from invalid commit (invalid_sub1 => valid_sub1)
>> +# - Submodule replaced by tracked files in directory (add_sub1 =>
>> +#   replace_sub1_with_directory)
>> +# - Directory containing tracked files replaced by submodule
>> +#   (replace_sub1_with_directory => replace_directory_with_sub1)
>> +# - Submodule replaced by tracked file with the same name (add_sub1 =>
>> +#   replace_sub1_with_file)
>> +# - Tracked file replaced by submodule (replace_sub1_with_file =>
>> +#   replace_file_with_sub1)
>> +#
>> +#                   --O-----O
>> +#                  /  ^     replace_directory_with_sub1
>> +#                 /   replace_sub1_with_directory
>> +#                /----O
>> +#               /     ^
>> +#              /      modify_sub1
>> +#      O------O-------O
>> +#      ^      ^\      ^
>> +#      |      | \     remove_sub1
>> +#      |      |  -----O-----O
>> +#      |      |   \   ^     replace_file_with_sub1
>> +#      |      |    \  replace_sub1_with_file
>> +#      |   add_sub1 --O-----O
>> +# no_submodule        ^     valid_sub1
>> +#                     invalid_sub1
>> +#
>> +create_lib_submodule_repo () {
>> +	git init submodule_update_repo &&
>> +	(
>> +		cd submodule_update_repo &&
>> +		echo "expect" >>.gitignore &&
>> +		echo "actual" >>.gitignore &&
>> +		echo "x" >file1 &&
>> +		echo "y" >file2 &&
>> +		git add .gitignore file1 file2 &&
>> +		git commit -m "Base" &&
>> +		git branch "no_submodule" &&
>> +
>> +		git checkout -b "add_sub1" &&
>> +		git submodule add ./. sub1 &&
> 
> This is not technically wrong per-se, but having the project's
> history itself as its own submodule *is* something nobody sane would
> do in the real life.  Do we really have to do it this unusual way?

I agree that this isn't a sane setup for real world usage, but I did that because it makes things easier when adding tests for recursive submodule update later, as we can then use the same test setup just one submodule level deeper.

Show 62 quoted lines
>> +		git config -f .gitmodules submodule.sub1.ignore all &&
>> +		git config submodule.sub1.ignore all &&
>> +		git add .gitmodules &&
>> +		git commit -m "Add sub1" &&
>> +		git checkout -b remove_sub1 &&
>> +		git revert HEAD &&
>> +
>> +		git checkout -b "modify_sub1" "add_sub1" &&
>> +		git submodule update &&
>> +		(
>> +			cd sub1 &&
>> +			git fetch &&
>> +			git checkout -b "modifications" &&
>> +			echo "z" >file2 &&
>> +			echo "x" >file3 &&
>> +			git add file2 file3 &&
>> +			git commit -m "modified file2 and added file3" &&
>> +			git push origin modifications
>> +		) &&
>> +		git add sub1 &&
>> +		git commit -m "Modify sub1" &&
>> +
>> +		git checkout -b "replace_sub1_with_directory" "add_sub1" &&
>> +		git submodule update &&
>> +		(
>> +			cd sub1 &&
>> +			git checkout modifications
>> +		) &&
>> +		git rm --cached sub1 &&
>> +		rm sub1/.git* &&
>> +		git config -f .gitmodules --remove-section "submodule.sub1" &&
>> +		git add .gitmodules sub1/* &&
>> +		git commit -m "Replace sub1 with directory" &&
>> +		git checkout -b replace_directory_with_sub1 &&
>> +		git revert HEAD &&
>> +
>> +		git checkout -b "replace_sub1_with_file" "add_sub1" &&
>> +		git rm sub1 &&
>> +		echo "content" >sub1 &&
>> +		git add sub1 &&
>> +		git commit -m "Replace sub1 with file" &&
>> +		git checkout -b replace_file_with_sub1 &&
>> +		git revert HEAD &&
>> +
>> +		git checkout -b "invalid_sub1" "add_sub1" &&
>> +		git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 sub1 &&
>> +		git commit -m "Invalid sub1 commit" &&
>> +		git checkout -b valid_sub1 &&
>> +		git revert HEAD &&
>> +		git checkout master
>> +	)
>> +}
>> +
>> +# Helper function to replace gitfile with .git directory
>> +replace_gitfile_with_git_dir () {
>> +	(
>> +		cd "$1" &&
>> +		git_dir="$(git rev-parse --git-dir)" &&
>> +		rm -f .git &&
>> +		cp -a "$git_dir" .git &&
> 
> We avoid "cp -a" for portability, don't we?
Yup, will change to "cp -R".
Show 5 quoted lines
>> +		GIT_WORK_TREE=. git config --unset core.worktree
> 
> Hmph.  What does GIT_WORK_TREE=. alone without GIT_DIR=<somewhere>
> do?  It's not like it is a workaround for "git config" that complains
> when you do not have a working tree, right?  Puzzled...

It is, it overrides the core.worktree config that would stop us from unsetting the core.worktree config with this error message:

  fatal: Could not chdir to '../../../sub1': No such file or directory
(We use the same pattern in git-submodule.sh and some other tests)
Show 14 quoted lines
>> +	)
>> +}
>> +
>> +# Test that the .git directory in the submodule is unchanged (except for the
>> +# core.worktree setting)
>> +test_git_directory_is_unchanged () {
>> +	(
>> +		cd "$1" &&
>> +		git config core.worktree "../../../$1"
>> +	) &&
>> +	git diff -r ".git/modules/$1" "$1/.git" &&
> 
> I'd prefer to see "--no-index" spelled out, if that is what is going
> on.

Thanks for catching, this should read "diff -r" (to compare the two directories like the comment above states). Correcting this brings up two test failures (in which the index file changed), will investigate ...

Show 30 quoted lines
>> +	(
>> +		cd "$1" &&
>> +		GIT_WORK_TREE=. git config --unset core.worktree
>> +	)
>> +}
>> +
>> +# Helper function to be executed at the start of every test below, it sets up
>> +# the submodule repo if it doesn't exist and configures the most problematic
>> +# settings for diff.ignoreSubmodules.
>> +prolog () {
>> +	(test -d submodule_update_repo || create_lib_submodule_repo) &&
>> +	test_config_global diff.ignoreSubmodules all &&
>> +	test_config diff.ignoreSubmodules all
>> +}
>> +
>> +# Helper function to bring work tree back into the state given by the
>> +# commit. This includes trying to populate sub1 accordingly if it exists and
>> +# should be updated to an existing commit.
>> +reset_work_tree_to () {
>> +	rm -rf submodule_update &&
>> +	git clone submodule_update_repo submodule_update &&
>> +	(
>> +		cd submodule_update &&
>> +		rm -rf sub1 &&
>> +		git checkout -f "$1" &&
>> +		git status -u -s >actual &&
>> +		test_must_be_empty actual &&
>> +		sha1=$(git ls-tree HEAD "sub1" 2>/dev/null | grep 160000 | tr '\t' ' ' | cut -d ' ' -f3) &&
> 
> Why discard the standard error stream?
Because we sometimes reset to commits where "sub1" isn't present:
  fatal: Path 'sub1' does not exist in 'HEAD'
Show 8 quoted lines
> grep|tr|cut looks somewhat stupid.  Can't we do that with a single
> sed?
> 
> 	sha1=$(git ls-tree HEAD sub1 | sed -ne "s/^160000 commit \($_x40\)     .*/\1/p")
> 
> or better yet, perhaps
> 
> 	sha1=$(git rev-parse HEAD:sub1)

Cool, that's much better. Due to the sometimes missing "sub1" I needed to modify it to drop the error and not fail:

  sha1=$(git rev-parse HEAD:sub1 2>/dev/null || true) &&
Show 15 quoted lines
>> +# Test that the given submodule at path "$1" contains the content according
>> +# to the submodule commit recorded in the superproject's commit "$2"
>> +test_submodule_content () {
>> +	if test $# != 2
>> +	then
>> +		echo "test_submodule_content needs two arguments"
>> +		return 1
>> +	fi &&
>> +	submodule="$1" &&
>> +	commit="$2" &&
>> +	test -d "$submodule"/ &&
>> +	if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
> 
> I wonder if we can get away with a single "test -e" (we do not
> expect us to be creating device nodes or fifos there, do we?).

But a symbolic link maybe? I wouldn't object loosening this test, but I thought it best to spell out the only two currently expected cases.

Show 7 quoted lines
>> +	then
>> +		echo "Submodule $submodule is not populated"
>> +		return 1
>> +	fi &&
>> +	sha1=$(git ls-tree "$commit" "$submodule" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f3) &&
> 
> Likewise.
Yup, I changed this to:
  sha1=$(git rev-parse "$commit:$submodule")
because "$submodule" should always exist when we end up here.
Junio C Hamano· Jun 17, 2014, 18:44 UTC · re: Jens Lehmann · lore

Re: [PATCH 02/14] submodules: Add the lib-submodule-update.sh test library

Jens Lehmann <Jens.Lehmann@web.de> writes:
Show 13 quoted lines
> Am 17.06.2014 00:49, schrieb Junio C Hamano:
>> Jens Lehmann <Jens.Lehmann@web.de> writes:
>>> +		git checkout -b "add_sub1" &&
>>> +		git submodule add ./. sub1 &&
>> 
>> This is not technically wrong per-se, but having the project's
>> history itself as its own submodule *is* something nobody sane would
>> do in the real life.  Do we really have to do it this unusual way?
>
> I agree that this isn't a sane setup for real world usage, but I did
> that because it makes things easier when adding tests for recursive
> submodule update later, as we can then use the same test setup just
> one submodule level deeper.
Hmmm... ok....
Show 12 quoted lines
>>> +		GIT_WORK_TREE=. git config --unset core.worktree
>> 
>> Hmph.  What does GIT_WORK_TREE=. alone without GIT_DIR=<somewhere>
>> do?  It's not like it is a workaround for "git config" that complains
>> when you do not have a working tree, right?  Puzzled...
>
> It is, it overrides the core.worktree config that would stop us
> from unsetting the core.worktree config with this error message:
>
>   fatal: Could not chdir to '../../../sub1': No such file or directory
>
> (We use the same pattern in git-submodule.sh and some other tests)

Is this a work-around for a bug in "git config"? Or is this an expected failure and it is unusual and not realistic outside of test setup to want to unset core.worktree? I am inclined to think it is the latter, but I dunno.

Show 7 quoted lines
>>> +		sha1=$(git ls-tree HEAD "sub1" 2>/dev/null | grep 160000 | tr '\t' ' ' | cut -d ' ' -f3) &&
>> 
>> Why discard the standard error stream?
>
> Because we sometimes reset to commits where "sub1" isn't present:
>
>   fatal: Path 'sub1' does not exist in 'HEAD'
Huh?  We shouldn't.
	$ git ls-tree HEAD no-such; echo $?
        0

It discards errors that may happen in other situations, too---is that something we do not have to worry about?

> Cool, that's much better. Due to the sometimes missing "sub1" I
> needed to modify it to drop the error and not fail:
>
>   sha1=$(git rev-parse HEAD:sub1 2>/dev/null || true) &&

The "HEAD:sub1" notation does require that the path exists in the specified tree-ish. Even if we tried to express the above in a more carefully written form:

	# We may or may not have sub1 in HEAD
        if "sub1 exists in HEAD"
        then
        	sha1=$(git rev-parse HEAD:sub1)
	else
		sha1= # empty
	fi

we would end up using "git rev-parse HEAD:sub1" to implement "sub1 exists in HEAD" part, so your updated alternative would be the best we could do, I would think.

Show 17 quoted lines
>>> +# Test that the given submodule at path "$1" contains the content according
>>> +# to the submodule commit recorded in the superproject's commit "$2"
>>> +test_submodule_content () {
>>> +	if test $# != 2
>>> +	then
>>> +		echo "test_submodule_content needs two arguments"
>>> +		return 1
>>> +	fi &&
>>> +	submodule="$1" &&
>>> +	commit="$2" &&
>>> +	test -d "$submodule"/ &&
>>> +	if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
>> 
>> I wonder if we can get away with a single "test -e" (we do not
>> expect us to be creating device nodes or fifos there, do we?).
>
> But a symbolic link maybe?

Symlinks should pose no problems, I would say, without loosening anything.

	$ test -f RelNotes; echo $?; test -e RelNotes; echo $?
        0
        0
        $ ln -s t tests; test -d tests; echo $?; test -e tests; echo $?
        0
        $ ln -s no-such x; test -f x; echo $?; test -e x; echo $?
        1
        1
Thanks.
Jens Lehmann· Jun 17, 2014, 20:46 UTC · re: Junio C Hamano · lore

Re: [PATCH 02/14] submodules: Add the lib-submodule-update.sh test library

Am 17.06.2014 20:44, schrieb Junio C Hamano:
Show 21 quoted lines
> Jens Lehmann <Jens.Lehmann@web.de> writes:
> 
>> Am 17.06.2014 00:49, schrieb Junio C Hamano:
>>> Jens Lehmann <Jens.Lehmann@web.de> writes:
>>>> +		GIT_WORK_TREE=. git config --unset core.worktree
>>>
>>> Hmph.  What does GIT_WORK_TREE=. alone without GIT_DIR=<somewhere>
>>> do?  It's not like it is a workaround for "git config" that complains
>>> when you do not have a working tree, right?  Puzzled...
>>
>> It is, it overrides the core.worktree config that would stop us
>> from unsetting the core.worktree config with this error message:
>>
>>   fatal: Could not chdir to '../../../sub1': No such file or directory
>>
>> (We use the same pattern in git-submodule.sh and some other tests)
> 
> Is this a work-around for a bug in "git config"?  Or is this an
> expected failure and it is unusual and not realistic outside of test
> setup to want to unset core.worktree?  I am inclined to think it is
> the latter, but I dunno.

I didn't think deeply about that, but when I first encountered this behavior it felt a bit strange that config does a chdir into the work tree. I somehow expected it only to access the config file in GIT_DIR and not the work tree, but I didn't care enough to investigate further after I found this solution.

Show 12 quoted lines
>>>> +		sha1=$(git ls-tree HEAD "sub1" 2>/dev/null | grep 160000 | tr '\t' ' ' | cut -d ' ' -f3) &&
>>>
>>> Why discard the standard error stream?
>>
>> Because we sometimes reset to commits where "sub1" isn't present:
>>
>>   fatal: Path 'sub1' does not exist in 'HEAD'
> 
> Huh?  We shouldn't.
> 
> 	$ git ls-tree HEAD no-such; echo $?
>         0

You are correct, it looks like I used rev-parse instead of ls-files when producing that error.

> It discards errors that may happen in other situations, too---is
> that something we do not have to worry about?

I'll look deeper into that tomorrow. After all in some tests "sub1" will be a file and not a submodule, and then we should not try to populate it ...

Show 19 quoted lines
>>>> +# Test that the given submodule at path "$1" contains the content according
>>>> +# to the submodule commit recorded in the superproject's commit "$2"
>>>> +test_submodule_content () {
>>>> +	if test $# != 2
>>>> +	then
>>>> +		echo "test_submodule_content needs two arguments"
>>>> +		return 1
>>>> +	fi &&
>>>> +	submodule="$1" &&
>>>> +	commit="$2" &&
>>>> +	test -d "$submodule"/ &&
>>>> +	if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
>>>
>>> I wonder if we can get away with a single "test -e" (we do not
>>> expect us to be creating device nodes or fifos there, do we?).
>>
>> But a symbolic link maybe?
> 
> Symlinks should pose no problems, ...

Oh, I forgot to add a smiley there, I haven't been serious about that statement. I don't care too deeply about it but feel a bit more confident with the two explicit tests. Do you want me to change them to a single "test -e"?

Junio C Hamano· Jun 17, 2014, 21:05 UTC · re: Jens Lehmann · lore

Re: [PATCH 02/14] submodules: Add the lib-submodule-update.sh test library

Jens Lehmann <Jens.Lehmann@web.de> writes:
> Oh, I forgot to add a smiley there, I haven't been serious about
> that statement. I don't care too deeply about it but feel a bit
> more confident with the two explicit tests. Do you want me to
> change them to a single "test -e"?
I have no strong preference.
Jens Lehmann· Jun 19, 2014, 20:12 UTC · re: Jens Lehmann · lore

[PATCH v2 02/14] submodules: Add the lib-submodule-update.sh test library

Add this test library to simplify covering all combinations of submodule update scenarios without having to add those to a test of each work tree manipulating command over and over again.

The functions test_submodule_switch() and test_submodule_forced_switch() are intended to be called from a test script with a single argument. This argument is either a work tree manipulating command (including any command line options) or a function (when more than a single git command is needed to switch work trees from the current HEAD to another commit). This command (or function) is passed a target branch as argument. The two new functions check that each submodule transition is handled as expected, which currently means that submodule work trees are not affected until "git submodule update" is called. The "forced" variant is for commands using their '-f' or '--hard' option and expects them to overwrite local modifications as a result. Each of these two functions contains 14 tests_expect_* calls.

Calling one of these test functions the first time creates a repository named "submodule_update_repo". At first it contains two files, then a single submodule is added in another commit followed by commits covering all relevant submodule modifications. This repository is newly cloned into the "submodule_update" for each test_expect_* to avoid interference between different parts of the test functions (some to-be-tested commands also manipulate refs along with the work tree, e.g. "git reset").

Follow-up commits will then call these two test functions for all work tree manipulating commands (with a combination of all their options relevant to what they do with the work tree) making sure they work as expected. Later this test library will be extended to cover merges resulting in conflicts too. Also it is intended to be easily extendable for the recursive update functionality, where even more combinations of submodule modifications have to be tested for.

This version documents two bugs in current Git with expected failures:
*) When a submodule is replaced with a tracked file of the same name the
   submodule work tree including any local modifications (and even the
   whole history if it uses a .git directory instead of a gitfile!) is
   silently removed.
*) Forced work tree updates happily manipulate files in the directory of a
   submodule that has just been removed in the superproject (but is of
   course still present in the work tree due to the way submodules are
   currently handled). This becomes dangerous when files in the submodule
   directory are overwritten by files from the new superproject commit, as
   any modifications to the submodule files will be lost) and is expected
   to also destroy history in the - admittedly unlikely case - the new
   commit adds a file named ".git" to the submodule directory.
Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
Changes to v1:
*) avoid non-portable "cp -a"
*) use "diff -r" without "git " when comparing directory trees
*) simplify retrieving the SHA-1 of sub1
*) call test_git_directory_is_unchanged before test_submodule_content (this
   was the reason for two test failures after removing the "git " before
   "diff" as test_submodule_content would sometimes modify the index)
 t/lib-submodule-update.sh | 632 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 632 insertions(+)
 create mode 100755 t/lib-submodule-update.sh
Show changes to t/lib-submodule-update.sh +632 −0
diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
new file mode 100755
index 0000000..03217b2
--- /dev/null
+++ b/t/lib-submodule-update.sh
@@ -0,0 +1,632 @@
+# Create a submodule layout used for all tests below.
+#
+# The following use cases are covered:
+# - New submodule (no_submodule => add_sub1)
+# - Removed submodule (add_sub1 => remove_sub1)
+# - Updated submodule (add_sub1 => modify_sub1)
+# - Submodule updated to invalid commit (add_sub1 => invalid_sub1)
+# - Submodule updated from invalid commit (invalid_sub1 => valid_sub1)
+# - Submodule replaced by tracked files in directory (add_sub1 =>
+#   replace_sub1_with_directory)
+# - Directory containing tracked files replaced by submodule
+#   (replace_sub1_with_directory => replace_directory_with_sub1)
+# - Submodule replaced by tracked file with the same name (add_sub1 =>
+#   replace_sub1_with_file)
+# - Tracked file replaced by submodule (replace_sub1_with_file =>
+#   replace_file_with_sub1)
+#
+#                   --O-----O
+#                  /  ^     replace_directory_with_sub1
+#                 /   replace_sub1_with_directory
+#                /----O
+#               /     ^
+#              /      modify_sub1
+#      O------O-------O
+#      ^      ^\      ^
+#      |      | \     remove_sub1
+#      |      |  -----O-----O
+#      |      |   \   ^     replace_file_with_sub1
+#      |      |    \  replace_sub1_with_file
+#      |   add_sub1 --O-----O
+# no_submodule        ^     valid_sub1
+#                     invalid_sub1
+#
+create_lib_submodule_repo () {
+	git init submodule_update_repo &&
+	(
+		cd submodule_update_repo &&
+		echo "expect" >>.gitignore &&
+		echo "actual" >>.gitignore &&
+		echo "x" >file1 &&
+		echo "y" >file2 &&
+		git add .gitignore file1 file2 &&
+		git commit -m "Base" &&
+		git branch "no_submodule" &&
+
+		git checkout -b "add_sub1" &&
+		git submodule add ./. sub1 &&
+		git config -f .gitmodules submodule.sub1.ignore all &&
+		git config submodule.sub1.ignore all &&
+		git add .gitmodules &&
+		git commit -m "Add sub1" &&
+		git checkout -b remove_sub1 &&
+		git revert HEAD &&
+
+		git checkout -b "modify_sub1" "add_sub1" &&
+		git submodule update &&
+		(
+			cd sub1 &&
+			git fetch &&
+			git checkout -b "modifications" &&
+			echo "z" >file2 &&
+			echo "x" >file3 &&
+			git add file2 file3 &&
+			git commit -m "modified file2 and added file3" &&
+			git push origin modifications
+		) &&
+		git add sub1 &&
+		git commit -m "Modify sub1" &&
+
+		git checkout -b "replace_sub1_with_directory" "add_sub1" &&
+		git submodule update &&
+		(
+			cd sub1 &&
+			git checkout modifications
+		) &&
+		git rm --cached sub1 &&
+		rm sub1/.git* &&
+		git config -f .gitmodules --remove-section "submodule.sub1" &&
+		git add .gitmodules sub1/* &&
+		git commit -m "Replace sub1 with directory" &&
+		git checkout -b replace_directory_with_sub1 &&
+		git revert HEAD &&
+
+		git checkout -b "replace_sub1_with_file" "add_sub1" &&
+		git rm sub1 &&
+		echo "content" >sub1 &&
+		git add sub1 &&
+		git commit -m "Replace sub1 with file" &&
+		git checkout -b replace_file_with_sub1 &&
+		git revert HEAD &&
+
+		git checkout -b "invalid_sub1" "add_sub1" &&
+		git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 sub1 &&
+		git commit -m "Invalid sub1 commit" &&
+		git checkout -b valid_sub1 &&
+		git revert HEAD &&
+		git checkout master
+	)
+}
+
+# Helper function to replace gitfile with .git directory
+replace_gitfile_with_git_dir () {
+	(
+		cd "$1" &&
+		git_dir="$(git rev-parse --git-dir)" &&
+		rm -f .git &&
+		cp -R "$git_dir" .git &&
+		GIT_WORK_TREE=. git config --unset core.worktree
+	)
+}
+
+# Test that the .git directory in the submodule is unchanged (except for the
+# core.worktree setting, which we temporarily restore). Call this function
+# before test_submodule_content as the latter might write the index file
+# leading to false positive index differences.
+test_git_directory_is_unchanged () {
+	(
+		cd "$1" &&
+		git config core.worktree "../../../$1"
+	) &&
+	diff -r ".git/modules/$1" "$1/.git" &&
+	(
+		cd "$1" &&
+		GIT_WORK_TREE=. git config --unset core.worktree
+	)
+}
+
+# Helper function to be executed at the start of every test below, it sets up
+# the submodule repo if it doesn't exist and configures the most problematic
+# settings for diff.ignoreSubmodules.
+prolog () {
+	(test -d submodule_update_repo || create_lib_submodule_repo) &&
+	test_config_global diff.ignoreSubmodules all &&
+	test_config diff.ignoreSubmodules all
+}
+
+# Helper function to bring work tree back into the state given by the
+# commit. This includes trying to populate sub1 accordingly if it exists and
+# should be updated to an existing commit.
+reset_work_tree_to () {
+	rm -rf submodule_update &&
+	git clone submodule_update_repo submodule_update &&
+	(
+		cd submodule_update &&
+		rm -rf sub1 &&
+		git checkout -f "$1" &&
+		git status -u -s >actual &&
+		test_must_be_empty actual &&
+		sha1=$(git rev-parse HEAD:sub1 || true) &&
+		if test -n "$sha1" &&
+		   test $(cd "sub1" && git rev-parse --verify "$sha1^{commit}")
+		then
+			git submodule update --init --recursive "sub1"
+		fi
+	)
+}
+
+# Test that the superproject contains the content according to commit "$1"
+# (the work tree must match the index for everything but submodules but the
+# index must exactly match the given commit including any submodule SHA-1s).
+test_superproject_content () {
+	git diff-index --cached "$1" >actual &&
+	test_must_be_empty actual &&
+	git diff-files --ignore-submodules >actual &&
+	test_must_be_empty actual
+}
+
+# Test that the given submodule at path "$1" contains the content according
+# to the submodule commit recorded in the superproject's commit "$2"
+test_submodule_content () {
+	if test $# != 2
+	then
+		echo "test_submodule_content needs two arguments"
+		return 1
+	fi &&
+	submodule="$1" &&
+	commit="$2" &&
+	test -d "$submodule"/ &&
+	if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
+	then
+		echo "Submodule $submodule is not populated"
+		return 1
+	fi &&
+	sha1=$(git rev-parse "$commit:$submodule") &&
+	if test -z "$sha1"
+	then
+		echo "Couldn't retrieve SHA-1 of $submodule for $commit"
+		return 1
+	fi &&
+	(
+		cd "$submodule" &&
+		git status -u -s >actual &&
+		test_must_be_empty actual &&
+		git diff "$sha1" >actual &&
+		test_must_be_empty actual
+	)
+}
+
+# Test that the following transitions are correctly handled:
+# - Updated submodule
+# - New submodule
+# - Removed submodule
+# - Directory containing tracked files replaced by submodule
+# - Submodule replaced by tracked files in directory
+# - Submodule replaced by tracked file with the same name
+# - tracked file replaced by submodule
+#
+# The default is that submodule contents aren't changed until "git submodule
+# update" is run. And even then that command doesn't delete the work tree of
+# a removed submodule.
+#
+# Removing a submodule containing a .git directory must fail even when forced
+# to protect the history!
+#
+
+# Test that submodule contents are currently not updated when switching
+# between commits that change a submodule.
+test_submodule_switch () {
+	command="$1"
+	######################### Appearing submodule #########################
+	# Switching to a commit letting a submodule appear creates empty dir ...
+	test_expect_success "$command: added submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... and doesn't care if it already exists ...
+	test_expect_success "$command: added submodule leaves existing empty directory alone" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			mkdir sub1 &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... unless there is an untracked file in its place.
+	test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			echo -n >sub1 &&
+			test_must_fail $command add_sub1 &&
+			test_superproject_content origin/no_submodule &&
+			test_must_be_empty sub1
+		)
+	'
+	# Replacing a tracked file with a submodule produces an empty
+	# directory ...
+	test_expect_success "$command: replace tracked file with submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_file &&
+		(
+			cd submodule_update &&
+			git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
+			$command replace_file_with_sub1 &&
+			test_superproject_content origin/replace_file_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_file_with_sub1
+		)
+	'
+	# ... as does removing a directory with tracked files with a
+	# submodule.
+	test_expect_success "$command: replace directory with submodule" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_directory &&
+		(
+			cd submodule_update &&
+			git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
+			$command replace_directory_with_sub1 &&
+			test_superproject_content origin/replace_directory_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_directory_with_sub1
+		)
+	'
+
+	######################## Disappearing submodule #######################
+	# Removing a submodule doesn't remove its work tree ...
+	test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			replace_gitfile_with_git_dir sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Replacing a submodule with files in a directory must fail as the
+	# submodule work tree isn't removed ...
+	test_expect_success "$command: replace submodule with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Replacing it with a file must fail as it could throw away any local
+	# work tree changes ...
+	test_expect_failure "$command: replace submodule with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... or even destroy unpushed parts of submodule history if that
+	# still uses a .git directory.
+	test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+
+	########################## Modified submodule #########################
+	# Updating a submodule sha1 doesn't update the submodule's work tree
+	test_expect_success "$command: modified submodule does not update submodule work tree" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t modify_sub1 origin/modify_sub1 &&
+			$command modify_sub1 &&
+			test_superproject_content origin/modify_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			git submodule update &&
+			test_submodule_content sub1 origin/modify_sub1
+		)
+	'
+
+	# Updating a submodule to an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will fail
+	test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t invalid_sub1 origin/invalid_sub1 &&
+			$command invalid_sub1 &&
+			test_superproject_content origin/invalid_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_must_fail git submodule update &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Updating a submodule from an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will succeed
+	test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
+		prolog &&
+		reset_work_tree_to invalid_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t valid_sub1 origin/valid_sub1 &&
+			$command valid_sub1 &&
+			test_superproject_content origin/valid_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/valid_sub1
+		)
+	'
+}
+
+# Test that submodule contents are currently not updated when switching
+# between commits that change a submodule, but throwing away local changes in
+# the superproject is allowed.
+test_submodule_forced_switch () {
+	command="$1"
+	######################### Appearing submodule #########################
+	# Switching to a commit letting a submodule appear creates empty dir ...
+	test_expect_success "$command: added submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... and doesn't care if it already exists ...
+	test_expect_success "$command: added submodule leaves existing empty directory alone" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			mkdir sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... unless there is an untracked file in its place.
+	test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			echo -n >sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1
+		)
+	'
+	# Replacing a tracked file with a submodule produces an empty
+	# directory ...
+	test_expect_success "$command: replace tracked file with submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_file &&
+		(
+			cd submodule_update &&
+			git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
+			$command replace_file_with_sub1 &&
+			test_superproject_content origin/replace_file_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_file_with_sub1
+		)
+	'
+	# ... as does removing a directory with tracked files with a
+	# submodule.
+	test_expect_success "$command: replace directory with submodule" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_directory &&
+		(
+			cd submodule_update &&
+			git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
+			$command replace_directory_with_sub1 &&
+			test_superproject_content origin/replace_directory_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_directory_with_sub1
+		)
+	'
+
+	######################## Disappearing submodule #######################
+	# Removing a submodule doesn't remove its work tree ...
+	test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			replace_gitfile_with_git_dir sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Replacing a submodule with files in a directory must fail as the
+	# submodule work tree isn't removed ...
+	test_expect_failure "$command: replace submodule with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_failure "$command: replace submodule containing a .git directory with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Replacing it with a file must fail as it could throw away any local
+	# work tree changes ...
+	test_expect_failure "$command: replace submodule with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... or even destroy unpushed parts of submodule history if that
+	# still uses a .git directory.
+	test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+
+	########################## Modified submodule #########################
+	# Updating a submodule sha1 doesn't update the submodule's work tree
+	test_expect_success "$command: modified submodule does not update submodule work tree" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t modify_sub1 origin/modify_sub1 &&
+			$command modify_sub1 &&
+			test_superproject_content origin/modify_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			git submodule update &&
+			test_submodule_content sub1 origin/modify_sub1
+		)
+	'
+	# Updating a submodule to an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will fail
+	test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t invalid_sub1 origin/invalid_sub1 &&
+			$command invalid_sub1 &&
+			test_superproject_content origin/invalid_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_must_fail git submodule update &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Updating a submodule from an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will succeed
+	test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
+		prolog &&
+		reset_work_tree_to invalid_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t valid_sub1 origin/valid_sub1 &&
+			$command valid_sub1 &&
+			test_superproject_content origin/valid_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/valid_sub1
+		)
+	'
+}
-- 
2.0.0.406.gf4dce28
Junio C Hamano· Jun 20, 2014, 17:31 UTC · re: Jens Lehmann · lore

Re: [PATCH v2 02/14] submodules: Add the lib-submodule-update.sh test library

Jens Lehmann <Jens.Lehmann@web.de> writes:
Show 5 quoted lines
> +		rm -rf sub1 &&
> +		git checkout -f "$1" &&
> +		git status -u -s >actual &&
> +		test_must_be_empty actual &&
> +		sha1=$(git rev-parse HEAD:sub1 || true) &&
	$ xx=; xx=$(git rev-parse HEAD:no-such-path || true) ; echo $? ; echo "<$xx>"
        fatal: Path 'no-such-path' does not exist in 'HEAD'
        0
        <HEAD:no-such-path>
Perhaps you want --verify (or --revs-only) there, i.e.
	sha1=$(git rev-parse --verify HEAD:sub1 || :) &&
or
	sha1=$(git rev-parse --revs-only HEAD:sub1) &&
> +		if test -n "$sha1" &&
> ...
> +	sha1=$(git rev-parse "$commit:$submodule") &&
And here too.
> +	if test -z "$sha1"
Jens Lehmann· Jul 1, 2014, 21:24 UTC · re: Junio C Hamano · lore

[PATCH v3 02/14] submodules: Add the lib-submodule-update.sh test library

Add this test library to simplify covering all combinations of submodule update scenarios without having to add those to a test of each work tree manipulating command over and over again.

The functions test_submodule_switch() and test_submodule_forced_switch() are intended to be called from a test script with a single argument. This argument is either a work tree manipulating command (including any command line options) or a function (when more than a single git command is needed to switch work trees from the current HEAD to another commit). This command (or function) is passed a target branch as argument. The two new functions check that each submodule transition is handled as expected, which currently means that submodule work trees are not affected until "git submodule update" is called. The "forced" variant is for commands using their '-f' or '--hard' option and expects them to overwrite local modifications as a result. Each of these two functions contains 14 tests_expect_* calls.

Calling one of these test functions the first time creates a repository named "submodule_update_repo". At first it contains two files, then a single submodule is added in another commit followed by commits covering all relevant submodule modifications. This repository is newly cloned into the "submodule_update" for each test_expect_* to avoid interference between different parts of the test functions (some to-be-tested commands also manipulate refs along with the work tree, e.g. "git reset").

Follow-up commits will then call these two test functions for all work tree manipulating commands (with a combination of all their options relevant to what they do with the work tree) making sure they work as expected. Later this test library will be extended to cover merges resulting in conflicts too. Also it is intended to be easily extendable for the recursive update functionality, where even more combinations of submodule modifications have to be tested for.

This version documents two bugs in current Git with expected failures:
*) When a submodule is replaced with a tracked file of the same name the
   submodule work tree including any local modifications (and even the
   whole history if it uses a .git directory instead of a gitfile!) is
   silently removed.
*) Forced work tree updates happily manipulate files in the directory of a
   submodule that has just been removed in the superproject (but is of
   course still present in the work tree due to the way submodules are
   currently handled). This becomes dangerous when files in the submodule
   directory are overwritten by files from the new superproject commit, as
   any modifications to the submodule files will be lost) and is expected
   to also destroy history in the - admittedly unlikely case - the new
   commit adds a file named ".git" to the submodule directory.
Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
Am 20.06.2014 19:31, schrieb Junio C Hamano:
Show 26 quoted lines
> Jens Lehmann <Jens.Lehmann@web.de> writes:
> 
>> +		rm -rf sub1 &&
>> +		git checkout -f "$1" &&
>> +		git status -u -s >actual &&
>> +		test_must_be_empty actual &&
>> +		sha1=$(git rev-parse HEAD:sub1 || true) &&
> 
> 	$ xx=; xx=$(git rev-parse HEAD:no-such-path || true) ; echo $? ; echo "<$xx>"
>         fatal: Path 'no-such-path' does not exist in 'HEAD'
>         0
>         <HEAD:no-such-path>
> 
> Perhaps you want --verify (or --revs-only) there, i.e.
> 
> 	sha1=$(git rev-parse --verify HEAD:sub1 || :) &&
> 
> or
> 
> 	sha1=$(git rev-parse --revs-only HEAD:sub1) &&
> 
>> +		if test -n "$sha1" &&
>> ...
>> +	sha1=$(git rev-parse "$commit:$submodule") &&
> 
> And here too.
Thanks, I squashed your "SQUASH???" commit (currently in pu) into this version.
 t/lib-submodule-update.sh | 632 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 632 insertions(+)
 create mode 100755 t/lib-submodule-update.sh
Show changes to t/lib-submodule-update.sh +632 −0
diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
new file mode 100755
index 0000000..ceeaaeb
--- /dev/null
+++ b/t/lib-submodule-update.sh
@@ -0,0 +1,632 @@
+# Create a submodule layout used for all tests below.
+#
+# The following use cases are covered:
+# - New submodule (no_submodule => add_sub1)
+# - Removed submodule (add_sub1 => remove_sub1)
+# - Updated submodule (add_sub1 => modify_sub1)
+# - Submodule updated to invalid commit (add_sub1 => invalid_sub1)
+# - Submodule updated from invalid commit (invalid_sub1 => valid_sub1)
+# - Submodule replaced by tracked files in directory (add_sub1 =>
+#   replace_sub1_with_directory)
+# - Directory containing tracked files replaced by submodule
+#   (replace_sub1_with_directory => replace_directory_with_sub1)
+# - Submodule replaced by tracked file with the same name (add_sub1 =>
+#   replace_sub1_with_file)
+# - Tracked file replaced by submodule (replace_sub1_with_file =>
+#   replace_file_with_sub1)
+#
+#                   --O-----O
+#                  /  ^     replace_directory_with_sub1
+#                 /   replace_sub1_with_directory
+#                /----O
+#               /     ^
+#              /      modify_sub1
+#      O------O-------O
+#      ^      ^\      ^
+#      |      | \     remove_sub1
+#      |      |  -----O-----O
+#      |      |   \   ^     replace_file_with_sub1
+#      |      |    \  replace_sub1_with_file
+#      |   add_sub1 --O-----O
+# no_submodule        ^     valid_sub1
+#                     invalid_sub1
+#
+create_lib_submodule_repo () {
+	git init submodule_update_repo &&
+	(
+		cd submodule_update_repo &&
+		echo "expect" >>.gitignore &&
+		echo "actual" >>.gitignore &&
+		echo "x" >file1 &&
+		echo "y" >file2 &&
+		git add .gitignore file1 file2 &&
+		git commit -m "Base" &&
+		git branch "no_submodule" &&
+
+		git checkout -b "add_sub1" &&
+		git submodule add ./. sub1 &&
+		git config -f .gitmodules submodule.sub1.ignore all &&
+		git config submodule.sub1.ignore all &&
+		git add .gitmodules &&
+		git commit -m "Add sub1" &&
+		git checkout -b remove_sub1 &&
+		git revert HEAD &&
+
+		git checkout -b "modify_sub1" "add_sub1" &&
+		git submodule update &&
+		(
+			cd sub1 &&
+			git fetch &&
+			git checkout -b "modifications" &&
+			echo "z" >file2 &&
+			echo "x" >file3 &&
+			git add file2 file3 &&
+			git commit -m "modified file2 and added file3" &&
+			git push origin modifications
+		) &&
+		git add sub1 &&
+		git commit -m "Modify sub1" &&
+
+		git checkout -b "replace_sub1_with_directory" "add_sub1" &&
+		git submodule update &&
+		(
+			cd sub1 &&
+			git checkout modifications
+		) &&
+		git rm --cached sub1 &&
+		rm sub1/.git* &&
+		git config -f .gitmodules --remove-section "submodule.sub1" &&
+		git add .gitmodules sub1/* &&
+		git commit -m "Replace sub1 with directory" &&
+		git checkout -b replace_directory_with_sub1 &&
+		git revert HEAD &&
+
+		git checkout -b "replace_sub1_with_file" "add_sub1" &&
+		git rm sub1 &&
+		echo "content" >sub1 &&
+		git add sub1 &&
+		git commit -m "Replace sub1 with file" &&
+		git checkout -b replace_file_with_sub1 &&
+		git revert HEAD &&
+
+		git checkout -b "invalid_sub1" "add_sub1" &&
+		git update-index --cacheinfo 160000 0123456789012345678901234567890123456789 sub1 &&
+		git commit -m "Invalid sub1 commit" &&
+		git checkout -b valid_sub1 &&
+		git revert HEAD &&
+		git checkout master
+	)
+}
+
+# Helper function to replace gitfile with .git directory
+replace_gitfile_with_git_dir () {
+	(
+		cd "$1" &&
+		git_dir="$(git rev-parse --git-dir)" &&
+		rm -f .git &&
+		cp -R "$git_dir" .git &&
+		GIT_WORK_TREE=. git config --unset core.worktree
+	)
+}
+
+# Test that the .git directory in the submodule is unchanged (except for the
+# core.worktree setting, which we temporarily restore). Call this function
+# before test_submodule_content as the latter might write the index file
+# leading to false positive index differences.
+test_git_directory_is_unchanged () {
+	(
+		cd "$1" &&
+		git config core.worktree "../../../$1"
+	) &&
+	diff -r ".git/modules/$1" "$1/.git" &&
+	(
+		cd "$1" &&
+		GIT_WORK_TREE=. git config --unset core.worktree
+	)
+}
+
+# Helper function to be executed at the start of every test below, it sets up
+# the submodule repo if it doesn't exist and configures the most problematic
+# settings for diff.ignoreSubmodules.
+prolog () {
+	(test -d submodule_update_repo || create_lib_submodule_repo) &&
+	test_config_global diff.ignoreSubmodules all &&
+	test_config diff.ignoreSubmodules all
+}
+
+# Helper function to bring work tree back into the state given by the
+# commit. This includes trying to populate sub1 accordingly if it exists and
+# should be updated to an existing commit.
+reset_work_tree_to () {
+	rm -rf submodule_update &&
+	git clone submodule_update_repo submodule_update &&
+	(
+		cd submodule_update &&
+		rm -rf sub1 &&
+		git checkout -f "$1" &&
+		git status -u -s >actual &&
+		test_must_be_empty actual &&
+		sha1=$(git rev-parse --revs-only HEAD:sub1) &&
+		if test -n "$sha1" &&
+		   test $(cd "sub1" && git rev-parse --verify "$sha1^{commit}")
+		then
+			git submodule update --init --recursive "sub1"
+		fi
+	)
+}
+
+# Test that the superproject contains the content according to commit "$1"
+# (the work tree must match the index for everything but submodules but the
+# index must exactly match the given commit including any submodule SHA-1s).
+test_superproject_content () {
+	git diff-index --cached "$1" >actual &&
+	test_must_be_empty actual &&
+	git diff-files --ignore-submodules >actual &&
+	test_must_be_empty actual
+}
+
+# Test that the given submodule at path "$1" contains the content according
+# to the submodule commit recorded in the superproject's commit "$2"
+test_submodule_content () {
+	if test $# != 2
+	then
+		echo "test_submodule_content needs two arguments"
+		return 1
+	fi &&
+	submodule="$1" &&
+	commit="$2" &&
+	test -d "$submodule"/ &&
+	if ! test -f "$submodule"/.git && ! test -d "$submodule"/.git
+	then
+		echo "Submodule $submodule is not populated"
+		return 1
+	fi &&
+	sha1=$(git rev-parse --verify "$commit:$submodule") &&
+	if test -z "$sha1"
+	then
+		echo "Couldn't retrieve SHA-1 of $submodule for $commit"
+		return 1
+	fi &&
+	(
+		cd "$submodule" &&
+		git status -u -s >actual &&
+		test_must_be_empty actual &&
+		git diff "$sha1" >actual &&
+		test_must_be_empty actual
+	)
+}
+
+# Test that the following transitions are correctly handled:
+# - Updated submodule
+# - New submodule
+# - Removed submodule
+# - Directory containing tracked files replaced by submodule
+# - Submodule replaced by tracked files in directory
+# - Submodule replaced by tracked file with the same name
+# - tracked file replaced by submodule
+#
+# The default is that submodule contents aren't changed until "git submodule
+# update" is run. And even then that command doesn't delete the work tree of
+# a removed submodule.
+#
+# Removing a submodule containing a .git directory must fail even when forced
+# to protect the history!
+#
+
+# Test that submodule contents are currently not updated when switching
+# between commits that change a submodule.
+test_submodule_switch () {
+	command="$1"
+	######################### Appearing submodule #########################
+	# Switching to a commit letting a submodule appear creates empty dir ...
+	test_expect_success "$command: added submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... and doesn't care if it already exists ...
+	test_expect_success "$command: added submodule leaves existing empty directory alone" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			mkdir sub1 &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... unless there is an untracked file in its place.
+	test_expect_success "$command: added submodule doesn't remove untracked unignored file with same name" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			echo -n >sub1 &&
+			test_must_fail $command add_sub1 &&
+			test_superproject_content origin/no_submodule &&
+			test_must_be_empty sub1
+		)
+	'
+	# Replacing a tracked file with a submodule produces an empty
+	# directory ...
+	test_expect_success "$command: replace tracked file with submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_file &&
+		(
+			cd submodule_update &&
+			git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
+			$command replace_file_with_sub1 &&
+			test_superproject_content origin/replace_file_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_file_with_sub1
+		)
+	'
+	# ... as does removing a directory with tracked files with a
+	# submodule.
+	test_expect_success "$command: replace directory with submodule" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_directory &&
+		(
+			cd submodule_update &&
+			git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
+			$command replace_directory_with_sub1 &&
+			test_superproject_content origin/replace_directory_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_directory_with_sub1
+		)
+	'
+
+	######################## Disappearing submodule #######################
+	# Removing a submodule doesn't remove its work tree ...
+	test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			replace_gitfile_with_git_dir sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Replacing a submodule with files in a directory must fail as the
+	# submodule work tree isn't removed ...
+	test_expect_success "$command: replace submodule with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Replacing it with a file must fail as it could throw away any local
+	# work tree changes ...
+	test_expect_failure "$command: replace submodule with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... or even destroy unpushed parts of submodule history if that
+	# still uses a .git directory.
+	test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+
+	########################## Modified submodule #########################
+	# Updating a submodule sha1 doesn't update the submodule's work tree
+	test_expect_success "$command: modified submodule does not update submodule work tree" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t modify_sub1 origin/modify_sub1 &&
+			$command modify_sub1 &&
+			test_superproject_content origin/modify_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			git submodule update &&
+			test_submodule_content sub1 origin/modify_sub1
+		)
+	'
+
+	# Updating a submodule to an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will fail
+	test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t invalid_sub1 origin/invalid_sub1 &&
+			$command invalid_sub1 &&
+			test_superproject_content origin/invalid_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_must_fail git submodule update &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Updating a submodule from an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will succeed
+	test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
+		prolog &&
+		reset_work_tree_to invalid_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t valid_sub1 origin/valid_sub1 &&
+			$command valid_sub1 &&
+			test_superproject_content origin/valid_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/valid_sub1
+		)
+	'
+}
+
+# Test that submodule contents are currently not updated when switching
+# between commits that change a submodule, but throwing away local changes in
+# the superproject is allowed.
+test_submodule_forced_switch () {
+	command="$1"
+	######################### Appearing submodule #########################
+	# Switching to a commit letting a submodule appear creates empty dir ...
+	test_expect_success "$command: added submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... and doesn't care if it already exists ...
+	test_expect_success "$command: added submodule leaves existing empty directory alone" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			mkdir sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... unless there is an untracked file in its place.
+	test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
+		prolog &&
+		reset_work_tree_to no_submodule &&
+		(
+			cd submodule_update &&
+			git branch -t add_sub1 origin/add_sub1 &&
+			echo -n >sub1 &&
+			$command add_sub1 &&
+			test_superproject_content origin/add_sub1 &&
+			test_dir_is_empty sub1
+		)
+	'
+	# Replacing a tracked file with a submodule produces an empty
+	# directory ...
+	test_expect_success "$command: replace tracked file with submodule creates empty directory" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_file &&
+		(
+			cd submodule_update &&
+			git branch -t replace_file_with_sub1 origin/replace_file_with_sub1 &&
+			$command replace_file_with_sub1 &&
+			test_superproject_content origin/replace_file_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_file_with_sub1
+		)
+	'
+	# ... as does removing a directory with tracked files with a
+	# submodule.
+	test_expect_success "$command: replace directory with submodule" '
+		prolog &&
+		reset_work_tree_to replace_sub1_with_directory &&
+		(
+			cd submodule_update &&
+			git branch -t replace_directory_with_sub1 origin/replace_directory_with_sub1 &&
+			$command replace_directory_with_sub1 &&
+			test_superproject_content origin/replace_directory_with_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/replace_directory_with_sub1
+		)
+	'
+
+	######################## Disappearing submodule #######################
+	# Removing a submodule doesn't remove its work tree ...
+	test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t remove_sub1 origin/remove_sub1 &&
+			replace_gitfile_with_git_dir sub1 &&
+			$command remove_sub1 &&
+			test_superproject_content origin/remove_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Replacing a submodule with files in a directory must fail as the
+	# submodule work tree isn't removed ...
+	test_expect_failure "$command: replace submodule with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... especially when it contains a .git directory.
+	test_expect_failure "$command: replace submodule containing a .git directory with a directory must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_directory &&
+			test_superproject_content origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Replacing it with a file must fail as it could throw away any local
+	# work tree changes ...
+	test_expect_failure "$command: replace submodule with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# ... or even destroy unpushed parts of submodule history if that
+	# still uses a .git directory.
+	test_expect_failure "$command: replace submodule containing a .git directory with a file must fail" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+			replace_gitfile_with_git_dir sub1 &&
+			test_must_fail $command replace_sub1_with_file &&
+			test_superproject_content origin/add_sub1 &&
+			test_git_directory_is_unchanged sub1 &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+
+	########################## Modified submodule #########################
+	# Updating a submodule sha1 doesn't update the submodule's work tree
+	test_expect_success "$command: modified submodule does not update submodule work tree" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t modify_sub1 origin/modify_sub1 &&
+			$command modify_sub1 &&
+			test_superproject_content origin/modify_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			git submodule update &&
+			test_submodule_content sub1 origin/modify_sub1
+		)
+	'
+	# Updating a submodule to an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will fail
+	test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
+		prolog &&
+		reset_work_tree_to add_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t invalid_sub1 origin/invalid_sub1 &&
+			$command invalid_sub1 &&
+			test_superproject_content origin/invalid_sub1 &&
+			test_submodule_content sub1 origin/add_sub1 &&
+			test_must_fail git submodule update &&
+			test_submodule_content sub1 origin/add_sub1
+		)
+	'
+	# Updating a submodule from an invalid sha1 doesn't update the
+	# submodule's work tree, subsequent update will succeed
+	test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
+		prolog &&
+		reset_work_tree_to invalid_sub1 &&
+		(
+			cd submodule_update &&
+			git branch -t valid_sub1 origin/valid_sub1 &&
+			$command valid_sub1 &&
+			test_superproject_content origin/valid_sub1 &&
+			test_dir_is_empty sub1 &&
+			git submodule update --init --recursive &&
+			test_submodule_content sub1 origin/valid_sub1
+		)
+	'
+}
-- 
2.0.1.458.gf680257
Jens Lehmann· Jun 15, 2014, 16:58 UTC · re: Jens Lehmann · lore

[PATCH 03/14] checkout: call the new submodule update test framework

Test that the checkout command updates the work tree as expected with and without the '-f' flag.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/t2013-checkout-submodule.sh | 5 +++++
 1 file changed, 5 insertions(+)
Show changes to t/t2013-checkout-submodule.sh +5 −0
diff --git a/t/t2013-checkout-submodule.sh b/t/t2013-checkout-submodule.sh
index 06b18f8..6847f75 100755
--- a/t/t2013-checkout-submodule.sh
+++ b/t/t2013-checkout-submodule.sh
@@ -3,6 +3,7 @@
 test_description='checkout can handle submodules'

 . ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh

 test_expect_success 'setup' '
 	mkdir submodule &&
@@ -62,4 +63,8 @@ test_expect_success '"checkout <submodule>" honors submodule.*.ignore from .git/
 	! test -s actual
 '

+test_submodule_switch "git checkout"
+
+test_submodule_forced_switch "git checkout -f"
+
 test_done
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 15, 2014, 16:59 UTC · re: Jens Lehmann · lore

[PATCH 04/14] apply: add t4137 for submodule updates

Test that the apply command updates the work tree as expected for the '--index' and the '--3way' options (for submodule changes which don't result in conflicts).

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/t4137-apply-submodule.sh | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100755 t/t4137-apply-submodule.sh
Show changes to t/t4137-apply-submodule.sh +20 −0
diff --git a/t/t4137-apply-submodule.sh b/t/t4137-apply-submodule.sh
new file mode 100755
index 0000000..a9bd40a
--- /dev/null
+++ b/t/t4137-apply-submodule.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+test_description='git apply handling submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+apply_index () {
+	git diff --ignore-submodules=dirty "..$1" | git apply --index -
+}
+
+test_submodule_switch "apply_index"
+
+apply_3way () {
+	git diff --ignore-submodules=dirty "..$1" | git apply --3way -
+}
+
+test_submodule_switch "apply_3way"
+
+test_done
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 15, 2014, 16:59 UTC · re: Jens Lehmann · lore

[PATCH 05/14] read-tree: add t1013 for submodule updates

Test that the read-tree command updates the work tree as expected for changes which don't result in conflicts with the '-m' and '--reset' flag.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/t1013-read-tree-submodule.sh | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100755 t/t1013-read-tree-submodule.sh
Show changes to t/t1013-read-tree-submodule.sh +12 −0
diff --git a/t/t1013-read-tree-submodule.sh b/t/t1013-read-tree-submodule.sh
new file mode 100755
index 0000000..20526ae
--- /dev/null
+++ b/t/t1013-read-tree-submodule.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+test_description='read-tree can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+test_submodule_switch "git read-tree -u -m"
+
+test_submodule_forced_switch "git read-tree -u --reset"
+
+test_done
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 15, 2014, 17:00 UTC · re: Jens Lehmann · lore

[PATCH 06/14] reset: add t7112 for submodule updates

Test that the reset command updates the work tree as expected for changes with '--keep', '--merge' (for changes which don't result in conflicts) and '--hard'.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/t7112-reset-submodule.sh | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
 create mode 100755 t/t7112-reset-submodule.sh
Show changes to t/t7112-reset-submodule.sh +14 −0
diff --git a/t/t7112-reset-submodule.sh b/t/t7112-reset-submodule.sh
new file mode 100755
index 0000000..2eda6ad
--- /dev/null
+++ b/t/t7112-reset-submodule.sh
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+test_description='reset can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+test_submodule_switch "git reset --keep"
+
+test_submodule_switch "git reset --merge"
+
+test_submodule_forced_switch "git reset --hard"
+
+test_done
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 15, 2014, 17:01 UTC · re: Jens Lehmann · lore

[PATCH 07/14] bisect: add t6041 for submodule updates

Test that the bisect command updates the work tree as expected. To make that work with the new submodule test framework a git_bisect helper function is added. This adds a commit after the one given to be switched to and makes that one the bad commit. The starting point is then given to bisect as the good commit which makes bisect change the work tree to the commit in between, which is the commit given.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/t6041-bisect-submodule.sh | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100755 t/t6041-bisect-submodule.sh
Show changes to t/t6041-bisect-submodule.sh +32 −0
diff --git a/t/t6041-bisect-submodule.sh b/t/t6041-bisect-submodule.sh
new file mode 100755
index 0000000..4f37d24
--- /dev/null
+++ b/t/t6041-bisect-submodule.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+test_description='bisect can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+git_bisect () {
+	git status -su >expected &&
+	ls -1pR * >>expected &&
+	tar czf "$TRASH_DIRECTORY/tmp.tgz" * &&
+	GOOD=$(git rev-parse --verify HEAD) &&
+	git checkout "$1" &&
+	echo "foo" >bar &&
+	git add bar &&
+	git commit -m "bisect bad" &&
+	BAD=$(git rev-parse --verify HEAD) &&
+	git reset --hard HEAD^^ &&
+	git submodule update &&
+	git bisect start &&
+	git bisect good $GOOD &&
+	rm -rf * &&
+	tar xzf "$TRASH_DIRECTORY/tmp.tgz" &&
+	git status -su >actual &&
+	ls -1pR * >>actual &&
+	test_cmp expected actual &&
+	git bisect bad $BAD
+}
+
+test_submodule_switch "git_bisect"
+
+test_done
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 19, 2014, 20:12 UTC · re: Jens Lehmann · lore

[PATCH v2 07/14] bisect: add t6041 for submodule updates

Test that the bisect command updates the work tree as expected. To make that work with the new submodule test framework a git_bisect helper function is added. This adds a commit after the one given to be switched to and makes that one the bad commit. The starting point is then given to bisect as the good commit which makes bisect change the work tree to the commit in between, which is the commit given.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
Changes to v1:
*) use "expect" instead of "expected"
 t/t6041-bisect-submodule.sh | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100755 t/t6041-bisect-submodule.sh
Show changes to t/t6041-bisect-submodule.sh +32 −0
diff --git a/t/t6041-bisect-submodule.sh b/t/t6041-bisect-submodule.sh
new file mode 100755
index 0000000..c6b7aa6
--- /dev/null
+++ b/t/t6041-bisect-submodule.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+test_description='bisect can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+git_bisect () {
+	git status -su >expect &&
+	ls -1pR * >>expect &&
+	tar czf "$TRASH_DIRECTORY/tmp.tgz" * &&
+	GOOD=$(git rev-parse --verify HEAD) &&
+	git checkout "$1" &&
+	echo "foo" >bar &&
+	git add bar &&
+	git commit -m "bisect bad" &&
+	BAD=$(git rev-parse --verify HEAD) &&
+	git reset --hard HEAD^^ &&
+	git submodule update &&
+	git bisect start &&
+	git bisect good $GOOD &&
+	rm -rf * &&
+	tar xzf "$TRASH_DIRECTORY/tmp.tgz" &&
+	git status -su >actual &&
+	ls -1pR * >>actual &&
+	test_cmp expect actual &&
+	git bisect bad $BAD
+}
+
+test_submodule_switch "git_bisect"
+
+test_done
-- 
2.0.0.406.gf4dce28
Jens Lehmann· Jun 15, 2014, 17:01 UTC · re: Jens Lehmann · lore

[PATCH 08/14] merge: add t7613 for submodule updates

Test that the merge command updates the work tree as expected (for submodule changes which don't result in conflicts) when used without arguments or with the '--ff', '--ff-only' and '--no-ff' flag.

Implement the KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR switch to expect the known failure that --no-ff merges do not create the empty submodule directory.

The KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES switch is also implemented to expect the known failure that --no-ff merges attempt to merge the new files in the former submodule directory with those of the removed submodule.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/lib-submodule-update.sh  | 24 +++++++++++++++++++++---
 t/t7613-merge-submodule.sh | 19 +++++++++++++++++++
 2 files changed, 40 insertions(+), 3 deletions(-)
 create mode 100755 t/t7613-merge-submodule.sh
Show changes to 2 files +40 −3

t/lib-submodule-update.sh, t/t7613-merge-submodule.sh

diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
index c6c842a..bc9415c 100755
--- a/t/lib-submodule-update.sh
+++ b/t/lib-submodule-update.sh
@@ -275,7 +275,16 @@ test_submodule_switch () {
 	'
 	# ... as does removing a directory with tracked files with a
 	# submodule.
-	test_expect_success "$command: replace directory with submodule" '
+	if test "$KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR" = 1
+	then
+		# Non fast-forward merges fail with "Directory sub1 doesn't
+		# exist. sub1" because the empty submodule directory is not
+		# created
+		RESULT="failure"
+	else
+		RESULT="success"
+	fi
+	test_expect_$RESULT "$command: replace directory with submodule" '
 		prolog &&
 		reset_work_tree_to replace_sub1_with_directory &&
 		(
@@ -318,7 +327,16 @@ test_submodule_switch () {
 	'
 	# Replacing a submodule with files in a directory must fail as the
 	# submodule work tree isn't removed ...
-	test_expect_success "$command: replace submodule with a directory must fail" '
+	if test "$KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES" = 1
+	then
+		# Non fast-forward merges attempt to merge the former
+		# submodule files with the newly checked out ones in the
+		# directory of the same name while it shouldn't.
+		RESULT="failure"
+	else
+		RESULT="success"
+	fi
+	test_expect_$RESULT "$command: replace submodule with a directory must fail" '
 		prolog &&
 		reset_work_tree_to add_sub1 &&
 		(
@@ -330,7 +348,7 @@ test_submodule_switch () {
 		)
 	'
 	# ... especially when it contains a .git directory.
-	test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
+	test_expect_$RESULT "$command: replace submodule containing a .git directory with a directory must fail" '
 		prolog &&
 		reset_work_tree_to add_sub1 &&
 		(
diff --git a/t/t7613-merge-submodule.sh b/t/t7613-merge-submodule.sh
new file mode 100755
index 0000000..d1e9fcc
--- /dev/null
+++ b/t/t7613-merge-submodule.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+test_description='merge can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+# merges without conflicts
+test_submodule_switch "git merge"
+
+test_submodule_switch "git merge --ff"
+
+test_submodule_switch "git merge --ff-only"
+
+KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES=1
+test_submodule_switch "git merge --no-ff"
+
+test_done
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 15, 2014, 17:02 UTC · re: Jens Lehmann · lore

[PATCH 09/14] rebase: add t3426 for submodule updates

Test that the rebase command updates the work tree as expected for changes which don't result in conflicts. To make that work add two helper functions that add a commit only touching files and then revert it. This allows to rebase the target commit over these two and to compare the result.

Set KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR to document that "replace directory with submodule" fails for an interactive rebase because a directory "sub1" already exists.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/t3426-rebase-submodule.sh | 46 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
 create mode 100755 t/t3426-rebase-submodule.sh
Show changes to t/t3426-rebase-submodule.sh +46 −0
diff --git a/t/t3426-rebase-submodule.sh b/t/t3426-rebase-submodule.sh
new file mode 100755
index 0000000..019ce52
--- /dev/null
+++ b/t/t3426-rebase-submodule.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+test_description='rebase can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+git_rebase () {
+	git status -su >expected &&
+	ls -1pR * >>expected &&
+	git checkout -b ours HEAD &&
+	echo x >>file1 &&
+	git add file1 &&
+	git commit -m add_x &&
+	git revert HEAD &&
+	git status -su >actual &&
+	ls -1pR * >>actual &&
+	test_cmp expected actual &&
+	git rebase "$1"
+}
+
+test_submodule_switch "git_rebase"
+
+git_rebase_interactive () {
+	git status -su >expected &&
+	ls -1pR * >>expected &&
+	git checkout -b ours HEAD &&
+	echo x >>file1 &&
+	git add file1 &&
+	git commit -m add_x &&
+	git revert HEAD &&
+	git status -su >actual &&
+	ls -1pR * >>actual &&
+	test_cmp expected actual &&
+	set_fake_editor &&
+	echo "fake-editor.sh" >.git/info/exclude
+	git rebase -i "$1"
+}
+
+KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+# The real reason "replace directory with submodule" fails is because a
+# directory "sub1" exists, but we reuse the suppression added for merge here
+test_submodule_switch "git_rebase_interactive"
+
+test_done
-- 
2.0.0.275.gc479268
Eric Sunshine· Jun 16, 2014, 09:57 UTC · re: Jens Lehmann · lore

Re: [PATCH 09/14] rebase: add t3426 for submodule updates

On Sun, Jun 15, 2014 at 1:02 PM, Jens Lehmann <Jens.Lehmann@web.de> wrote:
Show 59 quoted lines
> Test that the rebase command updates the work tree as expected for
> changes which don't result in conflicts. To make that work add two
> helper functions that add a commit only touching files and then
> revert it. This allows to rebase the target commit over these two
> and to compare the result.
>
> Set KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR to
> document that "replace directory with submodule" fails for an
> interactive rebase because a directory "sub1" already exists.
>
> Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
> ---
>  t/t3426-rebase-submodule.sh | 46 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)
>  create mode 100755 t/t3426-rebase-submodule.sh
>
> diff --git a/t/t3426-rebase-submodule.sh b/t/t3426-rebase-submodule.sh
> new file mode 100755
> index 0000000..019ce52
> --- /dev/null
> +++ b/t/t3426-rebase-submodule.sh
> @@ -0,0 +1,46 @@
> +#!/bin/sh
> +
> +test_description='rebase can handle submodules'
> +
> +. ./test-lib.sh
> +. "$TEST_DIRECTORY"/lib-submodule-update.sh
> +. "$TEST_DIRECTORY"/lib-rebase.sh
> +
> +git_rebase () {
> +       git status -su >expected &&
> +       ls -1pR * >>expected &&
> +       git checkout -b ours HEAD &&
> +       echo x >>file1 &&
> +       git add file1 &&
> +       git commit -m add_x &&
> +       git revert HEAD &&
> +       git status -su >actual &&
> +       ls -1pR * >>actual &&
> +       test_cmp expected actual &&
> +       git rebase "$1"
> +}
> +
> +test_submodule_switch "git_rebase"
> +
> +git_rebase_interactive () {
> +       git status -su >expected &&
> +       ls -1pR * >>expected &&
> +       git checkout -b ours HEAD &&
> +       echo x >>file1 &&
> +       git add file1 &&
> +       git commit -m add_x &&
> +       git revert HEAD &&
> +       git status -su >actual &&
> +       ls -1pR * >>actual &&
> +       test_cmp expected actual &&
> +       set_fake_editor &&
> +       echo "fake-editor.sh" >.git/info/exclude
Broken &&-chain.
Show 11 quoted lines
> +       git rebase -i "$1"
> +}
> +
> +KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
> +# The real reason "replace directory with submodule" fails is because a
> +# directory "sub1" exists, but we reuse the suppression added for merge here
> +test_submodule_switch "git_rebase_interactive"
> +
> +test_done
> --
> 2.0.0.275.gc479268
Jens Lehmann· Jun 17, 2014, 17:41 UTC · re: Eric Sunshine · lore

Re: [PATCH 09/14] rebase: add t3426 for submodule updates

Am 16.06.2014 11:57, schrieb Eric Sunshine:
Show 62 quoted lines
> On Sun, Jun 15, 2014 at 1:02 PM, Jens Lehmann <Jens.Lehmann@web.de> wrote:
>> Test that the rebase command updates the work tree as expected for
>> changes which don't result in conflicts. To make that work add two
>> helper functions that add a commit only touching files and then
>> revert it. This allows to rebase the target commit over these two
>> and to compare the result.
>>
>> Set KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR to
>> document that "replace directory with submodule" fails for an
>> interactive rebase because a directory "sub1" already exists.
>>
>> Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
>> ---
>>  t/t3426-rebase-submodule.sh | 46 +++++++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 46 insertions(+)
>>  create mode 100755 t/t3426-rebase-submodule.sh
>>
>> diff --git a/t/t3426-rebase-submodule.sh b/t/t3426-rebase-submodule.sh
>> new file mode 100755
>> index 0000000..019ce52
>> --- /dev/null
>> +++ b/t/t3426-rebase-submodule.sh
>> @@ -0,0 +1,46 @@
>> +#!/bin/sh
>> +
>> +test_description='rebase can handle submodules'
>> +
>> +. ./test-lib.sh
>> +. "$TEST_DIRECTORY"/lib-submodule-update.sh
>> +. "$TEST_DIRECTORY"/lib-rebase.sh
>> +
>> +git_rebase () {
>> +       git status -su >expected &&
>> +       ls -1pR * >>expected &&
>> +       git checkout -b ours HEAD &&
>> +       echo x >>file1 &&
>> +       git add file1 &&
>> +       git commit -m add_x &&
>> +       git revert HEAD &&
>> +       git status -su >actual &&
>> +       ls -1pR * >>actual &&
>> +       test_cmp expected actual &&
>> +       git rebase "$1"
>> +}
>> +
>> +test_submodule_switch "git_rebase"
>> +
>> +git_rebase_interactive () {
>> +       git status -su >expected &&
>> +       ls -1pR * >>expected &&
>> +       git checkout -b ours HEAD &&
>> +       echo x >>file1 &&
>> +       git add file1 &&
>> +       git commit -m add_x &&
>> +       git revert HEAD &&
>> +       git status -su >actual &&
>> +       ls -1pR * >>actual &&
>> +       test_cmp expected actual &&
>> +       set_fake_editor &&
>> +       echo "fake-editor.sh" >.git/info/exclude
> 
> Broken &&-chain.
Thanks for spotting!
Show 16 quoted lines
>> +       git rebase -i "$1"
>> +}
>> +
>> +KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
>> +# The real reason "replace directory with submodule" fails is because a
>> +# directory "sub1" exists, but we reuse the suppression added for merge here
>> +test_submodule_switch "git_rebase_interactive"
>> +
>> +test_done
>> --
>> 2.0.0.275.gc479268
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
Jens Lehmann· Jun 19, 2014, 20:12 UTC · re: Jens Lehmann · lore

[PATCH v2 09/14] rebase: add t3426 for submodule updates

Test that the rebase command updates the work tree as expected for changes which don't result in conflicts. To make that work add two helper functions that add a commit only touching files and then revert it. This allows to rebase the target commit over these two and to compare the result.

Set KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR to document that "replace directory with submodule" fails for an interactive rebase because a directory "sub1" already exists.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
Changes to v1:
*) fix broken &&-chain (you have this as "SQUASH???" commit in pu)
*) use "expect" instead of "expected"
 t/t3426-rebase-submodule.sh | 46 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
 create mode 100755 t/t3426-rebase-submodule.sh
Show changes to t/t3426-rebase-submodule.sh +46 −0
diff --git a/t/t3426-rebase-submodule.sh b/t/t3426-rebase-submodule.sh
new file mode 100755
index 0000000..d5b896d
--- /dev/null
+++ b/t/t3426-rebase-submodule.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+test_description='rebase can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+git_rebase () {
+	git status -su >expect &&
+	ls -1pR * >>expect &&
+	git checkout -b ours HEAD &&
+	echo x >>file1 &&
+	git add file1 &&
+	git commit -m add_x &&
+	git revert HEAD &&
+	git status -su >actual &&
+	ls -1pR * >>actual &&
+	test_cmp expect actual &&
+	git rebase "$1"
+}
+
+test_submodule_switch "git_rebase"
+
+git_rebase_interactive () {
+	git status -su >expect &&
+	ls -1pR * >>expect &&
+	git checkout -b ours HEAD &&
+	echo x >>file1 &&
+	git add file1 &&
+	git commit -m add_x &&
+	git revert HEAD &&
+	git status -su >actual &&
+	ls -1pR * >>actual &&
+	test_cmp expect actual &&
+	set_fake_editor &&
+	echo "fake-editor.sh" >.git/info/exclude &&
+	git rebase -i "$1"
+}
+
+KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+# The real reason "replace directory with submodule" fails is because a
+# directory "sub1" exists, but we reuse the suppression added for merge here
+test_submodule_switch "git_rebase_interactive"
+
+test_done
-- 
2.0.0.406.gf4dce28
Jens Lehmann· Jun 15, 2014, 17:02 UTC · re: Jens Lehmann · lore

[PATCH 10/14] pull: add t5572 for submodule updates

Test that the pull command updates the work tree as expected (for submodule changes which don't result in conflicts) when used without arguments or with the '--ff', '--ff-only' and '--no-ff' flag each. Add helper functions to reset the branch to be updated to to the current HEAD so that pull is doing the transition from HEAD to the given branch.

Set KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES and KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR to document that pull has the same --no-ff known failures merge has.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/t5572-pull-submodule.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
 create mode 100755 t/t5572-pull-submodule.sh
Show changes to t/t5572-pull-submodule.sh +45 −0
diff --git a/t/t5572-pull-submodule.sh b/t/t5572-pull-submodule.sh
new file mode 100755
index 0000000..accfa5c
--- /dev/null
+++ b/t/t5572-pull-submodule.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description='pull can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+reset_branch_to_HEAD () {
+	git branch -D "$1" &&
+	git checkout -b "$1" HEAD &&
+	git branch --set-upstream-to="origin/$1" "$1"
+}
+
+git_pull () {
+	reset_branch_to_HEAD "$1" &&
+	git pull
+}
+
+# pulls without conflicts
+test_submodule_switch "git_pull"
+
+git_pull_ff () {
+	reset_branch_to_HEAD "$1" &&
+	git pull --ff
+}
+
+test_submodule_switch "git_pull_ff"
+
+git_pull_ff_only () {
+	reset_branch_to_HEAD "$1" &&
+	git pull --ff-only
+}
+
+test_submodule_switch "git_pull_ff_only"
+
+git_pull_noff () {
+	reset_branch_to_HEAD "$1" &&
+	git pull --no-ff
+}
+
+KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES=1
+test_submodule_switch "git_pull_noff"
+
+test_done
-- 
2.0.0.275.gba02dd0
Jens Lehmann· Jun 15, 2014, 17:03 UTC · re: Jens Lehmann · lore

[PATCH 11/14] cherry-pick: add t3512 for submodule updates

Test that the cherry-pick command updates the work tree as expected (for submodule changes which don't result in conflicts).

Set KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES and KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR to document that cherry-pick has the same --no-ff known failures merge has.

Implement the KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT switch to expect the known failure that while cherry picking just a SHA-1 update for an ignored submodule the commit incorrectly fails with "The previous cherry-pick is now empty, possibly due to conflict resolution.".

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/lib-submodule-update.sh        | 15 ++++++++++++---
 t/t3512-cherry-pick-submodule.sh | 13 +++++++++++++
 2 files changed, 25 insertions(+), 3 deletions(-)
 create mode 100755 t/t3512-cherry-pick-submodule.sh
Show changes to 2 files +25 −3

t/lib-submodule-update.sh, t/t3512-cherry-pick-submodule.sh

diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
index bc9415c..95e041b 100755
--- a/t/lib-submodule-update.sh
+++ b/t/lib-submodule-update.sh
@@ -392,7 +392,16 @@ test_submodule_switch () {

 	########################## Modified submodule #########################
 	# Updating a submodule sha1 doesn't update the submodule's work tree
-	test_expect_success "$command: modified submodule does not update submodule work tree" '
+	if test "$KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT" = 1
+	then
+		# When cherry picking a SHA-1 update for an ignored submodule
+		# the commit incorrectly fails with "The previous cherry-pick
+		# is now empty, possibly due to conflict resolution."
+		RESULT="failure"
+	else
+		RESULT="success"
+	fi
+	test_expect_$RESULT "$command: modified submodule does not update submodule work tree" '
 		prolog &&
 		reset_work_tree_to add_sub1 &&
 		(
@@ -408,7 +417,7 @@ test_submodule_switch () {

 	# Updating a submodule to an invalid sha1 doesn't update the
 	# submodule's work tree, subsequent update will fail
-	test_expect_success "$command: modified submodule does not update submodule work tree to invalid commit" '
+	test_expect_$RESULT "$command: modified submodule does not update submodule work tree to invalid commit" '
 		prolog &&
 		reset_work_tree_to add_sub1 &&
 		(
@@ -423,7 +432,7 @@ test_submodule_switch () {
 	'
 	# Updating a submodule from an invalid sha1 doesn't update the
 	# submodule's work tree, subsequent update will succeed
-	test_expect_success "$command: modified submodule does not update submodule work tree from invalid commit" '
+	test_expect_$RESULT "$command: modified submodule does not update submodule work tree from invalid commit" '
 		prolog &&
 		reset_work_tree_to invalid_sub1 &&
 		(
diff --git a/t/t3512-cherry-pick-submodule.sh b/t/t3512-cherry-pick-submodule.sh
new file mode 100755
index 0000000..6863b7b
--- /dev/null
+++ b/t/t3512-cherry-pick-submodule.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+test_description='cherry-pick can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT=1
+KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES=1
+test_submodule_switch "git cherry-pick"
+
+test_done
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 15, 2014, 17:03 UTC · re: Jens Lehmann · lore

[PATCH 12/14] am: add t4255 for submodule updates

Test that the am command updates the work tree as expected (for submodule changes which don't result in conflicts). To make that work add two helper functions that use format-patch to create the input for am.

Add the KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES switch to expect the known failure that --no-ff merges attempt to merge the new files in the former submodule directory with those of the removed submodule.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/t4255-am-submodule.sh | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100755 t/t4255-am-submodule.sh
Show changes to t/t4255-am-submodule.sh +21 −0
diff --git a/t/t4255-am-submodule.sh b/t/t4255-am-submodule.sh
new file mode 100755
index 0000000..8bde7db
--- /dev/null
+++ b/t/t4255-am-submodule.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+test_description='git am handling submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+am () {
+	git format-patch --stdout --ignore-submodules=dirty "..$1" | git am -
+}
+
+test_submodule_switch "am"
+
+am_3way () {
+	git format-patch --stdout --ignore-submodules=dirty "..$1" | git am --3way -
+}
+
+KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES=1
+test_submodule_switch "am_3way"
+
+test_done
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 15, 2014, 17:04 UTC · re: Jens Lehmann · lore

[PATCH 13/14] stash: add t3906 for submodule updates

Test that the stash apply command updates the work tree as expected for changes which don't result in conflicts. To make that work add a helper function that uses read-tree to apply the changes of the target commit to the work tree, then stashes these changes and at last applies that stash.

Implement the KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES switch and reuse two other already present switches to expect the known failure that stash does ignore submodule changes.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/lib-submodule-update.sh  | 23 ++++++++++++++++++-----
 t/t3906-stash-submodule.sh | 24 ++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 5 deletions(-)
 create mode 100755 t/t3906-stash-submodule.sh
Show changes to 2 files +42 −5

t/lib-submodule-update.sh, t/t3906-stash-submodule.sh

diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
index 95e041b..9901b45 100755
--- a/t/lib-submodule-update.sh
+++ b/t/lib-submodule-update.sh
@@ -217,7 +217,14 @@ test_submodule_switch () {
 	command="$1"
 	######################### Appearing submodule #########################
 	# Switching to a commit letting a submodule appear creates empty dir ...
-	test_expect_success "$command: added submodule creates empty directory" '
+	if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
+	then
+		# Restoring stash fails to restore submodule index entry
+		RESULT="failure"
+	else
+		RESULT="success"
+	fi
+	test_expect_$RESULT "$command: added submodule creates empty directory" '
 		prolog &&
 		reset_work_tree_to no_submodule &&
 		(
@@ -231,7 +238,7 @@ test_submodule_switch () {
 		)
 	'
 	# ... and doesn't care if it already exists ...
-	test_expect_success "$command: added submodule leaves existing empty directory alone" '
+	test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
 		prolog &&
 		reset_work_tree_to no_submodule &&
 		(
@@ -260,7 +267,7 @@ test_submodule_switch () {
 	'
 	# Replacing a tracked file with a submodule produces an empty
 	# directory ...
-	test_expect_success "$command: replace tracked file with submodule creates empty directory" '
+	test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
 		prolog &&
 		reset_work_tree_to replace_sub1_with_file &&
 		(
@@ -300,7 +307,13 @@ test_submodule_switch () {

 	######################## Disappearing submodule #######################
 	# Removing a submodule doesn't remove its work tree ...
-	test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
+	if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
+	then
+		RESULT="failure"
+	else
+		RESULT="success"
+	fi
+	test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
 		prolog &&
 		reset_work_tree_to add_sub1 &&
 		(
@@ -312,7 +325,7 @@ test_submodule_switch () {
 		)
 	'
 	# ... especially when it contains a .git directory.
-	test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
+	test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
 		prolog &&
 		reset_work_tree_to add_sub1 &&
 		(
diff --git a/t/t3906-stash-submodule.sh b/t/t3906-stash-submodule.sh
new file mode 100755
index 0000000..aaee424
--- /dev/null
+++ b/t/t3906-stash-submodule.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+test_description='stash apply can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+git_stash () {
+	git status -su >expected &&
+	ls -1pR * >>expected &&
+	git read-tree -u -m "$1" &&
+	git stash &&
+	git status -su >actual &&
+	ls -1pR * >>actual &&
+	test_cmp expected actual &&
+	git stash apply
+}
+
+KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES=1
+KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT=1
+KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+test_submodule_switch "git_stash"
+
+test_done
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 19, 2014, 20:12 UTC · re: Jens Lehmann · lore

[PATCH v2 13/14] stash: add t3906 for submodule updates

Test that the stash apply command updates the work tree as expected for changes which don't result in conflicts. To make that work add a helper function that uses read-tree to apply the changes of the target commit to the work tree, then stashes these changes and at last applies that stash.

Implement the KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES switch and reuse two other already present switches to expect the known failure that stash does ignore submodule changes.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
Changes to v1:
*) use "expect" instead of "expected"
 t/lib-submodule-update.sh  | 23 ++++++++++++++++++-----
 t/t3906-stash-submodule.sh | 24 ++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 5 deletions(-)
 create mode 100755 t/t3906-stash-submodule.sh
Show changes to 2 files +42 −5

t/lib-submodule-update.sh, t/t3906-stash-submodule.sh

diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
index a38cf52..2a504b2 100755
--- a/t/lib-submodule-update.sh
+++ b/t/lib-submodule-update.sh
@@ -219,7 +219,14 @@ test_submodule_switch () {
 	command="$1"
 	######################### Appearing submodule #########################
 	# Switching to a commit letting a submodule appear creates empty dir ...
-	test_expect_success "$command: added submodule creates empty directory" '
+	if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
+	then
+		# Restoring stash fails to restore submodule index entry
+		RESULT="failure"
+	else
+		RESULT="success"
+	fi
+	test_expect_$RESULT "$command: added submodule creates empty directory" '
 		prolog &&
 		reset_work_tree_to no_submodule &&
 		(
@@ -233,7 +240,7 @@ test_submodule_switch () {
 		)
 	'
 	# ... and doesn't care if it already exists ...
-	test_expect_success "$command: added submodule leaves existing empty directory alone" '
+	test_expect_$RESULT "$command: added submodule leaves existing empty directory alone" '
 		prolog &&
 		reset_work_tree_to no_submodule &&
 		(
@@ -262,7 +269,7 @@ test_submodule_switch () {
 	'
 	# Replacing a tracked file with a submodule produces an empty
 	# directory ...
-	test_expect_success "$command: replace tracked file with submodule creates empty directory" '
+	test_expect_$RESULT "$command: replace tracked file with submodule creates empty directory" '
 		prolog &&
 		reset_work_tree_to replace_sub1_with_file &&
 		(
@@ -302,7 +309,13 @@ test_submodule_switch () {

 	######################## Disappearing submodule #######################
 	# Removing a submodule doesn't remove its work tree ...
-	test_expect_success "$command: removed submodule leaves submodule directory and its contents in place" '
+	if test "$KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES" = 1
+	then
+		RESULT="failure"
+	else
+		RESULT="success"
+	fi
+	test_expect_$RESULT "$command: removed submodule leaves submodule directory and its contents in place" '
 		prolog &&
 		reset_work_tree_to add_sub1 &&
 		(
@@ -314,7 +327,7 @@ test_submodule_switch () {
 		)
 	'
 	# ... especially when it contains a .git directory.
-	test_expect_success "$command: removed submodule leaves submodule containing a .git directory alone" '
+	test_expect_$RESULT "$command: removed submodule leaves submodule containing a .git directory alone" '
 		prolog &&
 		reset_work_tree_to add_sub1 &&
 		(
diff --git a/t/t3906-stash-submodule.sh b/t/t3906-stash-submodule.sh
new file mode 100755
index 0000000..d7219d6
--- /dev/null
+++ b/t/t3906-stash-submodule.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+test_description='stash apply can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+git_stash () {
+	git status -su >expect &&
+	ls -1pR * >>expect &&
+	git read-tree -u -m "$1" &&
+	git stash &&
+	git status -su >actual &&
+	ls -1pR * >>actual &&
+	test_cmp expect actual &&
+	git stash apply
+}
+
+KNOWN_FAILURE_STASH_DOES_IGNORE_SUBMODULE_CHANGES=1
+KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT=1
+KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+test_submodule_switch "git_stash"
+
+test_done
-- 
2.0.0.406.gf4dce28
Jens Lehmann· Jun 15, 2014, 17:04 UTC · re: Jens Lehmann · lore

[PATCH 14/14] revert: add t3513 for submodule updates

Test that the revert command updates the work tree as expected (for submodule changes which don't result in conflicts). Add a helper function to first revert the checked out target commit to make the last revert produce the to-be-tested work tree.

Set the KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT and KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR switches to document that revert has the similar failures.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
 t/t3513-revert-submodule.sh | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100755 t/t3513-revert-submodule.sh
Show changes to t/t3513-revert-submodule.sh +32 −0
diff --git a/t/t3513-revert-submodule.sh b/t/t3513-revert-submodule.sh
new file mode 100755
index 0000000..57e2d75
--- /dev/null
+++ b/t/t3513-revert-submodule.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+test_description='revert can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+# Create a revert that moves from HEAD (including any test modifications to
+# the work tree) to $1 by first checking out $1 and reverting it. Reverting
+# the revert is the transition we test for. We tar the current work tree
+# first so we can restore the work tree test setup after doing the checkout
+# and revert.  We test here that the restored work tree content is identical
+# to that at the beginning. The last revert is then tested by the framework.
+git_revert () {
+	git status -su >expected &&
+	ls -1pR * >>expected &&
+	tar czf "$TRASH_DIRECTORY/tmp.tgz" * &&
+	git checkout "$1" &&
+	git revert HEAD &&
+	rm -rf * &&
+	tar xzf "$TRASH_DIRECTORY/tmp.tgz" &&
+	git status -su >actual &&
+	ls -1pR * >>actual &&
+	test_cmp expected actual &&
+	git revert HEAD
+}
+
+KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT=1
+KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+test_submodule_switch "git_revert"
+
+test_done
-- 
2.0.0.275.gc479268
Jens Lehmann· Jun 19, 2014, 20:12 UTC · re: Jens Lehmann · lore

[PATCH v2 14/14] revert: add t3513 for submodule updates

Test that the revert command updates the work tree as expected (for submodule changes which don't result in conflicts). Add a helper function to first revert the checked out target commit to make the last revert produce the to-be-tested work tree.

Set the KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT and KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR switches to document that revert has the similar failures.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
Changes to v1:
*) use "expect" instead of "expected"
 t/t3513-revert-submodule.sh | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100755 t/t3513-revert-submodule.sh
Show changes to t/t3513-revert-submodule.sh +32 −0
diff --git a/t/t3513-revert-submodule.sh b/t/t3513-revert-submodule.sh
new file mode 100755
index 0000000..a1c4e02
--- /dev/null
+++ b/t/t3513-revert-submodule.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+test_description='revert can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+# Create a revert that moves from HEAD (including any test modifications to
+# the work tree) to $1 by first checking out $1 and reverting it. Reverting
+# the revert is the transition we test for. We tar the current work tree
+# first so we can restore the work tree test setup after doing the checkout
+# and revert.  We test here that the restored work tree content is identical
+# to that at the beginning. The last revert is then tested by the framework.
+git_revert () {
+	git status -su >expect &&
+	ls -1pR * >>expect &&
+	tar czf "$TRASH_DIRECTORY/tmp.tgz" * &&
+	git checkout "$1" &&
+	git revert HEAD &&
+	rm -rf * &&
+	tar xzf "$TRASH_DIRECTORY/tmp.tgz" &&
+	git status -su >actual &&
+	ls -1pR * >>actual &&
+	test_cmp expect actual &&
+	git revert HEAD
+}
+
+KNOWN_FAILURE_CHERRY_PICK_SEES_EMPTY_COMMIT=1
+KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+test_submodule_switch "git_revert"
+
+test_done
-- 
2.0.0.406.gf4dce28
Torsten Bögershausen· Jul 2, 2014, 14:54 UTC · re: Jens Lehmann · lore

Re: [PATCH 00/14] Add submodule test harness

(Not sure if this is the right thread) (I haven't checked if this is fixed in your latest version)

On what I have on pu 7a0da7902cbbc9a876b90c9, Tue Jul 1 14:51:53 2014 -0700

Many submodule tests are broken. One problem is here:

lib-submodule-update.sh:264: possible problem: echo -n is not portable (please use printf): echo -n >sub1 && lib-submodule-update.sh:507: possible problem: echo -n is not portable (please use printf): echo -n >sub1 &&

You can remove the "empty" "echo -n" to create an empty file:
>sub1 &&
Jens Lehmann· Jul 2, 2014, 19:57 UTC · re: Torsten Bögershausen · lore

Re: [PATCH 00/14] Add submodule test harness

Am 02.07.2014 16:54, schrieb Torsten Bögershausen:
> (Not sure if this is the right thread)
> (I haven't checked if this is fixed in your latest version)
Yes, this is the right thread and no, it isn't fixed yet.
Show 10 quoted lines
> On what I have on pu 7a0da7902cbbc9a876b90c9, Tue Jul 1 14:51:53 2014 -0700
> 
> Many submodule tests are broken.
> One problem is here:
> 
> lib-submodule-update.sh:264: possible problem: echo -n is not portable (please use printf):                     echo -n >sub1 &&
> lib-submodule-update.sh:507: possible problem: echo -n is not portable (please use printf):                     echo -n >sub1 &&
> 
> You can remove the "empty" "echo -n" to create an empty file:
>> sub1 &&

Thanks for spotting and diagnosing this. Running "make lint" in the test directory only feeds the tests to check-non-portable-shell.pl, but not the *lib*.sh helper scripts, which made me miss this one.

The following diff should fix it for you. Am I understanding you correctly that you are experiencing other failures too? I see no other incompatibilities when running ./check-non-portable-shell.pl on all the shell scripts in the repo.

-----8<----
Show changes to t/lib-submodule-update.sh +2 −2
diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
index 24c9fd7..3584755 100755
--- a/t/lib-submodule-update.sh
+++ b/t/lib-submodule-update.sh
@@ -304,7 +304,7 @@ test_submodule_switch () {
 		(
 			cd submodule_update &&
 			git branch -t add_sub1 origin/add_sub1 &&
-			echo -n >sub1 &&
+			>sub1 &&
 			test_must_fail $command add_sub1 &&
 			test_superproject_content origin/no_submodule &&
 			test_must_be_empty sub1
@@ -547,7 +547,7 @@ test_submodule_forced_switch () {
 		(
 			cd submodule_update &&
 			git branch -t add_sub1 origin/add_sub1 &&
-			echo -n >sub1 &&
+			>sub1 &&
 			$command add_sub1 &&
 			test_superproject_content origin/add_sub1 &&
 			test_dir_is_empty sub1
-- 
2.0.1.458.gf680257.dirty
Torsten Bögershausen· Jul 3, 2014, 05:56 UTC · re: Jens Lehmann · lore

Re: [PATCH 00/14] Add submodule test harness

On 07/02/2014 09:57 PM, Jens Lehmann wrote:
Show 23 quoted lines
> Am 02.07.2014 16:54, schrieb Torsten Bögershausen:
>> (Not sure if this is the right thread)
>> (I haven't checked if this is fixed in your latest version)
> Yes, this is the right thread and no, it isn't fixed yet.
>
>> On what I have on pu 7a0da7902cbbc9a876b90c9, Tue Jul 1 14:51:53 2014 -0700
>>
>> Many submodule tests are broken.
>> One problem is here:
>>
>> lib-submodule-update.sh:264: possible problem: echo -n is not portable (please use printf):                     echo -n >sub1 &&
>> lib-submodule-update.sh:507: possible problem: echo -n is not portable (please use printf):                     echo -n >sub1 &&
>>
>> You can remove the "empty" "echo -n" to create an empty file:
>>> sub1 &&
> Thanks for spotting and diagnosing this. Running "make lint" in the
> test directory only feeds the tests to check-non-portable-shell.pl,
> but not the *lib*.sh helper scripts, which made me miss this one.
>
> The following diff should fix it for you. Am I understanding you
> correctly that you are experiencing other failures too? I see no
> other incompatibilities when running ./check-non-portable-shell.pl
> on all the shell scripts in the repo.

The longer story is that I run the test suite once a week or so. Most often under Mac OS, sometimes cygwin or Linux. Whenever there is a breakage under Mac OS which I can not debug within some minutes, I run it under Linux to see if there is the same breakage.

The ./check-non-portable-shell.pl can sometimes give an indication
why some test fail.
You can run it from command line:
  ./check-non-portable-shell.pl *.sh
and it will find the "echo -n" which I reported.
On the longer run it could probably check all *.sh files,
not only the ones under t/
I do not have the time to test the snipped patch below, but I can check pu
when the next round of your patch is in and give you some more info.
Jens Lehmann· Jul 3, 2014, 21:14 UTC · re: Torsten Bögershausen · lore

Re: [PATCH 00/14] Add submodule test harness

Am 03.07.2014 07:56, schrieb Torsten Bögershausen:
Show 29 quoted lines
> On 07/02/2014 09:57 PM, Jens Lehmann wrote:
>> Am 02.07.2014 16:54, schrieb Torsten Bögershausen:
>>> (Not sure if this is the right thread)
>>> (I haven't checked if this is fixed in your latest version)
>> Yes, this is the right thread and no, it isn't fixed yet.
>>
>>> On what I have on pu 7a0da7902cbbc9a876b90c9, Tue Jul 1 14:51:53 2014 -0700
>>>
>>> Many submodule tests are broken.
>>> One problem is here:
>>>
>>> lib-submodule-update.sh:264: possible problem: echo -n is not portable (please use printf):                     echo -n >sub1 &&
>>> lib-submodule-update.sh:507: possible problem: echo -n is not portable (please use printf):                     echo -n >sub1 &&
>>>
>>> You can remove the "empty" "echo -n" to create an empty file:
>>>> sub1 &&
>> Thanks for spotting and diagnosing this. Running "make lint" in the
>> test directory only feeds the tests to check-non-portable-shell.pl,
>> but not the *lib*.sh helper scripts, which made me miss this one.
>>
>> The following diff should fix it for you. Am I understanding you
>> correctly that you are experiencing other failures too? I see no
>> other incompatibilities when running ./check-non-portable-shell.pl
>> on all the shell scripts in the repo.
> The longer story is that I run the test suite once a week or so.
> Most often under Mac OS, sometimes cygwin or Linux.
> Whenever there is a breakage under Mac OS which I can not
> debug within some minutes, I run it under Linux to see if there
> is the same breakage.
Thanks, that really helps a lot and is more than I could ask for.
Show 7 quoted lines
> The ./check-non-portable-shell.pl can sometimes give an indication
> why some test fail.
> You can run it from command line:
>  ./check-non-portable-shell.pl *.sh
> and it will find the "echo -n" which I reported.
> On the longer run it could probably check all *.sh files,
> not only the ones under t/

That is exactly what I thought when I tried to find out why I missed this. Will cook up a patch to at least check the test helpers too.

> I do not have the time to test the snipped patch below, but I can check pu
> when the next round of your patch is in and give you some more info.
That'd be great!

Junio, do you want me to resend 02/14 without the non-portable "echo -n" or could you just squash the following diff in?

-----8<----
Show changes to t/lib-submodule-update.sh +2 −3
diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
index 24c9fd7..3584755 100755
--- a/t/lib-submodule-update.sh
+++ b/t/lib-submodule-update.sh
@@ -304,7 +304,7 @@ test_submodule_switch () {
 		(
 			cd submodule_update &&
 			git branch -t add_sub1 origin/add_sub1 &&
-			echo -n >sub1 &&
+			>sub1 &&
 			test_must_fail $command add_sub1 &&
 			test_superproject_content origin/no_submodule &&
 			test_must_be_empty sub1
@@ -547,7 +547,7 @@ test_submodule_forced_switch () {
 		(
 			cd submodule_update &&
 			git branch -t add_sub1 origin/add_sub1 &&
-			echo -n >sub1 &&
+			>sub1 &&
 			$command add_sub1 &&
 			test_superproject_content origin/add_sub1 &&
 			test_dir_is_empty sub1
-- 2.0.1.458.gf680257.dirty
Junio C Hamano· Jul 7, 2014, 17:05 UTC · re: Jens Lehmann · lore

Re: [PATCH 00/14] Add submodule test harness

Jens Lehmann <Jens.Lehmann@web.de> writes:
> Junio, do you want me to resend 02/14 without the non-portable "echo -n"
> or could you just squash the following diff in?
Amended locally here already; thanks, both.
Show 25 quoted lines
>
> -----8<----
> diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
> index 24c9fd7..3584755 100755
> --- a/t/lib-submodule-update.sh
> +++ b/t/lib-submodule-update.sh
> @@ -304,7 +304,7 @@ test_submodule_switch () {
>  		(
>  			cd submodule_update &&
>  			git branch -t add_sub1 origin/add_sub1 &&
> -			echo -n >sub1 &&
> +			>sub1 &&
>  			test_must_fail $command add_sub1 &&
>  			test_superproject_content origin/no_submodule &&
>  			test_must_be_empty sub1
> @@ -547,7 +547,7 @@ test_submodule_forced_switch () {
>  		(
>  			cd submodule_update &&
>  			git branch -t add_sub1 origin/add_sub1 &&
> -			echo -n >sub1 &&
> +			>sub1 &&
>  			$command add_sub1 &&
>  			test_superproject_content origin/add_sub1 &&
>  			test_dir_is_empty sub1
> -- 2.0.1.458.gf680257.dirty
Torsten Bögershausen· Jul 7, 2014, 19:40 UTC · re: Junio C Hamano · lore

Re: [PATCH 00/14] Add submodule test harness

On 2014-07-07 19.05, Junio C Hamano wrote:
Show 6 quoted lines
> Jens Lehmann <Jens.Lehmann@web.de> writes:
> 
>> Junio, do you want me to resend 02/14 without the non-portable "echo -n"
>> or could you just squash the following diff in?
> 
> Amended locally here already; thanks, both.

There seems to be some other trouble under Mac OS, not yet fully tracked down, (may be related to the "diff -r")

And Msysgit complains error: fchmod on c:/xxxt/trash directory.t7613-merge-submodule/submodule_update_repo/.git/modules/sub1/config.lock failed: Function not implemented

Jens Lehmann· Jul 8, 2014, 19:34 UTC · re: Torsten Bögershausen · lore

Re: [PATCH 00/14] Add submodule test harness

Am 07.07.2014 21:40, schrieb Torsten Bögershausen:
Show 10 quoted lines
> On 2014-07-07 19.05, Junio C Hamano wrote:
>> Jens Lehmann <Jens.Lehmann@web.de> writes:
>>
>>> Junio, do you want me to resend 02/14 without the non-portable "echo -n"
>>> or could you just squash the following diff in?
>>
>> Amended locally here already; thanks, both.
> 
> There seems to be some other trouble under Mac OS, not yet fully tracked down,
> (may be related to the "diff -r")
Torsten sees failures of this kind under Mac OS:

diff -r .git/modules/sub1/config sub1/.git/config 6d5 < worktree = ../../../sub1 8a8

>     worktree = ../../../sub1

So the config contains the same content, but the worktree setting moved to a different line. This seems to be the result of setting core.worktree in the test_git_directory_is_unchanged function just before the "diff -r", but only under Mac OS.

> And Msysgit complains 
> error: fchmod on c:/xxxt/trash directory.t7613-merge-submodule/submodule_update_repo/.git/modules/sub1/config.lock failed: Function not implemented

I'm not sure what this is about, seems to happen during the "cp -R" of the repo under .git/modules into the submodule.

I'm currently investigating both issues (the next steps probably being to install msysgit and to do some Git hacking on a Mac in the family).

Ramsay Jones· Jul 8, 2014, 20:25 UTC · re: Jens Lehmann · lore

Re: [PATCH 00/14] Add submodule test harness

On 08/07/14 20:34, Jens Lehmann wrote:
Show 30 quoted lines
> Am 07.07.2014 21:40, schrieb Torsten Bögershausen:
>> On 2014-07-07 19.05, Junio C Hamano wrote:
>>> Jens Lehmann <Jens.Lehmann@web.de> writes:
>>>
>>>> Junio, do you want me to resend 02/14 without the non-portable "echo -n"
>>>> or could you just squash the following diff in?
>>>
>>> Amended locally here already; thanks, both.
>>
>> There seems to be some other trouble under Mac OS, not yet fully tracked down,
>> (may be related to the "diff -r")
> 
> Torsten sees failures of this kind under Mac OS:
> 
> diff -r .git/modules/sub1/config sub1/.git/config
> 6d5
> <     worktree = ../../../sub1
> 8a8
>>     worktree = ../../../sub1
> 
> So the config contains the same content, but the worktree setting moved
> to a different line. This seems to be the result of setting core.worktree
> in the test_git_directory_is_unchanged function just before the "diff -r",
> but only under Mac OS.
> 
>> And Msysgit complains 
>> error: fchmod on c:/xxxt/trash directory.t7613-merge-submodule/submodule_update_repo/.git/modules/sub1/config.lock failed: Function not implemented
> 
> I'm not sure what this is about, seems to happen during the "cp -R" of
> the repo under .git/modules into the submodule.

I haven't looked into this at all, but from the above message, and noting that fchmod() is not implemented in mingw (see compat/mingw.h line 91), and the following:

    $ git grep -n fchmod
    compat/mingw.h:91:static inline int fchmod(int fildes, mode_t mode)
    config.c:1639:          if (fchmod(fd, st.st_mode & 07777) < 0) {
    config.c:1640:                  error("fchmod on %s failed: %s",
    config.c:1818:  if (fchmod(out_fd, st.st_mode & 07777) < 0) {
    config.c:1819:          ret = error("fchmod on %s failed: %s",
    $ 
[I happen to be on the pu branch at the moment, so YMMV!]

Both calls to fchmod() above are on config lock files, one in git_config_set_multivar_in_file() and the other in git_config_rename_section_in_file().

ATB, Ramsay Jones

Ramsay Jones· Jul 8, 2014, 21:03 UTC · re: Ramsay Jones · lore

Re: [PATCH 00/14] Add submodule test harness

On 08/07/14 21:25, Ramsay Jones wrote:
Show 50 quoted lines
> On 08/07/14 20:34, Jens Lehmann wrote:
>> Am 07.07.2014 21:40, schrieb Torsten Bögershausen:
>>> On 2014-07-07 19.05, Junio C Hamano wrote:
>>>> Jens Lehmann <Jens.Lehmann@web.de> writes:
>>>>
>>>>> Junio, do you want me to resend 02/14 without the non-portable "echo -n"
>>>>> or could you just squash the following diff in?
>>>>
>>>> Amended locally here already; thanks, both.
>>>
>>> There seems to be some other trouble under Mac OS, not yet fully tracked down,
>>> (may be related to the "diff -r")
>>
>> Torsten sees failures of this kind under Mac OS:
>>
>> diff -r .git/modules/sub1/config sub1/.git/config
>> 6d5
>> <     worktree = ../../../sub1
>> 8a8
>>>     worktree = ../../../sub1
>>
>> So the config contains the same content, but the worktree setting moved
>> to a different line. This seems to be the result of setting core.worktree
>> in the test_git_directory_is_unchanged function just before the "diff -r",
>> but only under Mac OS.
>>
>>> And Msysgit complains 
>>> error: fchmod on c:/xxxt/trash directory.t7613-merge-submodule/submodule_update_repo/.git/modules/sub1/config.lock failed: Function not implemented
>>
>> I'm not sure what this is about, seems to happen during the "cp -R" of
>> the repo under .git/modules into the submodule.
> 
> I haven't looked into this at all, but from the above message, and
> noting that fchmod() is not implemented in mingw (see compat/mingw.h
> line 91), and the following:
> 
>     $ git grep -n fchmod
>     compat/mingw.h:91:static inline int fchmod(int fildes, mode_t mode)
>     config.c:1639:          if (fchmod(fd, st.st_mode & 07777) < 0) {
>     config.c:1640:                  error("fchmod on %s failed: %s",
>     config.c:1818:  if (fchmod(out_fd, st.st_mode & 07777) < 0) {
>     config.c:1819:          ret = error("fchmod on %s failed: %s",
>     $ 
> 
> [I happen to be on the pu branch at the moment, so YMMV!]
> 
> Both calls to fchmod() above are on config lock files, one
> in git_config_set_multivar_in_file() and the other in
> git_config_rename_section_in_file().
> 

See commit daa22c6f8 ("config: preserve config file permissions on edits", 06-05-2014).

ATB, Ramsay Jones

Torsten Bögershausen· Jul 9, 2014, 06:39 UTC · re: Ramsay Jones · lore

No fchmod() under msygit - Was: Re: [PATCH 00/14] Add submodule test harness

On 07/08/2014 10:25 PM, Ramsay Jones wrote:
Show 51 quoted lines
> On 08/07/14 20:34, Jens Lehmann wrote:
>> Am 07.07.2014 21:40, schrieb Torsten Bögershausen:
>>> On 2014-07-07 19.05, Junio C Hamano wrote:
>>>> Jens Lehmann <Jens.Lehmann@web.de> writes:
>>>>
>>>>> Junio, do you want me to resend 02/14 without the non-portable "echo -n"
>>>>> or could you just squash the following diff in?
>>>> Amended locally here already; thanks, both.
>>> There seems to be some other trouble under Mac OS, not yet fully tracked down,
>>> (may be related to the "diff -r")
>> Torsten sees failures of this kind under Mac OS:
>>
>> diff -r .git/modules/sub1/config sub1/.git/config
>> 6d5
>> <     worktree = ../../../sub1
>> 8a8
>>>      worktree = ../../../sub1
>> So the config contains the same content, but the worktree setting moved
>> to a different line. This seems to be the result of setting core.worktree
>> in the test_git_directory_is_unchanged function just before the "diff -r",
>> but only under Mac OS.
>>
>>> And Msysgit complains
>>> error: fchmod on c:/xxxt/trash directory.t7613-merge-submodule/submodule_update_repo/.git/modules/sub1/config.lock failed: Function not implemented
>> I'm not sure what this is about, seems to happen during the "cp -R" of
>> the repo under .git/modules into the submodule.
> I haven't looked into this at all, but from the above message, and
> noting that fchmod() is not implemented in mingw (see compat/mingw.h
> line 91), and the following:
>
>      $ git grep -n fchmodcommit daa22c6f8da466bd7a438f1bc27375fd737ffcf3
> Author: Eric Wong <normalperson@yhbt.net>
> Date:   Tue May 6 00:17:14 2014 +0000
>
>      config: preserve config file permissions on edits
>      
>
>      compat/mingw.h:91:static inline int fchmod(int fildes, mode_t mode)
>      config.c:1639:          if (fchmod(fd, st.st_mode & 07777) < 0) {
>      config.c:1640:                  error("fchmod on %s failed: %s",
>      config.c:1818:  if (fchmod(out_fd, st.st_mode & 07777) < 0) {
>      config.c:1819:          ret = error("fchmod on %s failed: %s",
>      $
>
> [I happen to be on the pu branch at the moment, so YMMV!]
>
> Both calls to fchmod() above are on config lock files, one
> in git_config_set_multivar_in_file() and the other in
> git_config_rename_section_in_file().
>
>
commit daa22c6f8da466bd7a438f1bc27375fd737ffcf3
Author: Eric Wong <normalperson@yhbt.net>
Date:   Tue May 6 00:17:14 2014 +0000
     config: preserve config file permissions on edits

(And why is it "& 07777" and not "& 0777") Can we avoid the fchmod() all together ?

Eric Wong· Jul 9, 2014, 20:00 UTC · re: Torsten Bögershausen · lore

Re: No fchmod() under msygit - Was: Re: [PATCH 00/14] Add submodule test harness

Torsten Bögershausen <tboegi@web.de> wrote:
> (And why is it  "& 07777" and not  "& 0777")

This is to preserve the uncommon sticky/sgid/suid bits. Probably not needed, but better to keep as much intact as possible.

> Can we avoid the fchmod()  all together ?
For single-user systems, sure.

For multi-user systems with git-imap-send users and passwords in $GIT_CONFIG, I suggest not.

Erik Faye-Lund· Jul 14, 2014, 11:31 UTC · re: Eric Wong · lore

Re: No fchmod() under msygit - Was: Re: [PATCH 00/14] Add submodule test harness

On Wed, Jul 9, 2014 at 10:00 PM, Eric Wong <normalperson@yhbt.net> wrote:
Show 12 quoted lines
> Torsten Bögershausen <tboegi@web.de> wrote:
>> (And why is it  "& 07777" and not  "& 0777")
>
> This is to preserve the uncommon sticky/sgid/suid bits.  Probably not
> needed, but better to keep as much intact as possible.
>
>> Can we avoid the fchmod()  all together ?
>
> For single-user systems, sure.
>
> For multi-user systems with git-imap-send users and passwords in
> $GIT_CONFIG, I suggest not.

You're saying this as if Windows is a single-user system. It's not, but it uses ACLs rather than POSIX permissions to manage file-system permissions. So far we've opted to ignore ACLs in Git for Windows, though.

Nico Williams· Jul 14, 2014, 13:55 UTC · re: Erik Faye-Lund · lore

Re: No fchmod() under msygit - Was: Re: [PATCH 00/14] Add submodule test harness

On Mon, Jul 14, 2014 at 6:31 AM, Erik Faye-Lund <kusmabite@gmail.com> wrote:
Show 8 quoted lines
> On Wed, Jul 9, 2014 at 10:00 PM, Eric Wong <normalperson@yhbt.net> wrote:
>> Torsten Bögershausen <tboegi@web.de> wrote:
>>
>
> You're saying this as if Windows is a single-user system. It's not,
> but it uses ACLs rather than POSIX permissions to manage file-system
> permissions. So far we've opted to ignore ACLs in Git for Windows,
> though.

The clever thing to do (that some versions of ZFS do nowadays) is to use the new mask to "edit" the ACL as follows:

 - leave any DENY ACEs as-is; for all others continue
 - remove (reset) from any ACEs for Everyone (and/or Authenticated
Users) any bits corresponding to zero'ed bits in the new mask's other
bits
 - remove from any other ACEs that are not for the owner (@OWNER in
NFSv4 speak) any bits corresponding to zero'ed bits in the new mask's
group bits
 - remove from the owner's ACEs any bits corresponding to zero'ed bits
in the new mask's owner bits, but with some exceptions, in particular
the owner must retain the right to edit the ACL
 - add (set) to the Everyone (and/or Authenticated Users) ACEs a set
of bits corresponding to the set bits in the new mask's other bits
 - add (set) either only to the ACE for the file's group
(alternatively, to all non-owner, non-Everyone/Authenticated Users
ACEs) a set of bits corresponding to the set bits in the new mask's
group bits
...

I.e., use the chmod mask to decrease/increase access without changing the "shape" of the ACL.

Determining a file's mode_t from an ACL is similar, though it must take DENY entries into account: make a set of users/groups referred to by any ACEs in the ACL, divide them into owner, other (Everyone and/or Authenticated Users), and group (all others), find the maximal access granted.

Still, git might like to know what ACLs to apply to files at checkout time. That would be a vast new feature, I think, and probably not worth it, particularly since that would require dealing with the different types of ACLs: NTFS/NFSv4/ZFS on the one hand, POSIX Draft on the other, plus AFS and who knows what else -- ETOOMUCH IMO.

Nico --

Nico Williams· Jul 14, 2014, 14:02 UTC · re: Nico Williams · lore

Re: No fchmod() under msygit - Was: Re: [PATCH 00/14] Add submodule test harness

Show 5 quoted lines
> Still, git might like to know what ACLs to apply to files at checkout
> time.  That would be a vast new feature, I think, and probably not
> worth it, particularly since that would require dealing with the
> different types of ACLs: NTFS/NFSv4/ZFS on the one hand, POSIX Draft
> on the other, plus AFS and who knows what else -- ETOOMUCH IMO.
To complete that thought...

Inheritable ACLs + chmod should suffice. The user should setup inheritable ACLs for the directory where a repo is to be cloned, then on checkout git should just apply mode changes in such a way as to leave ACL "shapes" unchanged, only adding or removing bits from non-DENY ACEs according to the saved file modes.

Inheritable ACEs should be left as-is, since git doesn't track directory permissions (right?), or if it did, then those should be edited just like normal ACEs.

Nico --

Karsten Blees· Jul 14, 2014, 19:30 UTC · re: Eric Wong · lore

Re: No fchmod() under msygit - Was: Re: [PATCH 00/14] Add submodule test harness

Am 09.07.2014 22:00, schrieb Eric Wong:
Show 17 quoted lines
> Torsten Bögershausen <tboegi@web.de> wrote:
>> (And why is it  "& 07777" and not  "& 0777")
> 
> This is to preserve the uncommon sticky/sgid/suid bits.  Probably not
> needed, but better to keep as much intact as possible.
> 
>> Can we avoid the fchmod()  all together ?
> 
> For single-user systems, sure.
> 
> For multi-user systems with git-imap-send users and passwords in
> $GIT_CONFIG, I suggest not.
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

The Windows fchmod() problem is easily solved by using chmod() instead of fchmod(). The file name is known in both cases (and even used in the error message).

However, IMO there are more fundamental problems with your patch (not related to Windows):

1.) Permissions of files in .git are controlled by the core.sharedRepository setting, and your patch seems to break that (i.e. if someone accidentally has made .git/config world readable, git-config no longer fixes that, even if core.sharedRepository=0600).

2.) Sensitive data such as passwords doesn't belong in config files in the first place, that's what git-credentials is good for.

Junio C Hamano· Jul 14, 2014, 21:18 UTC · re: Karsten Blees · lore

Re: No fchmod() under msygit - Was: Re: [PATCH 00/14] Add submodule test harness

Karsten Blees <karsten.blees@gmail.com> writes:
> 1.) Permissions of files in .git are controlled by the core.sharedRepository
> setting, and your patch seems to break that (i.e. if someone accidentally
> has made .git/config world readable, git-config no longer fixes that, even
> if core.sharedRepository=0600).

For any repository, when you further run "git config" to update it, .git/config always exists with the permission bits initialized when "git init" was run there. And Eric's change is to propagate that same bits to a new file, so there wouldn't be any difference, no?

And $HOME/.gitconfig should not be governed by "sharedRepository", and that was the primary focus of Eric's change, I would think.

Torsten Bögershausen· Jul 9, 2014, 06:14 UTC · re: Jens Lehmann · lore

Re: [PATCH 00/14] Add submodule test harness

Show 14 quoted lines
>> There seems to be some other trouble under Mac OS, not yet fully tracked down,
>> (may be related to the "diff -r")
> Torsten sees failures of this kind under Mac OS:
>
> diff -r .git/modules/sub1/config sub1/.git/config
> 6d5
> <     worktree = ../../../sub1
> 8a8
>>      worktree = ../../../sub1
> So the config contains the same content, but the worktree setting moved
> to a different line. This seems to be the result of setting core.worktree
> in the test_git_directory_is_unchanged function just before the "diff -r",
> but only under Mac OS.
>

So I was suspecting diff -r beinng non-portable, but that doesn't seem to be the problem here. (But I wouldn't be surprised if there where problems with diff -r on some Unix systems) Anyway, checking all the files in the working tree seems to be a good thing to do, but that does not necessarily work for .git/config. A "brute force" approach could be to simply run the config file(s) through sort and compare them:

sort <.git/modules/sub1/config >expect && sort <sub1/.git/config >actual && test_cmp expect actual && rm expect actual && cp git/modules/sub1/config sub1/.git/config

[end of scriptlet]
And here the "dumps" of the 2 config files:
.......
Branch remove_sub1 set up to track remote branch remove_sub1 from origin.
warning: unable to rmdir sub1: Directory not empty
Updating 68c8810..81b9f6a
Fast-forward
  .gitmodules | 4 ----
  1 file changed, 4 deletions(-)
  delete mode 100644 .gitmodules
---------------------
[core]
         repositoryformatversion = 0
         filemode = true
         bare = false
         logallrefupdates = true
         worktree = ../../../sub1
         ignorecase = true
         precomposeunicode = true
[remote "origin"]
         url = /Users/tb/projects/git/tb.140704_JensLehman/t/trash 
directory.t7613-merge-submodule/submodule_update_repo/.
         fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
         remote = origin
         merge = refs/heads/master
----------------
[core]
         repositoryformatversion = 0
         filemode = true
         bare = false
         logallrefupdates = true
         ignorecase = true
         precomposeunicode = true
         worktree = ../../../sub1
[remote "origin"]
         url = /Users/tb/projects/git/tb.140704_JensLehman/t/trash 
directory.t7613-merge-submodule/submodule_update_repo/.
         fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
         remote = origin
         merge = refs/heads/master
=====================
diff -r .git/modules/sub1/config sub1/.git/config
6d5
<       worktree = ../../../sub1
8a8
 >       worktree = ../../../sub1
not ok 7 - git merge: removed submodule leaves submodule containing a 
.git directory alone
Junio C Hamano· Jul 9, 2014, 15:20 UTC · re: Torsten Bögershausen · lore

Re: [PATCH 00/14] Add submodule test harness

Torsten Bögershausen <tboegi@web.de> writes:
Show 6 quoted lines
> A "brute force" approach could be to simply run the config file(s)
> through sort and compare them:
>
> sort <.git/modules/sub1/config >expect &&
> sort <sub1/.git/config >actual &&
> test_cmp expect actual &&
Or "git config --list" from these files, perhaps?
Jens Lehmann· Jul 9, 2014, 18:19 UTC · re: Torsten Bögershausen · lore

Re: [PATCH 00/14] Add submodule test harness

Am 09.07.2014 08:14, schrieb Torsten Bögershausen:
Show 19 quoted lines
> 
>>> There seems to be some other trouble under Mac OS, not yet fully tracked down,
>>> (may be related to the "diff -r")
>> Torsten sees failures of this kind under Mac OS:
>>
>> diff -r .git/modules/sub1/config sub1/.git/config
>> 6d5
>> <     worktree = ../../../sub1
>> 8a8
>>>      worktree = ../../../sub1
>> So the config contains the same content, but the worktree setting moved
>> to a different line. This seems to be the result of setting core.worktree
>> in the test_git_directory_is_unchanged function just before the "diff -r",
>> but only under Mac OS.
>>
> So I was suspecting diff -r beinng non-portable, but that doesn't seem to be the problem here.
> (But I wouldn't be surprised if there where problems with diff -r on some Unix systems)
> Anyway, checking all the files in the working tree seems to be a good thing to do,
> but that does not necessarily work for .git/config.

I agree, but this case is special. The test asserts that nobody added, modified or removed *anything* inside the .git directory. The reason for problem we are seeing here is that I have to remove the core.worktree setting when moving the git directory from .git/modules into the submodule work tree. So the test adds it again to be able to diff it, and this happens in a different line only on Mac OS as comparing the two core sections shows:

Show 10 quoted lines
> ---------------------
> [core]
>         repositoryformatversion = 0
>         filemode = true
>         bare = false
>         logallrefupdates = true
>         worktree = ../../../sub1
>         ignorecase = true
>         precomposeunicode = true
> [remote "origin"]
vs.
Show 10 quoted lines
> ----------------
> [core]
>         repositoryformatversion = 0
>         filemode = true
>         bare = false
>         logallrefupdates = true
>         ignorecase = true
>         precomposeunicode = true
>         worktree = ../../../sub1
> [remote "origin"]

And now it's clear what happens here: On Mac OS the ignorecase and precomposeunicode settings are added behind the worktree line, then re-adding worktree later for the comparison adds it after these two.

Could you please test the following? It should avoid this kind of problem by removing the core.worktree setting temporarily from the original config in .git/modules instead of adding it temporarily to .git/config: -----------------------8<-----------------------

Show changes to t/lib-submodule-update.sh +5 −5
diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
index 3584755..98c86e3 100755
--- a/t/lib-submodule-update.sh
+++ b/t/lib-submodule-update.sh
@@ -143,18 +143,18 @@ replace_gitfile_with_git_dir () {
 }

 # Test that the .git directory in the submodule is unchanged (except for the
-# core.worktree setting, which we temporarily restore). Call this function
+# core.worktree setting, which we temporarily remove). Call this function
 # before test_submodule_content as the latter might write the index file
 # leading to false positive index differences.
 test_git_directory_is_unchanged () {
 	(
-		cd "$1" &&
-		git config core.worktree "../../../$1"
+		cd ".git/modules/$1" &&
+		git config --unset core.worktree
 	) &&
 	diff -r ".git/modules/$1" "$1/.git" &&
 	(
-		cd "$1" &&
-		GIT_WORK_TREE=. git config --unset core.worktree
+		cd ".git/modules/$1" &&
+		git config core.worktree "../../../$1"
 	)
 }
Junio C Hamano· Jul 9, 2014, 19:31 UTC · re: Jens Lehmann · lore

Re: [PATCH 00/14] Add submodule test harness

Jens Lehmann <Jens.Lehmann@web.de> writes:
Show 5 quoted lines
> I agree, but this case is special. The test asserts that nobody
> added, modified or removed *anything* inside the .git directory.
> The reason for problem we are seeing here is that I have to
> remove the core.worktree setting when moving the git directory
> from .git/modules into the submodule work tree.

Hmph. Comparing the files with core.worktree removed sounds like a workaround that knows too much about the implementation detail of what is being tested. I am just wondering if core.worktree will stay forever be the only thing that is special, or there may come other things (perhaps as a fallout of integrating things like Duy's multiple-worktree stuff).

But perhaps we cannot do better than this.
Show 32 quoted lines
> Could you please test the following? It should avoid this kind
> of problem by removing the core.worktree setting temporarily
> from the original config in .git/modules instead of adding it
> temporarily to .git/config:
> -----------------------8<-----------------------
> diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
> index 3584755..98c86e3 100755
> --- a/t/lib-submodule-update.sh
> +++ b/t/lib-submodule-update.sh
> @@ -143,18 +143,18 @@ replace_gitfile_with_git_dir () {
>  }
>
>  # Test that the .git directory in the submodule is unchanged (except for the
> -# core.worktree setting, which we temporarily restore). Call this function
> +# core.worktree setting, which we temporarily remove). Call this function
>  # before test_submodule_content as the latter might write the index file
>  # leading to false positive index differences.
>  test_git_directory_is_unchanged () {
>  	(
> -		cd "$1" &&
> -		git config core.worktree "../../../$1"
> +		cd ".git/modules/$1" &&
> +		git config --unset core.worktree
>  	) &&
>  	diff -r ".git/modules/$1" "$1/.git" &&
>  	(
> -		cd "$1" &&
> -		GIT_WORK_TREE=. git config --unset core.worktree
> +		cd ".git/modules/$1" &&
> +		git config core.worktree "../../../$1"
>  	)
>  }
Junio C Hamano· Jul 10, 2014, 20:52 UTC · re: Junio C Hamano · lore

Re: [PATCH 00/14] Add submodule test harness

Junio C Hamano <gitster@pobox.com> writes:
Show 16 quoted lines
> Jens Lehmann <Jens.Lehmann@web.de> writes:
>
>> I agree, but this case is special. The test asserts that nobody
>> added, modified or removed *anything* inside the .git directory.
>> The reason for problem we are seeing here is that I have to
>> remove the core.worktree setting when moving the git directory
>> from .git/modules into the submodule work tree.
>
> Hmph.  Comparing the files with core.worktree removed sounds like a
> workaround that knows too much about the implementation detail of
> what is being tested.  I am just wondering if core.worktree will
> stay forever be the only thing that is special, or there may come
> other things (perhaps as a fallout of integrating things like Duy's
> multiple-worktree stuff).
>
> But perhaps we cannot do better than this.

One thing we should be able to do (and must do) better is to validate that core.worktree in the relocated config file actually points at the right place. Unsetting before comparing may let us compare the relocated one in .git/modules/$1/config with the one that is embedded in the working tree (hence no .git/config), but the way your "how about this?" patch does, we wouldn't catch a possible breakage to the relocation code to point core.worktree to a bogus location, I'm afraid.

Perhaps squashing this to 7e8e5af9 instead?
 t/lib-submodule-update.sh | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
Show changes to t/lib-submodule-update.sh +12 −7
diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
index e441b98..fc1da84 100755
--- a/t/lib-submodule-update.sh
+++ b/t/lib-submodule-update.sh
@@ -110,18 +110,23 @@ replace_gitfile_with_git_dir () {
 }
 
 # Test that the .git directory in the submodule is unchanged (except for the
-# core.worktree setting, which we temporarily restore). Call this function
-# before test_submodule_content as the latter might write the index file
-# leading to false positive index differences.
+# core.worktree setting, which appears only in $GIT_DIR/modules/$1/config).
+# Call this function before test_submodule_content as the latter might
+# write the index file leading to false positive index differences.
 test_git_directory_is_unchanged () {
 	(
-		cd "$1" &&
-		git config core.worktree "../../../$1"
+		cd ".git/modules/$1" &&
+		# does core.worktree point at the right place?
+		test "$(git config core.worktree)" = "../../../$1" &&
+		# remove it temporarily before comparing, as
+		# "$1/.git/config" lacks it...
+		git config --unset core.worktree
 	) &&
 	diff -r ".git/modules/$1" "$1/.git" &&
 	(
-		cd "$1" &&
-		GIT_WORK_TREE=. git config --unset core.worktree
+		# ... and then restore.
+		cd ".git/modules/$1" &&
+		git config core.worktree "../../../$1"
 	)
 }
 
Jens Lehmann· Jul 12, 2014, 18:23 UTC · re: Junio C Hamano · lore

Re: [PATCH 00/14] Add submodule test harness

Am 10.07.2014 22:52, schrieb Junio C Hamano:
Show 27 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> 
>> Jens Lehmann <Jens.Lehmann@web.de> writes:
>>
>>> I agree, but this case is special. The test asserts that nobody
>>> added, modified or removed *anything* inside the .git directory.
>>> The reason for problem we are seeing here is that I have to
>>> remove the core.worktree setting when moving the git directory
>>> from .git/modules into the submodule work tree.
>>
>> Hmph.  Comparing the files with core.worktree removed sounds like a
>> workaround that knows too much about the implementation detail of
>> what is being tested.  I am just wondering if core.worktree will
>> stay forever be the only thing that is special, or there may come
>> other things (perhaps as a fallout of integrating things like Duy's
>> multiple-worktree stuff).
>>
>> But perhaps we cannot do better than this.
> 
> One thing we should be able to do (and must do) better is to
> validate that core.worktree in the relocated config file actually
> points at the right place.  Unsetting before comparing may let us
> compare the relocated one in .git/modules/$1/config with the one
> that is embedded in the working tree (hence no .git/config), but the
> way your "how about this?" patch does, we wouldn't catch a possible
> breakage to the relocation code to point core.worktree to a bogus
> location, I'm afraid.
Indeed.
> Perhaps squashing this to 7e8e5af9 instead?
Yes please, this is much better than my first attempt.
Show 39 quoted lines
>  t/lib-submodule-update.sh | 19 ++++++++++++-------
>  1 file changed, 12 insertions(+), 7 deletions(-)
> 
> diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
> index e441b98..fc1da84 100755
> --- a/t/lib-submodule-update.sh
> +++ b/t/lib-submodule-update.sh
> @@ -110,18 +110,23 @@ replace_gitfile_with_git_dir () {
>  }
>  
>  # Test that the .git directory in the submodule is unchanged (except for the
> -# core.worktree setting, which we temporarily restore). Call this function
> -# before test_submodule_content as the latter might write the index file
> -# leading to false positive index differences.
> +# core.worktree setting, which appears only in $GIT_DIR/modules/$1/config).
> +# Call this function before test_submodule_content as the latter might
> +# write the index file leading to false positive index differences.
>  test_git_directory_is_unchanged () {
>  	(
> -		cd "$1" &&
> -		git config core.worktree "../../../$1"
> +		cd ".git/modules/$1" &&
> +		# does core.worktree point at the right place?
> +		test "$(git config core.worktree)" = "../../../$1" &&
> +		# remove it temporarily before comparing, as
> +		# "$1/.git/config" lacks it...
> +		git config --unset core.worktree
>  	) &&
>  	diff -r ".git/modules/$1" "$1/.git" &&
>  	(
> -		cd "$1" &&
> -		GIT_WORK_TREE=. git config --unset core.worktree
> +		# ... and then restore.
> +		cd ".git/modules/$1" &&
> +		git config core.worktree "../../../$1"
>  	)
>  }
>  
> 
Junio C Hamano· Jul 14, 2014, 01:01 UTC · re: Jens Lehmann · lore

Re: [PATCH 00/14] Add submodule test harness

Jens Lehmann <Jens.Lehmann@web.de> writes:
>> Perhaps squashing this to 7e8e5af9 instead?
>
> Yes please, this is much better than my first attempt.

One thing that I found troubling is the ../../../ "three levels up" is hardcoded. Would it be always true for any value of "$1"? If the submodule is bound to the superproject at sub/dir/, not at dir/, for example, would it have to change?

I am not saying that we must support artibrary cases, but if there is such a limitation in the implementation, people who will use the helper in their new tests want it at least documented, I think.

Show 39 quoted lines
>>  t/lib-submodule-update.sh | 19 ++++++++++++-------
>>  1 file changed, 12 insertions(+), 7 deletions(-)
>> 
>> diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
>> index e441b98..fc1da84 100755
>> --- a/t/lib-submodule-update.sh
>> +++ b/t/lib-submodule-update.sh
>> @@ -110,18 +110,23 @@ replace_gitfile_with_git_dir () {
>>  }
>>  
>>  # Test that the .git directory in the submodule is unchanged (except for the
>> -# core.worktree setting, which we temporarily restore). Call this function
>> -# before test_submodule_content as the latter might write the index file
>> -# leading to false positive index differences.
>> +# core.worktree setting, which appears only in $GIT_DIR/modules/$1/config).
>> +# Call this function before test_submodule_content as the latter might
>> +# write the index file leading to false positive index differences.
>>  test_git_directory_is_unchanged () {
>>  	(
>> -		cd "$1" &&
>> -		git config core.worktree "../../../$1"
>> +		cd ".git/modules/$1" &&
>> +		# does core.worktree point at the right place?
>> +		test "$(git config core.worktree)" = "../../../$1" &&
>> +		# remove it temporarily before comparing, as
>> +		# "$1/.git/config" lacks it...
>> +		git config --unset core.worktree
>>  	) &&
>>  	diff -r ".git/modules/$1" "$1/.git" &&
>>  	(
>> -		cd "$1" &&
>> -		GIT_WORK_TREE=. git config --unset core.worktree
>> +		# ... and then restore.
>> +		cd ".git/modules/$1" &&
>> +		git config core.worktree "../../../$1"
>>  	)
>>  }
>>  
>> 
Jens Lehmann· Jul 14, 2014, 18:22 UTC · re: Junio C Hamano · lore

Re: [PATCH 00/14] Add submodule test harness

Am 14.07.2014 03:01, schrieb Junio C Hamano:
Show 10 quoted lines
> Jens Lehmann <Jens.Lehmann@web.de> writes:
> 
>>> Perhaps squashing this to 7e8e5af9 instead?
>>
>> Yes please, this is much better than my first attempt.
> 
> One thing that I found troubling is the ../../../ "three levels up"
> is hardcoded.  Would it be always true for any value of "$1"?  If
> the submodule is bound to the superproject at sub/dir/, not at dir/,
> for example, would it have to change?

Yes. And the code currently also doesn't handle submodules whose name and path differ.

> I am not saying that we must support artibrary cases, but if there
> is such a limitation in the implementation, people who will use the
> helper in their new tests want it at least documented, I think.

Ah, I didn't think about other tests reusing that and only wrote this as a local helper. But you're right, it would make sense to reuse this function instead of coding that again (even though I'd prefer to extract the generic helpers to t/lib-submodule.sh for that purpose).

So what about adding "Currently only submodules living in the root directory of the superproject with the default name (same as the path) are supported." to the comment above the function?

Show 40 quoted lines
>>>  t/lib-submodule-update.sh | 19 ++++++++++++-------
>>>  1 file changed, 12 insertions(+), 7 deletions(-)
>>>
>>> diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
>>> index e441b98..fc1da84 100755
>>> --- a/t/lib-submodule-update.sh
>>> +++ b/t/lib-submodule-update.sh
>>> @@ -110,18 +110,23 @@ replace_gitfile_with_git_dir () {
>>>  }
>>>  
>>>  # Test that the .git directory in the submodule is unchanged (except for the
>>> -# core.worktree setting, which we temporarily restore). Call this function
>>> -# before test_submodule_content as the latter might write the index file
>>> -# leading to false positive index differences.
>>> +# core.worktree setting, which appears only in $GIT_DIR/modules/$1/config).
>>> +# Call this function before test_submodule_content as the latter might
>>> +# write the index file leading to false positive index differences.
>>>  test_git_directory_is_unchanged () {
>>>  	(
>>> -		cd "$1" &&
>>> -		git config core.worktree "../../../$1"
>>> +		cd ".git/modules/$1" &&
>>> +		# does core.worktree point at the right place?
>>> +		test "$(git config core.worktree)" = "../../../$1" &&
>>> +		# remove it temporarily before comparing, as
>>> +		# "$1/.git/config" lacks it...
>>> +		git config --unset core.worktree
>>>  	) &&
>>>  	diff -r ".git/modules/$1" "$1/.git" &&
>>>  	(
>>> -		cd "$1" &&
>>> -		GIT_WORK_TREE=. git config --unset core.worktree
>>> +		# ... and then restore.
>>> +		cd ".git/modules/$1" &&
>>> +		git config core.worktree "../../../$1"
>>>  	)
>>>  }
>>>  
>>>
> 
Junio C Hamano· Jul 14, 2014, 21:18 UTC · re: Jens Lehmann · lore

Re: [PATCH 00/14] Add submodule test harness

Jens Lehmann <Jens.Lehmann@web.de> writes:
> So what about adding "Currently only submodules living in the
> root directory of the superproject with the default name (same
> as the path) are supported." to the comment above the function?
OK, done, and merged to 'next'.
Thanks.
Johannes Sixt· Jul 9, 2014, 17:21 UTC · re: Jens Lehmann · lore

Re: [PATCH 00/14] Add submodule test harness

Am 08.07.2014 21:34, schrieb Jens Lehmann:
Show 5 quoted lines
>> And Msysgit complains 
>> error: fchmod on c:/xxxt/trash directory.t7613-merge-submodule/submodule_update_repo/.git/modules/sub1/config.lock failed: Function not implemented
> 
> I'm not sure what this is about, seems to happen during the "cp -R" of
> the repo under .git/modules into the submodule.
No. It happens because fchmod() is not implemented in our Windows port.

Please see my band-aid patch at http://thread.gmane.org/gmane.comp.version-control.git/248154/focus=20266 The sub-thread ended inconclusive.

-- Hannes
Junio C Hamano· Jul 9, 2014, 19:22 UTC · re: Johannes Sixt · lore

Re: [PATCH 00/14] Add submodule test harness

Johannes Sixt <j6t@kdbg.org> writes:
Show 12 quoted lines
> Am 08.07.2014 21:34, schrieb Jens Lehmann:
>>> And Msysgit complains 
>>> error: fchmod on c:/xxxt/trash directory.t7613-merge-submodule/submodule_update_repo/.git/modules/sub1/config.lock failed: Function not implemented
>> 
>> I'm not sure what this is about, seems to happen during the "cp -R" of
>> the repo under .git/modules into the submodule.
>
> No. It happens because fchmod() is not implemented in our Windows port.
>
> Please see my band-aid patch at
> http://thread.gmane.org/gmane.comp.version-control.git/248154/focus=20266
> The sub-thread ended inconclusive.

We need to start somewhere, and a no-op fchmod() in your patch may be as a good place to start as anything. At least we would then keep the old behaviour without introducing any new failure.

An alternative might be to use chmod() after we are done writing to the config.lock in order to avoid the use of fchmod() altogether, which I think can replace the existing two callsites of fchmod(). That approach might be a more expedient, but may turn out to be undesirable in the longer term.

I also wonder if this "carry forward the original permission bits when updating an existing file by first writing the updated contents into a lockfile and then renaming it after we are done" pattern ought to be done in the lockfile API at commit_lock_file() time (Duy and Michael Cc'ed for their input, as they have recently touched lockfile API implementation in their series somewhat), not at the level of the user of the lockfile API like Eric's patch daa22c6f (config: preserve config file permissions on edits, 2014-05-06) did only for the config file.

Eric Wong· Jul 9, 2014, 19:56 UTC · re: Junio C Hamano · lore

Re: [PATCH 00/14] Add submodule test harness

Junio C Hamano <gitster@pobox.com> wrote:
Show 17 quoted lines
> Johannes Sixt <j6t@kdbg.org> writes:
> > Am 08.07.2014 21:34, schrieb Jens Lehmann:
> >>> And Msysgit complains 
> >>> error: fchmod on c:/xxxt/trash directory.t7613-merge-submodule/submodule_update_repo/.git/modules/sub1/config.lock failed: Function not implemented
> >> 
> >> I'm not sure what this is about, seems to happen during the "cp -R" of
> >> the repo under .git/modules into the submodule.
> >
> > No. It happens because fchmod() is not implemented in our Windows port.
> >
> > Please see my band-aid patch at
> > http://thread.gmane.org/gmane.comp.version-control.git/248154/focus=20266
> > The sub-thread ended inconclusive.
> 
> We need to start somewhere, and a no-op fchmod() in your patch may
> be as a good place to start as anything.  At least we would then
> keep the old behaviour without introducing any new failure.

Right, this likely makes the most sense for single-user systems or systesm without a *nix-like permission system.

Show 5 quoted lines
> An alternative might be to use chmod() after we are done writing to
> the config.lock in order to avoid the use of fchmod() altogether,
> which I think can replace the existing two callsites of fchmod().
> That approach might be a more expedient, but may turn out to be
> undesirable in the longer term.

In that case, we would need to open with mode=0600 to avoid a window where the file may be world-readable with any data in it.

Junio C Hamano· Jul 9, 2014, 21:57 UTC · re: Eric Wong · lore

Re: [PATCH 00/14] Add submodule test harness

Eric Wong <normalperson@yhbt.net> writes:
Show 30 quoted lines
> Junio C Hamano <gitster@pobox.com> wrote:
>> Johannes Sixt <j6t@kdbg.org> writes:
>> > Am 08.07.2014 21:34, schrieb Jens Lehmann:
>> >>> And Msysgit complains 
>> >>> error: fchmod on c:/xxxt/trash directory.t7613-merge-submodule/submodule_update_repo/.git/modules/sub1/config.lock failed: Function not implemented
>> >> 
>> >> I'm not sure what this is about, seems to happen during the "cp -R" of
>> >> the repo under .git/modules into the submodule.
>> >
>> > No. It happens because fchmod() is not implemented in our Windows port.
>> >
>> > Please see my band-aid patch at
>> > http://thread.gmane.org/gmane.comp.version-control.git/248154/focus=20266
>> > The sub-thread ended inconclusive.
>> 
>> We need to start somewhere, and a no-op fchmod() in your patch may
>> be as a good place to start as anything.  At least we would then
>> keep the old behaviour without introducing any new failure.
>
> Right, this likely makes the most sense for single-user systems or
> systesm without a *nix-like permission system.
>
>> An alternative might be to use chmod() after we are done writing to
>> the config.lock in order to avoid the use of fchmod() altogether,
>> which I think can replace the existing two callsites of fchmod().
>> That approach might be a more expedient, but may turn out to be
>> undesirable in the longer term.
>
> In that case, we would need to open with mode=0600 to avoid a window
> where the file may be world-readable with any data in it.
Yes, of course.

To elaborate what I was alluding to at the end of the message you are responding to a bit more, if we were to move this "grab perms from existing file (if there is any) and propagate to the new one" into the lockfile API,

 - in hold_lock_file_for_update(), we would record the permission of
   the original file, if any, to a new field in "struct lock_file";
 - open with 0600 or tighter in lock_file(), and
 - either before closing the file use fchmod() or after closing and
   moving the file use chmod() to propagate the permission.

If the original did not exist, we would pass 0666 to open as before in lock_file() and do not bother chmod/fchmod at the end.

Or something like that, perhaps.
Torsten Bögershausen· Jul 10, 2014, 06:22 UTC · re: Junio C Hamano · lore

No fchmd. was: Re: [PATCH 00/14] Add submodule test harness

On 07/09/2014 11:57 PM, Junio C Hamano wrote:
Show 45 quoted lines
> Eric Wong <normalperson@yhbt.net> writes:
>
>> Junio C Hamano <gitster@pobox.com> wrote:
>>> Johannes Sixt <j6t@kdbg.org> writes:
>>>> Am 08.07.2014 21:34, schrieb Jens Lehmann:
>>>>>> And Msysgit complains
>>>>>> error: fchmod on c:/xxxt/trash directory.t7613-merge-submodule/submodule_update_repo/.git/modules/sub1/config.lock failed: Function not implemented
>>>>> I'm not sure what this is about, seems to happen during the "cp -R" of
>>>>> the repo under .git/modules into the submodule.
>>>> No. It happens because fchmod() is not implemented in our Windows port.
>>>>
>>>> Please see my band-aid patch at
>>>> http://thread.gmane.org/gmane.comp.version-control.git/248154/focus=20266
>>>> The sub-thread ended inconclusive.
>>> We need to start somewhere, and a no-op fchmod() in your patch may
>>> be as a good place to start as anything.  At least we would then
>>> keep the old behaviour without introducing any new failure.
>> Right, this likely makes the most sense for single-user systems or
>> systesm without a *nix-like permission system.
>>
>>> An alternative might be to use chmod() after we are done writing to
>>> the config.lock in order to avoid the use of fchmod() altogether,
>>> which I think can replace the existing two callsites of fchmod().
>>> That approach might be a more expedient, but may turn out to be
>>> undesirable in the longer term.
>> In that case, we would need to open with mode=0600 to avoid a window
>> where the file may be world-readable with any data in it.
> Yes, of course.
>
> To elaborate what I was alluding to at the end of the message you
> are responding to a bit more, if we were to move this "grab perms
> from existing file (if there is any) and propagate to the new one"
> into the lockfile API,
>
>   - in hold_lock_file_for_update(), we would record the permission of
>     the original file, if any, to a new field in "struct lock_file";
>   - open with 0600 or tighter in lock_file(), and
>
>   - either before closing the file use fchmod() or after closing and
>     moving the file use chmod() to propagate the permission.
>
> If the original did not exist, we would pass 0666 to open as before
> in lock_file() and do not bother chmod/fchmod at the end.
>
> Or something like that, perhaps.

Isn't the whole problem starting here: in config.c:

     fd = hold_lock_file_for_update(lock, config_filename, 0);
In lockfile.c:
   /* This should return a meaningful errno on failure */
   int hold_lock_file_for_update(struct lock_file *lk, const char *path, 
int flags)
   {
       int fd = lock_file(lk, path, flags);
which leads to
   static int lock_file(struct lock_file *lk, const char *path, int flags)
     []
     lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);

There is no way to tell which permissions the new lockfile should have. That is somewhat unlucky.

On the other hand, shouldn't we call adjust_shared_perm(const char *path) from path.c on the config file?

And to all files which are fiddled through the lock_file API? In other words, the lockfile could be created with the restrictive permissions 600, and once the lockfile had been closed and renamed into the final name we apply adjust_shared_perm() on it ?

Or probably directly after close() ?
I think there are 2 different things missing here:
- Be able to specify permissions to hold_lock_file_for_update(),
    especially restrictive ones, like 600 and not 666.
- Adjust the permissions for "shared files" in a shared repo.
   This is probably needed for a shared repo, when the user itself
    has a umask which is too restrictive and adjust_shared_perm()
    must be run to widen the permissions.
Do I miss something ?
Junio C Hamano· Jul 10, 2014, 19:49 UTC · re: Torsten Bögershausen · lore

Re: No fchmd. was: Re: [PATCH 00/14] Add submodule test harness

Torsten Bögershausen <tboegi@web.de> writes:
Show 16 quoted lines
> Isn't the whole problem starting here:
> in config.c:
>
>     fd = hold_lock_file_for_update(lock, config_filename, 0);
> In lockfile.c:
>   /* This should return a meaningful errno on failure */
>   int hold_lock_file_for_update(struct lock_file *lk, const char
> *path, int flags)
>   {
>       int fd = lock_file(lk, path, flags);
> which leads to
>   static int lock_file(struct lock_file *lk, const char *path, int flags)
>     []
>     lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
>
> There is no way to tell which permissions the new lockfile should have.
We follow whategver user's umask says with this code.
> That is somewhat unlucky.
>
> On the other hand, shouldn't we call
> adjust_shared_perm(const char *path) from path.c on the config file?

Good question, but I am not sure. For $GIT_DIR/config, I tend to agree we should, but "git config --global foo bar" would not be a shared file anyway, and my understanding of Eric's original motivation is to keep $HOME/.gitconfig to be tighter than the user's umask normally would indicate.

Show 5 quoted lines
> And to all files which are fiddled through the lock_file API?
> In other words, the lockfile could be created with the restrictive
> permissions
> 600, and once the lockfile had been closed and renamed into the final name
> we apply adjust_shared_perm() on it ?

For all files that adjust-shared-perm should apply, yes, but I do not think it is relevant to the codepath in question.

> I think there are 2 different things missing here:
>
> - Be able to specify permissions to hold_lock_file_for_update(),
>    especially restrictive ones, like 600 and not 666.

Yes (in the sense that "yes we can add an extra parameter") and no (in the sense that "where would we get the value to pass to the extra parameter from? would it be worth to add configurations variables for different kinds of files?").

If we limit the case to "Inherit permissions from the file we are replacing by taking a lock on it", which is the topic of discussion in this thread, we do not have to worry about how to configure the value (we do not have to) and adding a new parameter to tell the mode to hold-lock-file-for-update is unneeded (the function will have a pathname of the original and can learn the current permission bits itself).

> - Adjust the permissions for "shared files" in a shared repo.
>   This is probably needed for a shared repo, when the user itself
>    has a umask which is too restrictive and adjust_shared_perm()
>    must be run to widen the permissions.

Don't we already do that for $GIT_DIR/config? In any case that will not help $HOME/.gitconfig and other files that are not shared.

Torsten Bögershausen· Jul 10, 2014, 20:55 UTC · re: Junio C Hamano · lore

Re: No fchmd. was: Re: [PATCH 00/14] Add submodule test harness

On 2014-07-10 21.49, Junio C Hamano wrote: []

Show 7 quoted lines
> If we limit the case to "Inherit permissions from the file we are
> replacing by taking a lock on it", which is the topic of discussion
> in this thread, we do not have to worry about how to configure the
> value (we do not have to) and adding a new parameter to tell the
> mode to hold-lock-file-for-update is unneeded (the function will
> have a pathname of the original and can learn the current permission
> bits itself).

So something like this: (I will probably not have the time to make a proper patch :-(

Show changes to lockfile.c +6 −1
diff --git a/lockfile.c b/lockfile.c
index 4899270..134d5c8 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -156,6 +156,11 @@ static void resolve_symlink(struct strbuf *path)
 /* Make sure errno contains a meaningful value on error */
 static int lock_file(struct lock_file *lk, const char *path, int flags)
 {
+       int perms = 0666;
+       struct stat st;
+       if (!lstat(path, &st))
+               perms = st.st_mode & 0777;
+
        if (!lock_file_list) {
                /* One-time initialization */
                sigchain_push_common(remove_lock_file_on_signal);
@@ -179,7 +184,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
        if (!(flags & LOCK_NODEREF))
                resolve_symlink(&lk->filename);
        strbuf_addstr(&lk->filename, LOCK_SUFFIX);
-       lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
+       lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, perms);
        if (lk->fd < 0) {
                strbuf_reset(&lk->filename);
                return -1;
Junio C Hamano· Jul 10, 2014, 21:43 UTC · re: Torsten Bögershausen · lore

Re: No fchmd. was: Re: [PATCH 00/14] Add submodule test harness

Torsten Bögershausen <tboegi@web.de> writes:
Show 10 quoted lines
> On 2014-07-10 21.49, Junio C Hamano wrote:
> []
>> If we limit the case to "Inherit permissions from the file we are
>> replacing by taking a lock on it", which is the topic of discussion
>> in this thread, we do not have to worry about how to configure the
>> value (we do not have to) and adding a new parameter to tell the
>> mode to hold-lock-file-for-update is unneeded (the function will
>> have a pathname of the original and can learn the current permission
>> bits itself).
> So something like this:

Yeah, I think something along those lines may be sufficient and we do not have to do anything when closing/committing, at least POSIX systems. I do not know if other filesystems we may care about let you open with 0400 and still write into it, though.

> (I will probably not have the time to make a proper patch :-(

That's OK. I see many names on Cc: who are all capable of helping us ;-)

Show 26 quoted lines
>
> diff --git a/lockfile.c b/lockfile.c
> index 4899270..134d5c8 100644
> --- a/lockfile.c
> +++ b/lockfile.c
> @@ -156,6 +156,11 @@ static void resolve_symlink(struct strbuf *path)
>  /* Make sure errno contains a meaningful value on error */
>  static int lock_file(struct lock_file *lk, const char *path, int flags)
>  {
> +       int perms = 0666;
> +       struct stat st;
> +       if (!lstat(path, &st))
> +               perms = st.st_mode & 0777;
> +
>         if (!lock_file_list) {
>                 /* One-time initialization */
>                 sigchain_push_common(remove_lock_file_on_signal);
> @@ -179,7 +184,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
>         if (!(flags & LOCK_NODEREF))
>                 resolve_symlink(&lk->filename);
>         strbuf_addstr(&lk->filename, LOCK_SUFFIX);
> -       lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
> +       lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, perms);
>         if (lk->fd < 0) {
>                 strbuf_reset(&lk->filename);
>                 return -1;

← back to recent threads