| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git for-each-repo builtin' |
| 4 | |
| 5 | # We need to test running 'git for-each-repo' outside of a repo context. |
| 6 | TEST_NO_CREATE_REPO=1 |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | test_expect_success 'run based on configured value' ' |
| 11 | git init --initial-branch=one one && |
| 12 | git init --initial-branch=two two && |
| 13 | git -C two worktree add --orphan ../three && |
| 14 | git -C three checkout -b three && |
| 15 | git init --initial-branch=four ~/four && |
| 16 | |
| 17 | git -C two commit --allow-empty -m "DID NOT RUN" && |
| 18 | git config --global run.key "$TRASH_DIRECTORY/one" && |
| 19 | git config --global --add run.key "$TRASH_DIRECTORY/three" && |
| 20 | git config --global --add run.key "~/four" && |
| 21 | |
| 22 | git for-each-repo --config=run.key commit --allow-empty -m "ran" && |
| 23 | git -C one log -1 --pretty=format:%s >message && |
| 24 | grep ran message && |
| 25 | git -C two log -1 --pretty=format:%s >message && |
| 26 | ! grep ran message && |
| 27 | git -C three log -1 --pretty=format:%s >message && |
| 28 | grep ran message && |
| 29 | git -C ~/four log -1 --pretty=format:%s >message && |
| 30 | grep ran message && |
| 31 | |
| 32 | git for-each-repo --config=run.key -- commit --allow-empty -m "ran again" && |
| 33 | git -C one log -1 --pretty=format:%s >message && |
| 34 | grep again message && |
| 35 | git -C two log -1 --pretty=format:%s >message && |
| 36 | ! grep again message && |
| 37 | git -C three log -1 --pretty=format:%s >message && |
| 38 | grep again message && |
| 39 | git -C ~/four log -1 --pretty=format:%s >message && |
| 40 | grep again message && |
| 41 | |
| 42 | git -C three for-each-repo --config=run.key -- \ |
| 43 | commit --allow-empty -m "ran from worktree" && |
| 44 | git -C one log -1 --pretty=format:%s >message && |
| 45 | test_grep "ran from worktree" message && |
| 46 | git -C two log -1 --pretty=format:%s >message && |
| 47 | test_grep ! "ran from worktree" message && |
| 48 | git -C three log -1 --pretty=format:%s >message && |
| 49 | test_grep "ran from worktree" message && |
| 50 | git -C ~/four log -1 --pretty=format:%s >message && |
| 51 | test_grep "ran from worktree" message && |
| 52 | |
| 53 | # Test running with config values set by environment |
| 54 | cat >expect <<-EOF && |
| 55 | ran from worktree (HEAD -> refs/heads/one) |
| 56 | ran from worktree (HEAD -> refs/heads/three) |
| 57 | ran from worktree (HEAD -> refs/heads/four) |
| 58 | EOF |
| 59 | |
| 60 | GIT_CONFIG_PARAMETERS="${SQ}log.decorate=full${SQ}" \ |
| 61 | git -C three for-each-repo --config=run.key -- log --format="%s%d" -1 >out && |
| 62 | test_cmp expect out && |
| 63 | |
| 64 | cat >test-config <<-EOF && |
| 65 | [run] |
| 66 | key = $(pwd)/one |
| 67 | key = $(pwd)/three |
| 68 | key = $(pwd)/four |
| 69 | |
| 70 | [log] |
| 71 | decorate = full |
| 72 | EOF |
| 73 | |
| 74 | GIT_CONFIG_GLOBAL="$(pwd)/test-config" \ |
| 75 | git -C three for-each-repo --config=run.key -- log --format="%s%d" -1 >out && |
| 76 | test_cmp expect out |
| 77 | ' |
| 78 | |
| 79 | test_expect_success 'do nothing on empty config' ' |
| 80 | # the whole thing would fail if for-each-ref iterated even |
| 81 | # once, because "git help --no-such-option" would fail |
| 82 | git for-each-repo --config=bogus.config -- help --no-such-option |
| 83 | ' |
| 84 | |
| 85 | test_expect_success 'error on bad config keys' ' |
| 86 | test_expect_code 129 git for-each-repo --config=a && |
| 87 | test_expect_code 129 git for-each-repo --config=a.b. && |
| 88 | test_expect_code 129 git for-each-repo --config="'\''.b" |
| 89 | ' |
| 90 | |
| 91 | test_expect_success 'error on NULL value for config keys' ' |
| 92 | cat >>.gitconfig <<-\EOF && |
| 93 | [empty] |
| 94 | key |
| 95 | EOF |
| 96 | cat >expect <<-\EOF && |
| 97 | error: missing value for '\''empty.key'\'' |
| 98 | EOF |
| 99 | test_expect_code 129 git for-each-repo --config=empty.key 2>actual.raw && |
| 100 | grep ^error actual.raw >actual && |
| 101 | test_cmp expect actual |
| 102 | ' |
| 103 | |
| 104 | test_expect_success '--keep-going' ' |
| 105 | git config --global keep.going non-existing && |
| 106 | git config --global --add keep.going one && |
| 107 | |
| 108 | test_must_fail git for-each-repo --config=keep.going \ |
| 109 | -- branch >out 2>err && |
| 110 | test_grep "cannot change to .*non-existing" err && |
| 111 | test_must_be_empty out && |
| 112 | |
| 113 | test_must_fail git for-each-repo --config=keep.going --keep-going \ |
| 114 | -- branch >out 2>err && |
| 115 | test_grep "cannot change to .*non-existing" err && |
| 116 | git -C one branch >expect && |
| 117 | test_cmp expect out |
| 118 | ' |
| 119 | |
| 120 | test_done |