#!/bin/sh

test_description='git for-each-repo builtin'

# We need to test running 'git for-each-repo' outside of a repo context.
TEST_NO_CREATE_REPO=1

. ./test-lib.sh

test_expect_success 'run based on configured value' '
	git init --initial-branch=one one &&
	git init --initial-branch=two two &&
	git -C two worktree add --orphan ../three &&
	git -C three checkout -b three &&
	git init --initial-branch=four ~/four &&

	git -C two commit --allow-empty -m "DID NOT RUN" &&
	git config --global run.key "$TRASH_DIRECTORY/one" &&
	git config --global --add run.key "$TRASH_DIRECTORY/three" &&
	git config --global --add run.key "~/four" &&

	git for-each-repo --config=run.key commit --allow-empty -m "ran" &&
	git -C one log -1 --pretty=format:%s >message &&
	grep ran message &&
	git -C two log -1 --pretty=format:%s >message &&
	! grep ran message &&
	git -C three log -1 --pretty=format:%s >message &&
	grep ran message &&
	git -C ~/four log -1 --pretty=format:%s >message &&
	grep ran message &&

	git for-each-repo --config=run.key -- commit --allow-empty -m "ran again" &&
	git -C one log -1 --pretty=format:%s >message &&
	grep again message &&
	git -C two log -1 --pretty=format:%s >message &&
	! grep again message &&
	git -C three log -1 --pretty=format:%s >message &&
	grep again message &&
	git -C ~/four log -1 --pretty=format:%s >message &&
	grep again message &&

	git -C three for-each-repo --config=run.key -- \
		commit --allow-empty -m "ran from worktree" &&
	git -C one log -1 --pretty=format:%s >message &&
	test_grep "ran from worktree" message &&
	git -C two log -1 --pretty=format:%s >message &&
	test_grep ! "ran from worktree" message &&
	git -C three log -1 --pretty=format:%s >message &&
	test_grep "ran from worktree" message &&
	git -C ~/four log -1 --pretty=format:%s >message &&
	test_grep "ran from worktree" message &&

	# Test running with config values set by environment
	cat >expect <<-EOF &&
	ran from worktree (HEAD -> refs/heads/one)
	ran from worktree (HEAD -> refs/heads/three)
	ran from worktree (HEAD -> refs/heads/four)
	EOF

	GIT_CONFIG_PARAMETERS="${SQ}log.decorate=full${SQ}" \
		git -C three for-each-repo --config=run.key -- log --format="%s%d" -1 >out &&
	test_cmp expect out &&

	cat >test-config <<-EOF &&
	[run]
		key = $(pwd)/one
		key = $(pwd)/three
		key = $(pwd)/four

	[log]
		decorate = full
	EOF

	GIT_CONFIG_GLOBAL="$(pwd)/test-config" \
		git -C three for-each-repo --config=run.key -- log --format="%s%d" -1 >out &&
	test_cmp expect out
'

test_expect_success 'do nothing on empty config' '
	# the whole thing would fail if for-each-ref iterated even
	# once, because "git help --no-such-option" would fail
	git for-each-repo --config=bogus.config -- help --no-such-option
'

test_expect_success 'error on bad config keys' '
	test_expect_code 129 git for-each-repo --config=a &&
	test_expect_code 129 git for-each-repo --config=a.b. &&
	test_expect_code 129 git for-each-repo --config="'\''.b"
'

test_expect_success 'error on NULL value for config keys' '
	cat >>.gitconfig <<-\EOF &&
	[empty]
		key
	EOF
	cat >expect <<-\EOF &&
	error: missing value for '\''empty.key'\''
	EOF
	test_expect_code 129 git for-each-repo --config=empty.key 2>actual.raw &&
	grep ^error actual.raw >actual &&
	test_cmp expect actual
'

test_expect_success '--keep-going' '
	git config --global keep.going non-existing &&
	git config --global --add keep.going one &&

	test_must_fail git for-each-repo --config=keep.going \
		-- branch >out 2>err &&
	test_grep "cannot change to .*non-existing" err &&
	test_must_be_empty out &&

	test_must_fail git for-each-repo --config=keep.going --keep-going \
		-- branch >out 2>err &&
	test_grep "cannot change to .*non-existing" err &&
	git -C one branch >expect &&
	test_cmp expect out
'

test_done
